Dada una array binaria, encuentre el recuento del número máximo de 1 consecutivos presentes en la array.
Ejemplos:
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
Una solución simple es considerar cada subarreglo y contar los 1 en cada subarreglo. Finalmente, devuelva el tamaño de retorno del subarreglo más grande con todos los 1. Una solución eficiente es atravesar la array de izquierda a derecha. Si vemos un 1, incrementamos la cuenta y la comparamos con el máximo hasta el momento. Si vemos un 0, reiniciamos el conteo como 0.
Implementación:
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
Tiempo Complejidad : O(n)
Espacio Auxiliar : O(1)
Ejercicio:
Máximo de ceros consecutivos en una array binaria.
Este artículo es una contribución de Smarak Chopdar . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA