Maximum distance between two 1's in Binary representation of N Last Updated : 21 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number N, the task is to find the maximum distance between two 1's in the binary representation of given N. Print -1 if binary representation contains less than two 1's.Examples: Input: N = 131 Output: 7 131 in binary = 10000011. The maximum distance between two 1's = 7. Input: N = 8 Output: -1 8 in binary = 01000. It contains less than two 1's. Approach: First find the binary representation of N.For each bit calculated, check if its a '1'.Store the index of first '1' found in first_1, and the last '1' found in last_1Then check if the last_1 is less than or equal to first_1. It will be the case when N is a power of 2. Hence print -1 in this case.In any other case, find the difference between the last_1 and first_1. This will be the required distance. Below is the implementation of the above approach: C++ // C++ program to find the // Maximum distance between two 1's // in Binary representation of N #include <bits/stdc++.h> using namespace std; int longest_gap(int N) { int distance = 0, count = 0, first_1 = -1, last_1 = -1; // Compute the binary representation while (N) { count++; int r = N & 1; if (r == 1) { first_1 = first_1 == -1 ? count : first_1; last_1 = count; } N = N / 2; } // if N is a power of 2 // then return -1 if (last_1 <= first_1) { return -1; } // else find the distance // between the first position of 1 // and last position of 1 else { distance = (last_1 - first_1); return distance; } } // Driver code int main() { int N = 131; cout << longest_gap(N) << endl; N = 8; cout << longest_gap(N) << endl; N = 17; cout << longest_gap(N) << endl; N = 33; cout << longest_gap(N) << endl; return 0; } Java // Java program to find the // Maximum distance between two 1's // in Binary representation of N class GFG { static int longest_gap(int N) { int distance = 0, count = 0, first_1 = -1, last_1 = -1; // Compute the binary representation while (N != 0) { count++; int r = N & 1; if (r == 1) { first_1 = first_1 == -1 ? count : first_1; last_1 = count; } N = N / 2; } // if N is a power of 2 // then return -1 if (last_1 <= first_1) { return -1; } // else find the distance // between the first position of 1 // and last position of 1 else { distance = (last_1 - first_1); return distance; } } // Driver code public static void main (String[] args) { int N = 131; System.out.println(longest_gap(N)); N = 8; System.out.println(longest_gap(N)); N = 17; System.out.println(longest_gap(N)); N = 33; System.out.println(longest_gap(N)); } } // This code is contributed by AnkitRai01 Python3 # Python3 program to find the # Maximum distance between two 1's # in Binary representation of N def longest_gap(N): distance = 0 count = 0 first_1 = -1 last_1 = -1 # Compute the binary representation while (N > 0): count += 1 r = N & 1 if (r == 1): if first_1 == -1: first_1 = count else: first_1 = first_1 last_1 = count N = N // 2 # if N is a power of 2 # then return -1 if (last_1 <= first_1): return -1 # else find the distance # between the first position of 1 # and last position of 1 else: distance = last_1 - first_1 return distance # Driver code N = 131 print(longest_gap(N)) N = 8 print(longest_gap(N)) N = 17 print(longest_gap(N)) N = 33 print(longest_gap(N)) # This code is contributed by Mohit Kumar C# // C# program to find the // Maximum distance between two 1's // in Binary representation of N using System; class GFG { static int longest_gap(int N) { int distance = 0, count = 0, first_1 = -1, last_1 = -1; // Compute the binary representation while (N != 0) { count++; int r = N & 1; if (r == 1) { first_1 = first_1 == -1 ? count : first_1; last_1 = count; } N = N / 2; } // if N is a power of 2 // then return -1 if (last_1 <= first_1) { return -1; } // else find the distance // between the first position of 1 // and last position of 1 else { distance = (last_1 - first_1); return distance; } } // Driver code public static void Main (String []args) { int N = 131; Console.WriteLine(longest_gap(N)); N = 8; Console.WriteLine(longest_gap(N)); N = 17; Console.WriteLine(longest_gap(N)); N = 33; Console.WriteLine(longest_gap(N)); } } // This code is contributed by Arnab Kundu JavaScript <script> // Javascript program to find the // Maximum distance between two 1's // in Binary representation of N function longest_gap(N) { let distance = 0, count = 0, first_1 = -1, last_1 = -1; // Compute the binary representation while (N) { count++; let r = N & 1; if (r == 1) { first_1 = first_1 == -1 ? count : first_1; last_1 = count; } N = parseInt(N / 2); } // if N is a power of 2 // then return -1 if (last_1 <= first_1) { return -1; } // else find the distance // between the first position of 1 // and last position of 1 else { distance = (last_1 - first_1); return distance; } } // Driver code let N = 131; document.write(longest_gap(N) + "<br>"); N = 8; document.write(longest_gap(N) + "<br>"); N = 17; document.write(longest_gap(N) + "<br>"); N = 33; document.write(longest_gap(N) + "<br>"); </script> Output7 -1 4 5 Time Complexity: O(log N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum distance between two 1's in Binary representation of N S shivanshukumarsingh1 Follow Improve Article Tags : Bit Magic Mathematical DSA binary-representation Practice Tags : Bit MagicMathematical Similar Reads Maximum 0's between two immediate 1's in binary representation Given a number n, the task is to find the maximum 0's between two immediate 1's in binary representation of given n. Return -1 if binary representation contains less than two 1's. Examples : Input : n = 47 Output: 1 // binary of n = 47 is 101111 Input : n = 549 Output: 3 // binary of n = 549 is 1000 9 min read Maximum distance between two 1s in a Binary Array in a given range Given a binary array of size N and a range in [l, r], the task is to find the maximum distance between two 1s in this given range.Examples: Input: arr = {1, 0, 0, 1}, l = 0, r = 3 Output: 3 In the given range from 0 to 3, first 1 lies at index 0 and last at index 3. Hence, maximum distance = 3 - 0 = 14 min read Find the maximum between N and the number formed by reversing 32-bit binary representation of N Given a positive 32-bit integer N, the task is to find the maximum between the value of N and the number obtained by decimal representation of reversal of binary representation of N in a 32-bit integer. Examples: Input: N = 6Output: 1610612736Explanation: Binary representation of 6 in a 32-bit integ 6 min read Maximum distance between adjacent 1s in given Binary String Given a binary string S containing N characters, the task is to find the maximum distance between two adjacent 1's. Examples: Input: S = "1010010"Output: 3Explanation: There are 2 sets of adjacent 1's in the given index on the indices {0, 2} and {2, 5}. The one with the maximum distance among them i 12 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 Like