Hackerrank – Problem description
The problem description – Hackerrank.
Solution
Steps are given in the problem description.
I created solution in:
All solutions are also available on my GitHub profile.
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 |
public class DoubleBasePalindromes { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); int base = stdin.nextInt(); long sum = 0; for(int i = 1; i < n; i++) { String decimalString = Integer.toString(i, 10); String kBaseString = Integer.toString(i, base); if(isPalindrome(decimalString) && isPalindrome(kBaseString)) { sum += i; } } System.out.println(sum); stdin.close(); } private static boolean isPalindrome(String n) { for(int i = 0; i < n.length() / 2; i++) { char first = n.charAt(i); char last = n.charAt(n.length() - i - 1); if(first != last) { return false; } } return true; } } |