Given an array arr[] of length N, the task is to find the length of the longest subarray which consists of consecutive numbers in increasing order, from the array.
Examples:
Input: arr[] = {2, 3, 4, 6, 7, 8, 9, 10}
Output: 5
Explanation: Subarray {6, 7, 8, 9, 10} is the longest subarray satisfying the given conditions. Therefore, the required output is 5.Input: arr[] = {4, 5, 1, 2, 3, 4, 9, 10, 11, 12}
Output: 4
Naive Approach: The simplest approach to solve the problem is to traverse the array and for every index i, traverse from over index and find the length of longest subarray satisfying the given condition starting from i. Shift i to the index which does not satisfy the condition and check from that index. Finally, print the maximum length of such subarray obtained.
Below is the implementation of the above approach:
C++
// C++ implementation for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the longest subarray// with increasing contiguous elementsint 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 Codeint 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 approachimport java.util.*;class GFG{ // Function to find the longest subarray// with increasing contiguous elementspublic 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 Codepublic 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 |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Largest sum contiguous increasing subarray
- Length of the largest subarray with contiguous elements | Set 1
- Length of longest increasing circular subarray
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Length of longest subarray of length at least 2 with maximum GCD
- Largest sum contiguous subarray having only non-negative elements
- Largest Sum Contiguous Subarray having unique elements
- Longest increasing subarray
- Longest increasing subsequence which forms a subarray in the sorted representation of the array
- Longest Increasing subarray with one change allowed
- Maximum length of subarray such that all elements are equal in the subarray
- Print all maximal increasing contiguous sub-array in an array
- Count the number of contiguous increasing and decreasing subsequences in a sequence
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Length of the longest Subarray with only Even Elements
- Length of Longest Subarray with same elements in atmost K increments
- Length of longest Subarray with equal number of odd and even elements
- Length of longest increasing index dividing subsequence
- Maximize length of longest increasing prime subsequence from the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : hemanthswarna1506

