二进制数组中最大连续1(或0)

给定二进制数组,查找数组中存在的最大连续1个数的计数。 例如:

null
Input  : arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}Output : 4Input  : arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}Output : 1

A. 简单解决方案 考虑每个子数组,在每个子数组中计数1。最后返回所有1的最大子阵列的大小。 一 有效解决方案 是从左到右遍历数组。如果我们看到1,我们增加计数,并将其与迄今为止的最大值进行比较。如果我们看到0,我们将计数重置为0。

CPP

// C++ program to count maximum consecutive
// 1's in a binary array.
#include<bits/stdc++.h>
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

<?php
// PHP 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]
function getMaxLength( $arr , $n )
{
// initialize count
$count = 0;
// initialize max
$result = 0;
for ( $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
{
// increase count
$count ++;
$result = max( $result , $count );
}
}
return $result ;
}
// Driver code
$arr = array (1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 1);
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo getMaxLength( $arr , $n ) ;
// This code is contributed by nitin mittal.
?>


Javascript

<script>
// JavaScript 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]
function getMaxLength(arr, n) {
// initialize count
let count = 0;
// initialize max
let result = 0;
for (let 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 {
// increase count
count++;
result = Math.max(result, count);
}
}
return result;
}
// Driver code
let arr = new Array(1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 1);
let n = arr.length;
document.write(getMaxLength(arr, n));
// This code is contributed by gfgking
</script>


输出:

4

时间复杂性: O(n) 辅助空间: O(1) 练习: 二进制数组中的最大连续零。 本文由 斯马拉克·乔普达尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论”。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享