📌  相关文章
📜  所有数组元素的二进制表示形式中连续1的最大数目

📅  最后修改于: 2021-05-25 04:35:19             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是在给定数组的所有元素中找到一个元素的二进制表示形式中连续的最大1个数。
例子:

方法:本文讨论了一种在数字的二进制表示形式中找到最大连续1的计数的方法。对于给定数组的所有元素,可以使用相同的方法来找到相同的元素,并且这些值中的最大值是必需的答案。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// maximum consecutive 1s in the
// binary represntation 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 represntation 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 represntation 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 represntation 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 represntation 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 represntation 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 represntation 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 represntation 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


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。