Skip to content

Instantly share code, notes, and snippets.

@vdenotaris
Last active June 9, 2017 08:58
Show Gist options
  • Save vdenotaris/6fbb812b5bf2429ad447c3d6178ed723 to your computer and use it in GitHub Desktop.
Save vdenotaris/6fbb812b5bf2429ad447c3d6178ed723 to your computer and use it in GitHub Desktop.
Codility: Binary Gap
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