Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse.
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, and a number, determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where is evenly divisible. If a day's value is a beautiful number, it is a beautiful day. Return the number of beautiful days in the range.
Example:
Input: i=20,j=23,k=6
Output: 2
Approach
Java
public class BeautifulDays {public static void main(String[] args) {int i = 20, j = 23, k = 6;System.out.println(beautifulDays(i, j, k));}static int beautifulDays(int i, int j, int k) {int no = 0, l;float m;for (l = i; l <= j; l++) {m = Math.abs(l - rev(l));if (m / k == (int) m / k)no++;}return no;}static int rev(int x) {int rev = 0;while (x > 0) {int temp = x % 10;rev = rev * 10 + temp;x = x / 10;}return (rev);}}
C++
#include <bits/stdc++.h>using namespace std;long long rev(long long x){long long rev = 0;while (x > 0){int temp = x % 10;rev = rev * 10 + temp;x = x / 10;}return (rev);}int beautifulDays(int i, int j, int k){int no = 0, l;float m;for (l = i; l <= j; l++){m = abs(l - rev(l));if (m / k == (int)m / k)no++;}return no;}int main(){long long i = 20, j = 23, k = 6;cout << beautifulDays(i, j, k);return 0;}
No comments:
Post a Comment