Created
June 17, 2021 11:44
-
-
Save bvpatel/fcebd0ec08542a34f8ae8fcbd90b1304 to your computer and use it in GitHub Desktop.
Given a hexadecimal string, s (e.g. "1a", "12b9c7"). Find out the minimum number of pieces, i, to split this string such that each of the i pieces is a hexadecimal representation of a perfect square.
This file contains hidden or 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
/* | |
Apollo likes perfect squares of integers (e.g. 1, 4, 9, 25 etc). You are given a hexadecimal string, s (e.g. "1a", "12b9c7"). Find out the minimum number of pieces, i, to split this string such that each of the i pieces is a hexadecimal representation of a perfect square. Note that this may not be possible for every hexadecimal string. | |
Complete the getMin function in your editor, in a language of your choice. It has one parameter: a string, s, a valid representation of a hexadecimal number. It must return an integer denoting the value of i; if there is no such positive integer i, return ?1. | |
Bonus: Performance is a concern. An unoptimized solution will see timeouts in some of the large test cases. | |
Input Format | |
The locked stub code in your editor reads a hexadecimal string, s, from stdin and passes it to your function. The input will conform to the following formats: | |
The characters in S are valid hexadecimal characters (0-9, a-f). You may assume the letters are in lowercase. | |
1 < |S| < 20 | |
The hexadecimal string S does not start with "0x" | |
Sample values for S: "1a", "12b9c7", "a1111", "0001", | |
Output Format | |
Your function must return either some positive integer i (denoting the minimum number of pieces s must be cut into such that each piece corresponds with the hexadecimal representation of a perfect square), or ?1 if no such i exists. | |
Examples | |
Input: "896bb1" | |
Output: 1 | |
Input: "0000000000000000000000000002" | |
Output: -1 | |
*/ | |
class PerfectRootHex { | |
static char []chars = {'1', '4','9'}; | |
public static String Cus_free_apollo_f81d7Challenge(String str) { | |
int result = getMin(str, 0); | |
if(result == Integer.MAX_VALUE) | |
return "-1"; | |
return String.valueOf(result); | |
} | |
public static int getMin(String hex, int index) { | |
if(index == hex.length()) | |
return 0; | |
int num = 0; | |
int result = Integer.MAX_VALUE; | |
for(int i=index; i<hex.length(); i++) { | |
char ch = hex.charAt(i); | |
num = num*16 + Integer.parseInt(String.valueOf(ch),16); | |
if(ch == '1' || ch == '4' || ch == '9') { | |
if (isPerfectSquare(num)) | |
result = Math.min(result, 1 + getMin(hex, i + 1)); | |
} | |
} | |
return result; | |
} | |
static boolean isPerfectSquare(int x) { | |
if (x >= 0) { | |
int sr = (int)Math.sqrt(x); | |
return ((sr * sr) == x); | |
} | |
return false; | |
} | |
public static void main (String[] args) { | |
System.out.println(Cus_free_apollo_f81d7Challenge("896bb1")); | |
System.out.println(Cus_free_apollo_f81d7Challenge("0000000000000000000000000002")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment