Created
April 21, 2018 18:26
-
-
Save mhewedy/f8c38ff14554080bd983089ca27495a0 to your computer and use it in GitHub Desktop.
Codility binary gap solution
This file contains hidden or 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
| private static int gap(int N) { | |
| int max = 0; | |
| int counter = -1; | |
| while (N != 0) { | |
| if ((N & 1) == 1) { | |
| if (counter > max) { | |
| max = counter; | |
| } | |
| counter = 0; | |
| } else if (counter >= 0) { | |
| counter++; | |
| } | |
| N >>>= 1; | |
| } | |
| return max; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment