# Daily HackerRank Challenge - Day 14

Daily HackerRank Challenge (30 Part Series)

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.

# Beautiful Days at the Movies

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. Print the number of beautiful days in the range.

Sample Input

20 23 6


Sample Output

2


Loop from i to j inclusively, calcute its reversed number.

A simple way to reverse a number is to declare a temp variable initialized to 0

int rn=0;


and get each digit

r=n%10;


Add it to rn*10

rn=rn*10+r;


Repeat it until n becomes 0

while(n){
//...
n/=10;
}


For example, the number 20, the first digit is 0.

rn=0*10+0


the second digit is 2

rn=0*10+2


so the reversed number of 20 is 2.

Then, we can apply |i-reverse(i)|%k to see if it is 0, meaning it is evenly divisible by k. If so, increase the counter by 1 and print the output at the end.

Final Solution

int i,j,k;

int reverseNumber(int n){
int r,rn=0;
while(n){
r=n%10;
rn=rn*10+r;
n/=10;
}
return rn;
}

int main()
{
FAST_INP;
int cnt=0;
cin >> i >> j >> k;
FORN(ii,i,j) if(abs(ii-reverseNumber(ii))%k == 0) cnt++;
cout << cnt;
return 0;
}


# Angry Professor

A Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, he decides to cancel class if fewer than some number of students are present when class starts. Arrival times go from on time (arrivalTime <=0) to arrived late (arrivalTime >0).

Given the arrival time of each student and a threshhold number of attendees, determine if the class is canceled.

Sample Input

2
4 3
-1 -3 4 2
4 2
0 -1 2 1


Sample Output

YES
NO


Count how many students have arrived on time (the input is negative or zero) and compare with the cancellation threshold k

Final Solution

int t,n,k,a;

int main()
{
FAST_INP;
cin >> t;
REP(i,t){
int cnt=0;
cin >> n >> k;
REP(j,n) {
cin >> a;
if(a<=0) cnt++;
}
if(k>cnt) cout << "YES" << "\n";
else cout << "NO" << "\n";
}
return 0;
}


# Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

Posted on by:

### Wing-Kam

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.