## Overview

In this article we will try to solve one of the problem from HackerRank. The problem is called 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. Return the number of beautiful days in the range.

**Example**

i = 20, j= 23, k = 6

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 – 2)/6| = 3 - Day 21 is
*not beautiful*because the following doesn’t evaluate to a whole number: |(21 – 12)/6| = 1.5 - Day 22 is
*beautiful*because the following evaluates to a whole number: |(22 – 22)/6| = 0 - Day 23 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.

## Code

We will iterate from the start to the last number. We will calculate the reverse of the given number in a separate utility function as below:

```
private static int reverseNumber(int num) {
int reversed = 0;
while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}
```

Then we will calculate the absolute difference between the number and it’s reverse:

```
Math.abs(start - reverseNumber(start))
```

We will check if this is divisible by `k`

, if yes we will increment the count.

```
for (int start = i; start <= j; start++) {
if (Math.abs(start - reverseNumber(start)) % k == 0) {
count++;
}
}
```

*BeautifulDaysAtMovies.java*

```
public class BeautifulDaysAtMovies {
public static int beautifulDays(int i, int j, int k) {
int count = 0;
for (int start = i; start <= j; start++) {
if (Math.abs(start - reverseNumber(start)) % k == 0) {
count++;
}
}
return count;
}
private static int reverseNumber(int num) {
int reversed = 0;
while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}
}
```

## Test

Below is a simple test to verify our logic:

*BeautifulDaysAtMoviesTest.java*

```
import org.junit.Test;
import static org.junit.Assert.*;
public class BeautifulDaysAtMoviesTest {
@Test
public void beautifulDays() {
assertEquals(2, BeautifulDaysAtMovies.beautifulDays(20, 23, 6));
assertEquals(33, BeautifulDaysAtMovies.beautifulDays(13, 45, 3));
}
}
```