Dada una array arr[] de N elementos. La tarea es encontrar el número máximo de números pares contiguos en la array dada.
Ejemplos :
Entrada : arr[] = {1, 2, 3, 4, 6, 7}
Salida : 2
La secuencia máxima de números pares contiguos es {4, 6}
Entrada : arr[] = {1, 0, 2, 4, 3, 8, 9}
Salida : 3
La secuencia máxima de números pares contiguos es {0, 2, 4}
Enfoque: la idea es seguir recorriendo la array con dos variables de conteo denominadas max_actual y max_so_far. Si se encuentra un número par, incremente current_max y compárelo con max_so_far. Cada vez que se encuentre un elemento impar, restablezca current_max a 0.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to count maximum // contiguous even numbers #include <iostream> using namespace std; // Function to count maximum // contiguous even numbers int countMaxContiguous(int arr[], int n) { int current_max = 0, max_so_far = 0; for (int i = 0; i < n; i++) { // If current element is not even // reset current_max if (arr[i] % 2 != 0) current_max = 0; // If even element is found, increment // current_max and update result if // count becomes more else { current_max++; // increase count max_so_far = max(current_max, max_so_far); } } return max_so_far; } // Driver code int main() { int arr[] = { 1, 0, 2, 4, 3, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); cout << countMaxContiguous(arr, n); return 0; }
Java
// Java program to count maximum // contiguous even numbers import java.io.*; class GFG { // Function to count maximum // contiguous even numbers static int countMaxContiguous(int arr[], int n) { int current_max = 0, max_so_far = 0; for (int i = 0; i < n; i++) { // If current element is not // even reset current_max if (arr[i] % 2 != 0) current_max = 0; // If even element is found, increment // current_max and update result if // count becomes more else { current_max++; // increase count max_so_far = Math.max(current_max, max_so_far); } } return max_so_far; } // Driver code public static void main (String[] args) { int arr[] = { 1, 0, 2, 4, 3, 8, 9 }; int n = arr.length; System.out.println(countMaxContiguous(arr, n)); } } // This code is contributed // by inder_verma
Python 3
# Python 3 program to count maximum # contiguous even numbers # Function to count maximum # contiguous even numbers def countMaxContiguous(arr, n): current_max = 0 max_so_far = 0 for i in range(n) : # If current element is not # even reset current_max if (arr[i] % 2 != 0): current_max = 0 # If even element is found, # increment current_max and # update result if count # becomes more else : current_max += 1 # increase count max_so_far = max(current_max, max_so_far) return max_so_far # Driver code if __name__ == "__main__": arr = [ 1, 0, 2, 4, 3, 8, 9 ] n = len(arr) print(countMaxContiguous(arr, n)) # This code is contributed # by ChitraNayal
C#
// C# program to count maximum // contiguous even numbers using System; class GFG { // Function to count maximum // contiguous even numbers static int countMaxContiguous(int []arr, int n) { int current_max = 0, max_so_far = 0; for (int i = 0; i < n; i++) { // If current element is not // even reset current_max if (arr[i] % 2 != 0) current_max = 0; // If even element is found, increment // current_max and update result if // count becomes more else { current_max++; // increase count max_so_far = Math.Max(current_max, max_so_far); } } return max_so_far; } // Driver code public static void Main () { int []arr = { 1, 0, 2, 4, 3, 8, 9 }; int n = arr.Length; Console.WriteLine(countMaxContiguous(arr, n)); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to count maximum // contiguous even numbers // Function to count maximum // contiguous even numbers function countMaxContiguous(&$arr, $n) { $max_so_far=0; for ($i = 0; $i < $n; $i++) { // If current element is not // even reset current_max if ($arr[$i] % 2 != 0) $current_max = 0; // If even element is found, // increment current_max and // update result if count // becomes more else { // increase count $current_max = $current_max + 1; $max_so_far = max($current_max, $max_so_far); } } return $max_so_far; } // Driver code $arr = array(1, 0, 2, 4, 3, 8, 9 ); $n = sizeof($arr); echo countMaxContiguous($arr, $n); // This code is contributed // by Shivi_Aggarwal ?>
Javascript
<script> // Javascript program to count maximum // contiguous even numbers // Function to count maximum // contiguous even numbers function countMaxContiguous(arr, n) { let current_max = 0, max_so_far = 0; for (let i = 0; i < n; i++) { // If current element is not even // reset current_max if (arr[i] % 2 != 0) current_max = 0; // If even element is found, increment // current_max and update result if // count becomes more else { current_max++; // increase count max_so_far = Math.max(current_max, max_so_far); } } return max_so_far; } // Driver code let arr = [ 1, 0, 2, 4, 3, 8, 9 ]; let n = arr.length; document.write(countMaxContiguous(arr, n)); // This code is contributed by Mayank Tyagi </script>
3
Publicación traducida automáticamente
Artículo escrito por Rohit_ranjan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA