Skip to content

Instantly share code, notes, and snippets.

@alvarezp
Last active March 21, 2016 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvarezp/6d9b593fc591689834bf to your computer and use it in GitHub Desktop.
Save alvarezp/6d9b593fc591689834bf to your computer and use it in GitHub Desktop.
--- binary-gap-old.c 2016-03-21 15:59:35.872587138 -0700
+++ binary-gap.c 2016-03-21 15:59:40.936787734 -0700
@@ -1,16 +1,17 @@
int binary_gap(int N) {
int c=0;
int m=0;
- if (N == 0)
- return 1;
+ while (N > 1 && N % 2 == 0)
+ N = N / 2;
+ N = N / 2;
while (N > 1) {
if (N % 2 == 0) {
c = c + 1;
if (c > m)
m = c;
} else
c = 0;
N = N / 2;
}
return m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment