Beautiful Days at the Movies Hackerrank problem solution

Beautiful Days at the Movies Hackerrank

Lily likes to play games with integers and their reversals. We can say the games like, for some integer x, we define reversed(x) to be the reversal of all digits in x. For example, reversed(123)=321, reversed(12)=21, and reversed(120)=021.

Logan wants to go to the movies with Lily on some day x satisfying i<=x<=j, but he knows she only goes to the movies on days she considers to be beautiful.And yes, Lily considers a day to be beautiful in its own logical way like if the absolute value of the difference between x and reversed(x) is evenly divisible by k.

Given i, j, and k, count and print the number of beautiful days and help Logan to decide when he can ask Lily to go out to the movies.

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
Output Format

Print the number of beautiful days in the inclusive range.

Sample Input

20 23 6
Sample Output

2
Explanation

Logan wants to 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=3
Day is not beautiful because the following doesn’t evaluate to a whole number: |21-12|/6=1.5
Day is beautiful because the following evaluates to a whole number: |22-22|/6=0
Day is not beautiful because the following doesn’t evaluate to a whole number: |23-32|/6=1.5
Only two days, 20 and 22, in this interval are beautiful. Thus, we print 2 as our answer.

Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int rev(int m){
    int r=0,rem;
    while(m != 0)
    {
        rem = m%10;
        r = r*10 + rem;
        m /= 10;
    }
    return r;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int i,j,re,c=0;
    long int k;
    cin>>i>>j>>k;
    for(int s=i; s<=j; s++){
      double x= (s-rev(s))/double(k);
        if(floor(x) == x)
            c++;
    }
    cout<<c;
    return 0;
}
olution in python
a, b, k = map(int, raw_input().split())
ans = 0
for i in range(a, b+1):
    ans = ans + abs(not (i - int(str(i)[::-1]))%k)
print ans

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 sc=new Scanner(System.in);
        int i=sc.nextInt();
        int c=0;
        int j=sc.nextInt();
        int k=sc.nextInt();
        for(int n=i;n<=j;n++)
            {
              if(  (  n-reversed(Integer.toString(n))  ) % k ==0)
                  c++;
        }
        System.out.print(c);
    }
    static int reversed(String str)
        {
          return(Integer.parseInt(new StringBuffer(str).reverse().toString()));
    }
       
}

you can learn more hackerrank questions and solution Here…..

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.