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

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*10^6
  • 1<=k<=2*10^9

Sample Input

20 23 6

 

Sample Output

2

 

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
        ++ptr_e;
    }
    --ptr_e;
    while(ptr_b < ptr_e){  //Reversing
        c = *ptr_b;
        *ptr_b = *ptr_e;
        *ptr_e = c;
        ++ptr_b;
        --ptr_e;
    }
    
    //String to integer
    sscanf(buff, "%d", &b);
    
    return b;
}


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

    //Output
    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
print(count)

 

Beautiful Days at the Movies HackerRank Solution in 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);
    }
}

 

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())
                {
                    result++;
                }
                else
                {
                    revNum = Convert.ToInt32(rev);
                    if ((n - revNum) % k == 0) result++;
                }

            }
            else
            {
                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])
                    {
                        break;
                    }
                    if (l + 1 == poly.Length / 2)
                    {
                        result++;
                    }
                }
            }
        }

        return result;

 

Attempt –  HackerRank Challenge

Link – https://www.hackerrank.com/challenges/beautiful-days-at-the-movies/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/viral-advertising-hackerrank-solution/

 

Leave a Comment