Last active
June 9, 2017 08:58
-
-
Save vdenotaris/6fbb812b5bf2429ad447c3d6178ed723 to your computer and use it in GitHub Desktop.
Codility: Binary Gap
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
class Solution { | |
public int solution(int N) { | |
String binN = Integer.toBinaryString(N); | |
int counter = 0; | |
int maxCounter = 0; | |
for (int i = 1; i < binN.length() - 1; i++) { | |
if (binN.charAt(i) == '0' && binN.charAt(i-1) == '1') { | |
counter = 1; | |
for (int j = i+1; j < binN.length(); j++) { | |
if (binN.charAt(j) == '0') | |
counter++; | |
else { | |
if (maxCounter < counter) | |
maxCounter = counter; | |
counter = 0; | |
i = j; | |
j = binN.length(); | |
} | |
} | |
} | |
} | |
return maxCounter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment