Skip to content

Instantly share code, notes, and snippets.

@arcesino
Created January 21, 2017 20:38
Show Gist options
  • Save arcesino/0a82344c7a21572f83953b40f9490385 to your computer and use it in GitHub Desktop.
Save arcesino/0a82344c7a21572f83953b40f9490385 to your computer and use it in GitHub Desktop.
Binary Gap (Java)
public class BinaryGap {
public static int maxBinaryGap(int N) {
int maxGap = 0;
int currentGap = 0;
boolean oneFound = false;
while (N > 0) {
if (N % 2 == 1) {
oneFound = true;
maxGap = Math.max(currentGap, maxGap);
currentGap = 0;
} else if (oneFound) {
currentGap++;
}
N /= 2;
}
return maxGap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment