-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
54 lines (50 loc) · 1.82 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Method to compute the number of beautiful days within the given range
static int beautifulDays(int i, int j, int k) {
int beautifulDays = 0;
// Traverse through the range and check if the given day is beautiful
for(int x = i; x <= j; x ++) {
// Get the reverse of the number
int revX = reverse(x);
// Check if the absolute difference between
// x and reverse of x is divisible by k
if(Math.abs(x - revX) % k == 0) {
beautifulDays ++;
}
}
// Finally return the number of beautiful days
return beautifulDays;
}
// Method to compute the reverse of a number
static int reverse(int x) {
int rev = 0;
// Extract the digits and store them in reverse order
while(x > 0) {
rev = rev * 10 + x % 10;
x /= 10;
}
// Finally return the reversed number
return rev;
}
// Driver code
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] ijk = scanner.nextLine().split(" ");
int i = Integer.parseInt(ijk[0]);
int j = Integer.parseInt(ijk[1]);
int k = Integer.parseInt(ijk[2]);
int result = beautifulDays(i, j, k);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}