Maximum number of consecutive 1's in binary representation of all the array elements
Last Updated :
22 Jun, 2022
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[] = {10, 15, 37, 89}
Output: 4
Approach: An approach to finding the count of maximum consecutive 1s in the binary representation of a number has been discussed in this article. The same approach can be used to find the same for all the elements of the given array and the maximum among those values is the required answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <iostream>
using namespace std;
// Function to return the count of
// maximum consecutive 1s in the
// binary representation of x
int maxConsecutiveOnes(int x)
{
// Initialize result
int count = 0;
// Count the number of iterations to
// reach x = 0.
while (x != 0) {
// This operation reduces length
// of every sequence of 1s by one
x = (x & (x << 1));
count++;
}
return count;
}
// Function to return the count of
// maximum consecutive 1s in the
// binary representation among all
// the elements of arr[]
int maxOnes(int arr[], int n)
{
// To store the answer
int ans = 0;
// For every element of the array
for (int i = 0; i < n; i++) {
// Count of maximum consecutive 1s in
// the binary representation of
// the current element
int currMax = maxConsecutiveOnes(arr[i]);
// Update the maximum count so far
ans = max(ans, currMax);
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr) / sizeof(int);
cout << maxOnes(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the count of
// maximum consecutive 1s in the
// binary representation of x
static int maxConsecutiveOnes(int x)
{
// Initialize result
int count = 0;
// Count the number of iterations to
// reach x = 0.
while (x != 0)
{
// This operation reduces length
// of every sequence of 1s by one
x = (x & (x << 1));
count++;
}
return count;
}
// Function to return the count of
// maximum consecutive 1s in the
// binary representation among all
// the elements of arr[]
static int maxOnes(int arr[], int n)
{
// To store the answer
int ans = 0;
// For every element of the array
for (int i = 0; i < n; i++)
{
// Count of maximum consecutive 1s in
// the binary representation of
// the current element
int currMax = maxConsecutiveOnes(arr[i]);
// Update the maximum count so far
ans = Math.max(ans, currMax);
}
return ans;
}
// Driver code
public static void main(String []args)
{
int arr[] = { 1, 2, 3, 4 };
int n = arr.length;
System.out.println(maxOnes(arr, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to return the count of
# maximum consecutive 1s in the
# binary representation of x
def maxConsecutiveOnes(x) :
# Initialize result
count = 0;
# Count the number of iterations to
# reach x = 0.
while (x != 0) :
# This operation reduces length
# of every sequence of 1s by one
x = (x & (x << 1));
count += 1;
return count;
# Function to return the count of
# maximum consecutive 1s in the
# binary representation among all
# the elements of arr[]
def maxOnes(arr, n) :
# To store the answer
ans = 0;
# For every element of the array
for i in range(n) :
# Count of maximum consecutive 1s in
# the binary representation of
# the current element
currMax = maxConsecutiveOnes(arr[i]);
# Update the maximum count so far
ans = max(ans, currMax);
return ans;
# Driver code
if __name__ == "__main__" :
arr = [ 1, 2, 3, 4 ];
n = len(arr);
print(maxOnes(arr, n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count of
// maximum consecutive 1s in the
// binary representation of x
static int maxConsecutiveOnes(int x)
{
// Initialize result
int count = 0;
// Count the number of iterations to
// reach x = 0.
while (x != 0)
{
// This operation reduces length
// of every sequence of 1s by one
x = (x & (x << 1));
count++;
}
return count;
}
// Function to return the count of
// maximum consecutive 1s in the
// binary representation among all
// the elements of arr[]
static int maxOnes(int []arr, int n)
{
// To store the answer
int ans = 0;
// For every element of the array
for (int i = 0; i < n; i++)
{
// Count of maximum consecutive 1s in
// the binary representation of
// the current element
int currMax = maxConsecutiveOnes(arr[i]);
// Update the maximum count so far
ans = Math.Max(ans, currMax);
}
return ans;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine(maxOnes(arr, n));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript implementation of the approach
// Function to return the count of
// maximum consecutive 1s in the
// binary representation of x
function maxConsecutiveOnes(x)
{
// Initialize result
var count = 0;
// Count the number of iterations to
// reach x = 0.
while (x != 0)
{
// This operation reduces length
// of every sequence of 1s by one
x = (x & (x << 1));
count++;
}
return count;
}
// Function to return the count of
// maximum consecutive 1s in the
// binary representation among all
// the elements of arr
function maxOnes(arr , n)
{
// To store the answer
var ans = 0;
// For every element of the array
for (i = 0; i < n; i++)
{
// Count of maximum consecutive 1s in
// the binary representation of
// the current element
var currMax = maxConsecutiveOnes(arr[i]);
// Update the maximum count so far
ans = Math.max(ans, currMax);
}
return ans;
}
// Driver code
var arr = [ 1, 2, 3, 4 ];
var n = arr.length;
document.write(maxOnes(arr, n));
// This code contributed by Princi Singh
</script>
Time Complexity: O(N*log(maxArr)), as we are using a loop to traverse N times and in each traversal, we are calling the function maxConsecutiveOnes which will cost log(maxArr). Where N is the number of elements in the array and maxArr is the element with maximum value in the array.
Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Length of longest consecutive zeroes in the binary representation of a number. 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
4 min read
Maximum consecutive oneâs (or zeros) in a binary array Given a binary array arr[] consisting of only 0s and 1s, find the length of the longest contiguous sequence of either 1s or 0s in the array.Examples : Input: arr[] = [0, 1, 0, 1, 1, 1, 1]Output: 4Explanation: The maximum number of consecutive 1âs in the array is 4 from index 3-6.Input: arr[] = [0, 0
7 min read
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
1 to n bit numbers with no consecutive 1s in binary representation. Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation. Examples: Input : n = 4 Output : 1 2 4 5 8 9 10 These are numbers with 1 to 4 bits and no consecutive ones in binary representation. Input : n = 3 Output : 1 2 4 5 Recommended: Please
6 min read
Maximum consecutive numbers present in an array Find the length of maximum number of consecutive numbers jumbled up in an array.Examples: Input : arr[] = {1, 94, 93, 1000, 5, 92, 78};Output : 3 The largest set of consecutive elements is92, 93, 94 Input : arr[] = {1, 5, 92, 4, 78, 6, 7};Output : 4 The largest set of consecutive elements is4, 5, 6,
15+ min read