Created
May 11, 2017 10:01
-
-
Save preetb123/54600c1432995b89810ba91e9ce0dbfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ | |
Scanner scan = new Scanner(System.in); | |
int numTestCases = scan.nextInt(); | |
int[] input = new int[numTestCases]; | |
for(int i=0; i < input.length; i++){ | |
input[i] = scan.nextInt(); | |
} | |
for(int num : input){ | |
System.out.println(countReversibleNumbers(num)); | |
} | |
} | |
public static int countReversibleNumbers(int num){ | |
int count = 0; | |
for(int i=1; i<num; i++){ | |
if(isReversible(i)) | |
count++; | |
} | |
return count; | |
} | |
public static boolean isReversible(int number){ | |
if(number % 10 == 0) return false; | |
int n = number; | |
int reversedNumber = 0; | |
int len = 0; | |
while(n > 0){ | |
n /= 10; | |
len++; | |
} | |
n = number; | |
while(n > 0){ | |
int lastNum = n % 10; | |
reversedNumber += lastNum * Math.pow(10, len - 1); | |
n /= 10; | |
len--; | |
} | |
reversedNumber += number; | |
while(reversedNumber > 0){ | |
if((reversedNumber % 10) % 2 == 0) return false; | |
reversedNumber /= 10; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment