Hacker Rank Practice Question: To find the beautiful day based on the (|digit - reverseDigit|)/k(random integer value) will provide result in double.
Example:
1. |21- 12| /6 = 9/6 = 1.5 (not a beautiful day)
2. |20 - 02|/6 = 18/6= 3.0 (beautiful day)
Hint : to check the value after decimal. using below logic.
if(digit % 1.0 == 0){
//the number represent a beautiful day.
}
int i = startDate of the range
int j = endDate of the range
int k = random value to divide the absolute difference.
My Solution:
public int beautifulDay(int i, int j, int k) {
int count =0;
for(int startDate = i; startDate<= j; startDate++){
int temp = startDate;
int reverseDigit =0 ;
while(true) {
int lastDigit = temp % 10;
int firstDigit = temp / 10;
temp = firstDigit;
reverseDigit = (reverseDigit * 10) + lastDigit;
if(firstDigit ==0 ){
break;
}
}
double absoluteValue = Math.abs((double)startDate - (double)reverseDigit);
if((absoluteValue/k)%1.0 == 0){
count++;
}
}
return count;
}
No comments:
Post a Comment