二进制数组中的最大连续一个(或零)
给定二进制数组,找出数组中连续 1 的最大数量。
例子 :
Input : arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}
Output : 4
Input : arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
Output : 1
一个简单的解决方案是考虑每个子数组并在每个子数组中计数 1。最后返回全 1 的最大子数组的返回大小。
一个有效的解决方案是从左到右遍历数组。如果我们看到 1,我们增加计数并将其与迄今为止的最大值进行比较。如果我们看到 0,我们将计数重置为 0。
CPP
// C++ program to count maximum consecutive
// 1's in a binary array.
#include
using namespace std;
// Returns count of maximum consecutive 1's
// in binary array arr[0..n-1]
int getMaxLength(bool arr[], int n)
{
int count = 0; //initialize count
int result = 0; //initialize max
for (int i = 0; i < n; i++)
{
// Reset count when 0 is found
if (arr[i] == 0)
count = 0;
// If 1 is found, increment count
// and update result if count becomes
// more.
else
{
count++;//increase count
result = max(result, count);
}
}
return result;
}
// Driver code
int main()
{
bool arr[] = {1, 1, 0, 0, 1, 0, 1, 0,
1, 1, 1, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << getMaxLength(arr, n) << endl;
return 0;
}
Java
// Java program to count maximum consecutive
// 1's in a binary array.
class GFG {
// Returns count of maximum consecutive 1's
// in binary array arr[0..n-1]
static int getMaxLength(boolean arr[], int n)
{
int count = 0; //initialize count
int result = 0; //initialize max
for (int i = 0; i < n; i++)
{
// Reset count when 0 is found
if (arr[i] == false)
count = 0;
// If 1 is found, increment count
// and update result if count becomes
// more.
else
{
count++;//increase count
result = Math.max(result, count);
}
}
return result;
}
// Driver method
public static void main(String[] args)
{
boolean arr[] = {true, true, false, false,
true, false, true, false,
true, true, true, true};
int n = arr.length;
System.out.println(getMaxLength(arr, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python 3 program to count
# maximum consecutive 1's
# in a binary array.
# Returns count of maximum
# consecutive 1's in binary
# array arr[0..n-1]
def getMaxLength(arr, n):
# initialize count
count = 0
# initialize max
result = 0
for i in range(0, n):
# Reset count when 0 is found
if (arr[i] == 0):
count = 0
# If 1 is found, increment count
# and update result if count
# becomes more.
else:
# increase count
count+= 1
result = max(result, count)
return result
# Driver code
arr = [1, 1, 0, 0, 1, 0, 1,
0, 1, 1, 1, 1]
n = len(arr)
print(getMaxLength(arr, n))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program to count maximum
// consecutive 1's in a binary array.
using System;
class GFG {
// Returns count of maximum consecutive
// 1's in binary array arr[0..n-1]
static int getMaxLength(bool []arr, int n)
{
int count = 0; //initialize count
int result = 0; //initialize max
for (int i = 0; i < n; i++)
{
// Reset count when 0 is found
if (arr[i] == false)
count = 0;
// If 1 is found, increment count
// and update result if count
// becomes more.
else
{
count++; //increase count
result = Math.Max(result, count);
}
}
return result;
}
// Driver code
public static void Main()
{
bool []arr = {true, true, false, false,
true, false, true, false,
true, true, true, true};
int n = arr.Length;
Console.Write(getMaxLength(arr, n));
}
}
// This code is contributed by Nitin Mittal.
PHP
Javascript
输出:
4