Coding Demons has solved "Beautiful Days at the Movies" a hackerrank problem. Lily is a genius girl who loves to play with integers. One day she discovered a unique method to calculate value if that value is a whole number she used to call it a beautiful day.
She decided she would go only on beautiful days. She used to calculate beautiful days for a range and go to movies. You have the task to find the number of beautiful days on which Lily would go to the movies.
You have given a starting day (i), ending day (j), and a divisor (k). The method to find a beautiful day is for a given number if you reverse that number and subtract it from the original number you get a value that doesn't matter positive negative. And when the answer is divided by the given divisor (k), if you get a whole number, it is a "beautiful day".
You need to find "number of Beautiful Days for given range from i to j".
Example :
Input :
5 4
1 6 3 5 2
Output :
2
Day 20 - 2 = 18/6 =3
Day 21 - 12 = 9/6 = 1.5
Day 22 - 22 = 0/6 = 0
Day 23 - 32 = 9/6 = 1.5
Day 20 & 22 are beautiful days.
Hence, the Number Of Beautiful Days between 20 to 23 is 2.
Logic:
- Take starting day, ending day, and divisor.
- Make a function to find the reverse of a number.
- Set counter to store no of beautiful days.
- Subtract the reverse from the original number.
- Check whether it is evenly divisible by k or not.
- If the number is evenly divisible, increase the counter of beautiful days.
- Print the counter.
Code :
#include<bits/stdc++.h>
using namespace std;
long int rev(long int a)
{
int n=0;
while(a!=0){
n=n*10+a%10;
a=a/10;
}
return n;
}
int main()
{
long int i,j,k,y=0;
cin>>i>>j>>k;
for(i;i<=j;i++)
{
long int x;
x=rev(i)-i; //calling reverse function
if(x%k==0)
{
y++;
}
}
cout<<y;
return 0;
}
0 Comments