Beautiful Days at the Movies HackerRank Solution in C, C++, Java, Python

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


  • 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.


  • 1<=i<=j<=2*10^6
  • 1<=k<=2*10^9

Sample Input

20 23 6


Sample Output



Beautiful Days at the Movies HackerRank Solution in C

#include <stdio.h>
#include <stdlib.h>

int reversed(int a)
    char c, *ptr_b, *ptr_e;
    char buff[12];
    int b;
    //Integer to string
    sprintf(buff, "%d", a);
    //Reversing string
    ptr_b = buff;
    ptr_e = buff;
    while(*ptr_e){           //Moving ptr_e to point the last char in the string
    while(ptr_b < ptr_e){  //Reversing
        c = *ptr_b;
        *ptr_b = *ptr_e;
        *ptr_e = c;
    //String to integer
    sscanf(buff, "%d", &b);
    return b;

int main()
    int i, j, k, rev, diff, count = 0;
    scanf("%d %d %d", &i, &j, &k);
    for(int ii = i; ii <= j; ++ii){
        rev = reversed(ii);
        diff = abs(ii-rev);
        if(diff%k == 0) ++count;

    printf("%d\n", count);
    return 0;


Beautiful Days at the Movies HackerRank Solution in C++

i,j,k = [int(x) for x in input().split()]
count = 0
for x in range(i,j+1):
    if (x - int(str(x)[::-1])) % k == 0:
        count += 1


Beautiful Days at the Movies HackerRank Solution in Java

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(;
        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();
            String temp1 = temp.toString();
            int aRev = Integer.parseInt(temp1);


Beautiful Days at the Movies HackerRank Solution in Python

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


Beautiful Days at the Movies HackerRank Solution in C#

if (rev == n.ToString())
                    revNum = Convert.ToInt32(rev);
                    if ((n - revNum) % k == 0) result++;

                var poly = n.ToString();
                if (poly.Length == 1) result++;
                for (int l = 0; l < poly.Length / 2; l++)
                    if (poly[l] != poly[poly.Length - l - 1])
                    if (l + 1 == poly.Length / 2)

        return result;


Attempt –  HackerRank Challenge

Link –

Next HackerRank Challenge Solution 

Link –


Leave a Comment