Created
January 21, 2017 20:38
-
-
Save arcesino/0a82344c7a21572f83953b40f9490385 to your computer and use it in GitHub Desktop.
Binary Gap (Java)
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
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