Hello Programmers, In this post, you will know how to solve the HackerRank Beautiful Days at the Movies Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Beautiful Days at the Movies Solution
Task
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 |i – reverse(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 i, j, 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 20, 21, 22, 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.
HackerRank Beautiful Days at the Movies Solution
Beautiful Days at the Movies Solution in C
#include<stdio.h> int main() { int i=0,j=0,k=0,x=0,rem=0,sum=0,count=0; scanf("%d%d%d",&i,&j,&k); for(i;i<=j;i++) { x=i; while(x!=0) { rem=x%10; sum=(sum*10)+rem; x=x/10; } if(abs(i-sum)%k==0) count=count+1; sum=0; } printf("%d",count); return 0; }
Beautiful Days at the Movies Solution in Cpp
#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; }
Beautiful Days at the Movies Solution in Java
public static int beautifulDays(int i, int j, int k) { // Write your code here int days = 0; while(i <= j){ if(Math.abs(i - revNumber(i)) % k == 0) days++; i++; } return days; } public static int revNumber(int n){ int rev = 0; while(n != 0){ rev = rev * 10 + n % 10; n /= 10; } return rev; }
Beautiful Days at the Movies Solution in Python
count = 0 while i<=j: rev = str(i) rev = int(rev[::-1]) if abs((i-rev)%k) == 0: count +=1 i += 1 return count
Beautiful Days at the Movies Solution using JavaScript
function beautifulDays(i, j, k) { // Write your code here let count = i; let result = 0; while (count <= j) { let convertNumber = parseInt(count.toString().split("").reverse().join("")); let beautifulDay = Math.abs(count - convertNumber); if (beautifulDay % k === 0) { result++; } count++; } return result; }
Beautiful Days at the Movies Solution in Scala
Beautiful Days at the Movies Solution in Pascal
Disclaimer: This problem (Beautiful Days at the Movies) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.