Dado un arreglo arr[] de tamaño N , la tarea es imprimir el subarreglo creciente más largo de modo que los elementos del subarreglo sean enteros consecutivos .
Ejemplos :
Entrada: arr[] = {1, 9, 3, 4, 20, 2}
Salida: {3, 4}
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: {32, 33, 34, 35}
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:
- Ejecutar un bucle de principio a fin
- Si el elemento actual no es igual al ( elemento anterior+1 ), establezca el recuento en 1 y actualice los puntos inicial y final de la ventana.
- De lo contrario , aumente la cuenta
- Finalmente, imprima los elementos de la ventana.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to return the longest // consecutive subarray void findLongestConseqSubarr(vector<int>& v) { int ans = 0, count = 0, start = 0, end = 0, x, y; // 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++; end = i; } // Reset the count else { start = i; count = 1; } // Update the maximum if (ans < count) { ans = count; x = start; y = end; } } for (int i = x; i <= y; i++) cout << v[i] << ", "; } // Driver Code int main() { vector<int> arr = { 1, 9, 3, 4, 20, 2 }; findLongestConseqSubarr(arr); return 0; }
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to return the longest // consecutive subarray static void findLongestConseqSubarr(int arr[ ]) { int ans = 0, count = 0, start = 0, end = 0, x = 0, y = 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++; end = i; } // Reset the count else { start = i; count = 1; } // Update the maximum if (ans < count) { ans = count; x = start; y = end; } } for (int i = x; i <= y; i++) System.out.print(arr[i] + ", "); } public static void main (String[] args) { int arr[ ] = { 1, 9, 3, 4, 20, 2 }; findLongestConseqSubarr(arr); } } // This code is contributed by hrithikgarg03188
Python3
# Python program for the above approach # Function to return the longest # consecutive subarray def findLongestConseqSubarr(v): ans = 0 count = 0 start = 0 end = 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 = count + 1 end = i # Reset the count else: start = i count = 1 # Update the maximum if (ans < count): ans = count x = start y = end for i in range(x, y + 1): print(v[i], end = ", ") # Driver Code arr = [1, 9, 3, 4, 20, 2] findLongestConseqSubarr(arr) # This code is contributed by Taranpreet
C#
// C# program for the above approach using System; class GFG { // Function to return the longest // consecutive subarray static void findLongestConseqSubarr(int[] arr) { int ans = 0, count = 0, start = 0, end = 0, x = 0, y = 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++; end = i; } // Reset the count else { start = i; count = 1; } // Update the maximum if (ans < count) { ans = count; x = start; y = end; } } for (int i = x; i <= y; i++) Console.Write(arr[i] + ", "); } // Driver code public static void Main() { int[] arr = { 1, 9, 3, 4, 20, 2 }; findLongestConseqSubarr(arr); } } // This code is contributed by Saurabh Jaiswal
Javascript
<script> // Javascript program for the above approach // Function to return the longest // consecutive subarray function findLongestConseqSubarr(v) { let ans = 0, count = 0, start = 0, end = 0, x, y; // 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++; end = i; } // Reset the count else { start = i; count = 1; } // Update the maximum if (ans < count) { ans = count; x = start; y = end; } } for (let i = x; i <= y; i++) document.write(v[i] + ", "); } // Driver Code let arr = [ 1, 9, 3, 4, 20, 2 ] findLongestConseqSubarr(arr); // This code is contributed by gfgking. </script>
3, 4,
Complejidad temporal: O(N)
Espacio auxiliar: O(1)