Created
April 9, 2017 14:29
-
-
Save green-coder/c9f56d0dfe179e06b181a2585c9afe7f to your computer and use it in GitHub Desktop.
Google Code Jam 2017, Qualification Round, readable C solution
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
/** | |
* Complexity O(log(k)) | |
* Terminal recursion | |
*/ | |
private static String solve(long n, long k) { | |
if (k == 1) { | |
long bigHalf = n / 2; | |
long smallHalf = (n - 1) - bigHalf; | |
return "" + bigHalf + " " + smallHalf; | |
} | |
boolean k_odd = ((k & 1) == 1); | |
boolean n_odd = ((n & 1) == 1); | |
if (n_odd) { | |
if (k_odd) { | |
return solve((n - 1) / 2, (k - 1) / 2); | |
} | |
else { | |
return solve((n - 1) / 2, k / 2); | |
} | |
} | |
else { | |
if (k_odd) { | |
return solve((n / 2) - 1, (k - 1) / 2); | |
} | |
else { | |
return solve(n / 2, k / 2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment