HackerRank Beautiful Days at the Movies Solution
In this post, we will solve HackerRank Beautiful Days at the Movies Problem. 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 ……. ĵ] and a number k, determine the number of beautiful days in the field. 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.
beautiful days 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.
Sample Input
20 23 6
Sample Output
2
Beautiful Days at the Movies C Solution
#include <stdio.h>
#include <stdlib.h>
int reverse(int n) {
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
int beautifulDays(int i, int j, int k) {
int count = 0;
for (int day = i; day <= j; day++) {
int rev = reverse(day);
if (abs(day - rev) % k == 0) {
count++;
}
}
return count;
}
int main() {
int i, j, k;
scanf("%d %d %d", &i, &j, &k);
int result = beautifulDays(i, j, k);
printf("%d\n", result);
return 0;
}
Beautiful Days at the Movies C++ Solution
#include <iostream>
#include <cmath>
using namespace std;
int reverse(int n) {
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
int main() {
int i, j, k, beautiful_days = 0;
cin >> i >> j >> k;
for (int day = i; day <= j; day++) {
int rev = reverse(day);
if (abs(day - rev) % k == 0) {
beautiful_days++;
}
}
cout << beautiful_days << endl;
return 0;
}
Beautiful Days at the Movies C Sharp Solution
using System;
class Solution {
static int Reverse(int n) {
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
static void Main(String[] args) {
string[] tokens_i = Console.ReadLine().Split(' ');
int i = int.Parse(tokens_i[0]);
int j = int.Parse(tokens_i[1]);
int k = int.Parse(tokens_i[2]);
int beautiful_days = 0;
for (int day = i; day <= j; day++) {
int rev = Reverse(day);
if (Math.Abs(day - rev) % k == 0) {
beautiful_days++;
}
}
Console.WriteLine(beautiful_days);
}
}
Beautiful Days at the Movies Java Solution
import java.util.Scanner;
public class Solution {
static int reverse(int n) {
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
int j = in.nextInt();
int k = in.nextInt();
int beautiful_days = 0;
for (int day = i; day <= j; day++) {
int rev = reverse(day);
if (Math.abs(day - rev) % k == 0) {
beautiful_days++;
}
}
System.out.println(beautiful_days);
}
}
Beautiful Days at the Movies javaScript Solution
function reverse(n) {
let rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n = Math.floor(n / 10);
}
return rev;
}
function beautifulDays(i, j, k) {
let beautiful_days = 0;
for (let day = i; day <= j; day++) {
let rev = reverse(day);
if (Math.abs(day - rev) % k === 0) {
beautiful_days++;
}
}
return beautiful_days;
}
console.log(beautifulDays(20, 23, 6)); // Output: 2
Beautiful Days at the Movies Python Solution
def reverse(n):
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
return rev
def beautifulDays(i, j, k):
beautiful_days = 0
for day in range(i, j+1):
rev = reverse(day)
if abs(day - rev) % k == 0:
beautiful_days += 1
return beautiful_days
print(beautifulDays(20, 23, 6)) # Output: 2
Other Solution