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++
#include <iostream>
using namespace std;
int maxConsecutiveOnes( int x)
{
int count = 0;
while (x != 0) {
x = (x & (x << 1));
count++;
}
return count;
}
int maxOnes( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int currMax = maxConsecutiveOnes(arr[i]);
ans = max(ans, currMax);
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof (arr) / sizeof ( int );
cout << maxOnes(arr, n);
return 0;
}
|
Java
class GFG
{
static int maxConsecutiveOnes( int x)
{
int count = 0 ;
while (x != 0 )
{
x = (x & (x << 1 ));
count++;
}
return count;
}
static int maxOnes( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
int currMax = maxConsecutiveOnes(arr[i]);
ans = Math.max(ans, currMax);
}
return ans;
}
public static void main(String []args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int n = arr.length;
System.out.println(maxOnes(arr, n));
}
}
|
Python3
def maxConsecutiveOnes(x) :
count = 0 ;
while (x ! = 0 ) :
x = (x & (x << 1 ));
count + = 1 ;
return count;
def maxOnes(arr, n) :
ans = 0 ;
for i in range (n) :
currMax = maxConsecutiveOnes(arr[i]);
ans = max (ans, currMax);
return ans;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ];
n = len (arr);
print (maxOnes(arr, n));
|
C#
using System;
class GFG
{
static int maxConsecutiveOnes( int x)
{
int count = 0;
while (x != 0)
{
x = (x & (x << 1));
count++;
}
return count;
}
static int maxOnes( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
int currMax = maxConsecutiveOnes(arr[i]);
ans = Math.Max(ans, currMax);
}
return ans;
}
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine(maxOnes(arr, n));
}
}
|
Javascript
<script>
function maxConsecutiveOnes(x)
{
var count = 0;
while (x != 0)
{
x = (x & (x << 1));
count++;
}
return count;
}
function maxOnes(arr , n)
{
var ans = 0;
for (i = 0; i < n; i++)
{
var currMax = maxConsecutiveOnes(arr[i]);
ans = Math.max(ans, currMax);
}
return ans;
}
var arr = [ 1, 2, 3, 4 ];
var n = arr.length;
document.write(maxOnes(arr, n));
</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.