Dado un arreglo arr[] de N enteros, la tarea es encontrar la longitud del subarreglo creciente más largo tal que los elementos en el subarreglo sean enteros consecutivos.
Ejemplos:
Entrada: arr[] = {1, 9, 3, 4, 20, 2}
Salida: 2
Explicación: El subarreglo {3, 4} es el subarreglo más largo de elementos consecutivosEntrada: arr[] = {36, 41, 56, 32, 33, 34, 35, 43, 32, 42}
Salida: 4
Explicación: El subarreglo {32, 33, 34, 35} es el subarreglo más largo de elementos consecutivos
Enfoque: la idea es ejecutar un ciclo y mantener un conteo y un máximo (ambos inicialmente cero). Siga los pasos que se mencionan a continuación:
- Ejecuta un bucle de principio a fin.
- Si el elemento actual no es igual al (elemento anterior+1), establezca el recuento en 1.
- De lo contrario, aumente la cuenta.
- Actualizar max con un máximo de conteo y max.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program to find longest // increasing consecutive subarray #include <bits/stdc++.h> using namespace std; // Returns length of the longest // consecutive subarray int findLongestConseqSubarr(vector<int>& v) { int ans = 0, count = 0; // find the maximum length // by traversing the array for (int i = 0; i < v.size(); i++) { // Check if the current element // is equal to previous element + 1 if (i > 0 && v[i] == v[i - 1] + 1) count++; // reset the count else count = 1; // update the maximum ans = max(ans, count); } return ans; } // Driver code int main() { vector<int> arr = { 1, 9, 3, 4, 20, 2 }; cout << findLongestConseqSubarr(arr); return 0; }
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Returns length of the longest // consecutive subarray static int findLongestConseqSubarr(int arr[ ]) { int ans = 0, count = 0; // find the maximum length // by traversing the array for (int i = 0; i < arr.length; i++) { // Check if the current element // is equal to previous element + 1 if (i > 0 && arr[i] == arr[i - 1] + 1) count++; // reset the count else count = 1; // update the maximum ans = Math.max(ans, count); } return ans; } // Driver code public static void main (String[] args) { int arr[ ] = { 1, 9, 3, 4, 20, 2 }; System.out.print(findLongestConseqSubarr(arr)); } } // This code is contributed by hrithikgarg03188.
Python3
# python3 program to find longest # increasing consecutive subarray # Returns length of the longest # consecutive subarray def findLongestConseqSubarr(v): ans, count = 0, 0 # find the maximum length # by traversing the array for i in range(0, len(v)): # Check if the current element # is equal to previous element + 1 if (i > 0 and v[i] == v[i - 1] + 1): count += 1 # reset the count else: count = 1 # update the maximum ans = max(ans, count) return ans # Driver code if __name__ == "__main__": arr = [1, 9, 3, 4, 20, 2] print(findLongestConseqSubarr(arr)) # This code is contributed by rakeshsahni
C#
// C# program for the above approach using System; class GFG { // Returns length of the longest // consecutive subarray static int findLongestConseqSubarr(int[] arr) { int ans = 0, count = 0; // find the maximum length // by traversing the array for (int i = 0; i < arr.Length; i++) { // Check if the current element // is equal to previous element + 1 if (i > 0 && arr[i] == arr[i - 1] + 1) count++; // reset the count else count = 1; // update the maximum ans = Math.Max(ans, count); } return ans; } // Driver code public static void Main() { int[] arr = { 1, 9, 3, 4, 20, 2 }; Console.Write(findLongestConseqSubarr(arr)); } } // This code is contributed by gfgking
Javascript
<script> // JavaScript code for the above approach // Returns length of the longest // consecutive subarray function findLongestConseqSubarr(v) { let ans = 0, count = 0; // find the maximum length // by traversing the array for (let i = 0; i < v.length; i++) { // Check if the current element // is equal to previous element + 1 if (i > 0 && v[i] == v[i - 1] + 1) count++; // reset the count else count = 1; // update the maximum ans = Math.max(ans, count); } return ans; } // Driver code let arr = [1, 9, 3, 4, 20, 2]; document.write(findLongestConseqSubarr(arr)); // This code is contributed by Potta Lokesh </script>
Producción
2
Complejidad temporal: O(N)
Espacio auxiliar: O(1)