SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int solution(int N) {
  2. int longest_binary_gap = -1;
  3. unsigned int mask = 1 << 31;
  4.  
  5. // Find the first occurence of 1
  6. for (; !(mask & N) && mask != 0; mask >>= 1);
  7. while (mask != 0) {
  8.     // Move to the next binary digit
  9.     mask >>= 1;
  10.     int current_gap = 0;
  11.     // Count zeroes
  12.     for (; !(mask & N) && mask != 0; mask >>= 1, current_gap += 1);
  13.  
  14.     // Check if the interval ends with 1 and if it is the longes found so far
  15.     if (mask != 0 && current_gap > longest_binary_gap) {
  16.         longest_binary_gap = current_gap;
  17.     }
  18. }
  19. return (longest_binary_gap < 0) ? 0 : longest_binary_gap;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top