Algorithm-1
Hello everyone. I have taken this question from hackerrank and posted solution for it.
And hope that it will be helpful for you.
Problem Tittle:- Beautiful days at movies
Description:-
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 is 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. Print the number of beautiful days in the range.
Input Format:-
A single line of three space-separated integers describing the respective values of i,jand k;
Constraints:-
1<=j<=2*(10^6)
1<=k<=2*10^9
Output Format:-
Print the number of beautiful days in the inclusive range between i and j;
Sample Input
20 23 6
Sample Output
2
Explanation
Lily may 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=18/6=3.
* Day 21 is not beautiful because the following doesn't evaluate to a whole number (|21-12|)/6=9/6=1.5.
* Day 22 is beautiful because the following evaluates to a whole number (|22-22|)/6=0/6=0.
* Day 23 is not beautiful because the following doesn't evaluate to a whole number (|23-32|)/6=9/6=1.5.
Only two days,20 and 22, in this interval are beautiful. Thus, we print 2 as our answer.
Solution:-
//this function will return the reverse of number n.
int reverse(int n)
{
int r;
int rev=0;
while(n!=0)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
return rev;
}
//this function will print the number of beautiful days.
void beautifulDays(int i, int j, int k) {
int num,count=0;
for(int n=i;n<=j;n++)
{
rev=reverse(n);
if(((n-rev)%k)==0)
count++;
}
print(count);
}
//So our required output(number of beautiful days) is stored in 'count' variable which is printed.
And hope that it will be helpful for you.
Problem Tittle:- Beautiful days at movies
Description:-
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 is 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. Print the number of beautiful days in the range.
Input Format:-
A single line of three space-separated integers describing the respective values of i,jand k;
Constraints:-
1<=j<=2*(10^6)
1<=k<=2*10^9
Output Format:-
Print the number of beautiful days in the inclusive range between i and j;
Sample Input
20 23 6
Sample Output
2
Explanation
Lily may 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=18/6=3.
* Day 21 is not beautiful because the following doesn't evaluate to a whole number (|21-12|)/6=9/6=1.5.
* Day 22 is beautiful because the following evaluates to a whole number (|22-22|)/6=0/6=0.
* Day 23 is not beautiful because the following doesn't evaluate to a whole number (|23-32|)/6=9/6=1.5.
Only two days,20 and 22, in this interval are beautiful. Thus, we print 2 as our answer.
Solution:-
//this function will return the reverse of number n.
int reverse(int n)
{
int r;
int rev=0;
while(n!=0)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
return rev;
}
//this function will print the number of beautiful days.
void beautifulDays(int i, int j, int k) {
int num,count=0;
for(int n=i;n<=j;n++)
{
rev=reverse(n);
if(((n-rev)%k)==0)
count++;
}
print(count);
}
//So our required output(number of beautiful days) is stored in 'count' variable which is printed.
Good. Looking after next algorithms.
ReplyDelete