Beautiful Days at the Movies – HackerRank Solution

In this post, we will solve Beautiful Days at the Movies HackerRank Solution. This problem (Beautiful Days at the Movies) is a part of HackerRank Algorithm series.

Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse. For instance, given the number 12, its reverse is 21. Their difference is 9. The number 120 reversed is 21, and their difference is 99.

She decides to apply her game to decision making. She will look at a numbered range of days and will only go to a movie on a beautiful day.

Given a range of numbered days, [i . . . j] and a number k, determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where |ireverse(i)| is evenly divisible by k. If a day’s value is a beautiful number, it is a beautiful day. Return the number of beautiful days in the range.

Function Description

Complete the beautifulDays function in the editor below.

beautifulDays has the following parameter(s):

• int i: the starting day number
• int j: the ending day number
• int k: the divisor

Returns

• int: the number of beautiful days in the range

Input Format

A single line of three space-separated integers describing the respective values of ij, and k.

Constraints

• 1 <= i <= j <= 2 x 106
• 1 <= k <= 2 x 109

Sample Input

``20 23 6``

Sample Output

``2``

Explanation

Lily may go to the movies on days 202122, and 23. We perform the following calculations to determine which days are beautiful:

• Day 20 is beautiful because the following evaluates to a whole number: |20 – 02| /6 = 18/6 = 3
• Day 21 is not beautiful because the following doesn’t evaluate to a whole number: |21 – 12|/6 = 9/6 = 1.5
• Day 22 is beautiful because the following evaluates to a whole number: |22 – 22|/6 = 0/6 = 0
• Day 23 is not beautiful because the following doesn’t evaluate to a whole number: |23 – 32|/6 = 9/6 = 1.5

Only two days, 20 and 22, in this interval are beautiful. Thus, we print 2 as our answer.

Solution – Beautiful Days at the Movies – HackerRank Solution

C++

```#include <iostream>

using namespace std;

bool isOk(int x, int mod) {
int n = x;
int m = 0;
while (x > 0) {
m = m * 10 + x % 10;
x /= 10;
}
int delta = abs(n - m);
delta %= mod;
return (delta == 0);
}

int main() {
int l, r, k;
cin >> l >> r >> k;
int ans = 0;
for (int i = l; i <= r; i++) {
if (isOk(i, k)) {
++ans;
}
}
cout << ans << endl;
return 0;
}
```

Python

```#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'beautifulDays' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER i
#  2. INTEGER j
#  3. INTEGER k
#

def beautifulDays(i, j, k):
daycount = int(0)
for x in range(i, j+1):
if (x - int(str(x)[::-1])) % k == 0:
daycount += 1
return daycount

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

first_multiple_input = input().rstrip().split()

i = int(first_multiple_input[0])

j = int(first_multiple_input[1])

k = int(first_multiple_input[2])

result = beautifulDays(i, j, k)

fptr.write(str(result) + '\n')

fptr.close()
```

Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int i = in.nextInt();
int j = in.nextInt();
int k = in.nextInt();
int count = 0;

for (int a=i;j>a; a++){
StringBuilder temp = new StringBuilder();
temp.append(a);
temp=temp.reverse();
String temp1 = temp.toString();
int aRev = Integer.parseInt(temp1);
if(Math.abs((a-aRev)%k)==0){
count++;
}
}

System.out.println(count);
}
}
```

Note: This problem (Beautiful Days at the Movies) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.