Beautiful Days at the Movies

Parag Naik
2 min readFeb 5, 2021

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.

Photo by Myke Simon on Unsplash

Solution-A very different and interesting hackerrank problem in terms of problem story narrative. Let move towards the code, as we are give two number of days we have to generate a series of number of days, so we use a for loop to generate the range of number of days. We need to substract the number with the reversal of it for ex.32 reversed becomes 23 or 80 becomes 08. To determine a beautiful day we substract the number with its reversal and divide it with a factor ‘K’ and result should completely divisible by the factor then that specific iteration no or day number will be beautiful.

def beautifulDays(i, j, k):
count=0
for num in range(i,j+1):
rev=str(num)
rev="".join(reversed(rev))
beautiful_num=(num-int(rev))%k
if beautiful_num==0:
count+=1
return count

First we will have to reverse the number as there is no in-built function to reverse a integer we will have to convert it into a string and the substract the reversed number store in “rev” variable with the “num” iteration and divide(modulo) it with “K” factor and store its result in “beautiful_num”.We will also initialize a counter an and increment it every time “beautiful_num” will become 0 and return its value at the end of the for loop.

Conclusion-
Enjoying what you do is the most important thing,I never thought i would start writing a coding blog looking at my coding skill but the day i started enjoying it , i was able to learn it.Keep learning and keep hustling…(cheers:-PN)

--

--