Guest User

Untitled

a guest
Jan 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  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;
Add Comment
Please, Sign In to add comment