Dado un arreglo arr[] de longitud N , la tarea es encontrar la longitud del subarreglo más largo que consta de números consecutivos en orden creciente, del arreglo .
Ejemplos:
Entrada: array[] = {2, 3, 4, 6, 7, 8, 9, 10}
Salida: 5
Explicación: el subarreglo {6, 7, 8, 9, 10} es el subarreglo más largo que satisface las condiciones dadas. Por lo tanto, la salida requerida es 5.Entrada: arr[] = {4, 5, 1, 2, 3, 4, 9, 10, 11, 12}
Salida: 4
Enfoque ingenuo: el enfoque más simple para resolver el problema es recorrer la array y, para cada índice i , recorrer desde el sobreíndice y encontrar la longitud del subarreglo más largo que satisfaga la condición dada a partir de i . Cambie i al índice que no cumple la condición y verifique desde ese índice. Finalmente, imprima la longitud máxima de dicho subarreglo obtenido.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the longest subarray // with increasing contiguous elements int maxiConsecutiveSubarray(int arr[], int N) { // Stores the length of // required longest subarray int maxi = 0; for (int i = 0; i < N - 1; i++) { // Stores the length of length of longest // such subarray from ith index int cnt = 1, j; for (j = i; j < N; j++) { // If consecutive elements are // increasing and differ by 1 if (arr[j + 1] == arr[j] + 1) { cnt++; } // Otherwise else { break; } } // Update the longest subarray // obtained so far maxi = max(maxi, cnt); i = j; } // Return the length obtained return maxi; } // Driver Code int main() { int N = 11; int arr[] = { 1, 3, 4, 2, 3, 4, 2, 3, 5, 6, 7 }; cout << maxiConsecutiveSubarray(arr, N); return 0; }
Java
// Java implementation for the above approach import java.util.*; class GFG{ // Function to find the longest subarray // with increasing contiguous elements public static int maxiConsecutiveSubarray(int arr[], int N) { // Stores the length of // required longest subarray int maxi = 0; for(int i = 0; i < N - 1; i++) { // Stores the length of length of // longest such subarray from ith // index int cnt = 1, j; for(j = i; j < N - 1; j++) { // If consecutive elements are // increasing and differ by 1 if (arr[j + 1] == arr[j] + 1) { cnt++; } // Otherwise else { break; } } // Update the longest subarray // obtained so far maxi = Math.max(maxi, cnt); i = j; } // Return the length obtained return maxi; } // Driver Code public static void main(String args[]) { int N = 11; int arr[] = { 1, 3, 4, 2, 3, 4, 2, 3, 5, 6, 7 }; System.out.println(maxiConsecutiveSubarray(arr, N)); } } // This code is contributed by hemanth gadarla
Python3
# Python3 implementation for # the above approach # Function to find the longest # subarray with increasing # contiguous elements def maxiConsecutiveSubarray(arr, N): # Stores the length of # required longest subarray maxi = 0; for i in range(N - 1): # Stores the length of # length of longest such # subarray from ith index cnt = 1; for j in range(i, N - 1): # If consecutive elements are # increasing and differ by 1 if (arr[j + 1] == arr[j] + 1): cnt += 1; # Otherwise else: break; # Update the longest subarray # obtained so far maxi = max(maxi, cnt); i = j; # Return the length obtained return maxi; # Driver Code if __name__ == '__main__': N = 11; arr = [1, 3, 4, 2, 3, 4, 2, 3, 5, 6, 7]; print(maxiConsecutiveSubarray(arr, N)); # This code is contributed by Rajput-Ji
C#
// C# implementation for the // above approach using System; class GFG{ // Function to find the longest // subarray with increasing // contiguous elements public static int maxiConsecutiveSubarray(int []arr, int N) { // Stores the length of // required longest subarray int maxi = 0; for(int i = 0; i < N - 1; i++) { // Stores the length of // length of longest such // subarray from ith index int cnt = 1, j; for(j = i; j < N - 1; j++) { // If consecutive elements are // increasing and differ by 1 if (arr[j + 1] == arr[j] + 1) { cnt++; } // Otherwise else { break; } } // Update the longest subarray // obtained so far maxi = Math.Max(maxi, cnt); i = j; } // Return the length // obtained return maxi; } // Driver Code public static void Main(String []args) { int N = 11; int []arr = {1, 3, 4, 2, 3, 4, 2, 3, 5, 6, 7}; Console.WriteLine( maxiConsecutiveSubarray(arr, N)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript program to implement // the above approach // Function to find the longest subarray // with increasing contiguous elements function maxiConsecutiveSubarray(arr, N) { // Stores the length of // required longest subarray let maxi = 0; for(let i = 0; i < N - 1; i++) { // Stores the length of length of // longest such subarray from ith // index let cnt = 1, j; for(j = i; j < N - 1; j++) { // If consecutive elements are // increasing and differ by 1 if (arr[j + 1] == arr[j] + 1) { cnt++; } // Otherwise else { break; } } // Update the longest subarray // obtained so far maxi = Math.max(maxi, cnt); i = j; } // Return the length obtained return maxi; } // Driver Code let N = 11; let arr = [ 1, 3, 4, 2, 3, 4, 2, 3, 5, 6, 7 ]; document.write(maxiConsecutiveSubarray(arr, N)); </script>
3
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
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