Length of longest consecutive zeroes in the binary representation of a number. Last Updated : 23 Nov, 2022 Comments Improve Suggest changes Like Article Like Report We have a number N. Determine the length of the longest consecutive 0's in its binary representation. Examples: Input : N = 14 Output : 1 Binary representation of 14 is 1110. There is only one 0 in the binary representation. Input : N = 9 Output : 2 A simple approach is to traverse through all bits and keep track of the maximum number of consecutive 0s. C++ // C++ code to determine Length of // longest consecutive zeroes in the // binary representation of a number. #include <bits/stdc++.h> using namespace std; int maxZeros(int N) { // variable to store the length of // longest consecutive 0's int maxm = -1; // to temporary store the consecutive 0's int cnt = 0; while (N) { if (!(N & 1)) { cnt++; N >>= 1; maxm = max(maxm, cnt); } else { maxm = max(maxm, cnt); cnt = 0; N >>= 1; } } return maxm; } // Driver code int main() { int N = 14; cout << maxZeros(N) << endl; return 0; } Java // Java code to determine Length of // longest consecutive zeroes in the // binary representation of a number. public class GFG { static int maxZeros(int N) { // variable to store the length of // longest consecutive 0's int maxm = -1; // to temporary store the consecutive 0's int cnt = 0; while (N != 0) { if ((N & 1) == 0 ) { cnt++; N >>= 1; maxm = Math.max(maxm, cnt); } else { maxm = Math.max(maxm, cnt); cnt = 0; N >>= 1; } } return maxm; } // Driver code public static void main(String args[]) { int N = 14; System.out.println(maxZeros(N)); } // This Code is contributed by ANKITRAI1 } Python3 # Python3 code to determine Length of # longest consecutive zeroes in the # binary representation of a number. def maxZeros(N): # variable to store the length # of longest consecutive 0's maxm = -1 # to temporary store the # consecutive 0's cnt = 0 while(N): if(not(N & 1)): cnt += 1 N >>= 1 maxm = max(maxm,cnt) else: maxm = max(maxm,cnt) cnt = 0 N >>= 1 return maxm # Driver Code N = 14 print(maxZeros(N)) # This code is written by Shrikant13 C# // C# code to determine Length of // longest consecutive zeroes in the // binary representation of a number. using System; class GFG { static int maxZeros(int N) { // variable to store the length // of longest consecutive 0's int maxm = -1; // to temporary store the // consecutive 0's int cnt = 0; while (N != 0) { if ((N & 1) == 0 ) { cnt++; N >>= 1; maxm = Math.Max(maxm, cnt); } else { maxm = Math.Max(maxm, cnt); cnt = 0; N >>= 1; } } return maxm; } // Driver code public static void Main() { int N = 14; Console.WriteLine(maxZeros(N)); } } // This code is contributed // by anuj_67 PHP <?php // PHP code to determine Length of // longest consecutive zeroes in the // binary representation of a number. function maxZeros($N) { // variable to store the length // of longest consecutive 0's $maxm = -1; // to temporary store the // of consecutive 0's $cnt = 0; while ($N) { if (!($N & 1)) { $cnt++; $N >>= 1; $maxm = max($maxm, $cnt); } else { $maxm = max($maxm, $cnt); $cnt = 0; $N >>= 1; } } return $maxm; } // Driver code $N = 14; echo (maxZeros($N)); // This code is contributed // by Shivi_Aggarwal ?> JavaScript <script> // Javascript code to determine Length of // longest consecutive zeroes in the // binary representation of a number. function maxZeros(N) { // variable to store the length // of longest consecutive 0's let maxm = -1; // to temporary store the // consecutive 0's let cnt = 0; while (N != 0) { if ((N & 1) == 0 ) { cnt++; N >>= 1; maxm = Math.max(maxm, cnt); } else { maxm = Math.max(maxm, cnt); cnt = 0; N >>= 1; } } return maxm; } let N = 14; document.write(maxZeros(N)); </script> Output1 Time Complexity: O(log2N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Length of longest consecutive zeroes in the binary representation of a number. S Shashank_Pathak Follow Improve Article Tags : Bit Magic Technical Scripter DSA Technical Scripter 2018 Practice Tags : Bit Magic Similar Reads Length of the Longest Consecutive 1s in Binary Representation Given a number N, The task is to find the length of the longest consecutive 1s series in its binary representation.Examples : Input: N = 14Output: 3Explanation: The binary representation of 14 is 1110. Input: N = 222Output: 4Explanation: The binary representation of 222 is 11011110. Recommended Prac 9 min read Find consecutive 1s of length >= n in binary representation of a number Given two integers x and n, the task is to search for the first consecutive stream of 1s (in the x's 32-bit binary representation) which is greater than or equal to n in length and return its position. If no such string exists then return -1.Examples: Input: x = 35, n = 2 Output: 31 Binary represent 10 min read Number of leading zeros in binary representation of a given number Given a positive integer N, the task is to find the number of leading zeros in its binary representation.A leading zero is any 0 digit that comes before the first nonzero digit in a number's binary form. Examples: Input : N = 16Output : 27Explanation: As Binary(16) = (0000000000000000000000000001000 10 min read Maximum number of consecutive 1's in binary representation of all the array elements Given an array arr[] of N elements, the task is to find the maximum number of consecutive 1's in the binary representation of an element among all the elements of the given array. Examples: Input: arr[] = {1, 2, 3, 4} Output: 2 Binary(1) = 01 Binary(2) = 10 Binary(3) = 11 Binary(4) = 100 Input: arr[ 6 min read Length of second longest sequence of consecutive 1s in a binary array Given a binary array arr[] of size N, the task is to find the length of the second longest sequence of consecutive 1s present in the array. Examples: Input: arr[] = {1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0} Output: 4 3 Explanation: Longest sequence of consecutive ones is 4 i.e {arr[7], ... arr[10]} 7 min read Like