Given an array arr[] of N integers, the task is to find the length of the longest increasing subarray such that elements in the subarray are consecutive integers.
Examples:
Input: arr[] = {1, 9, 3, 4, 20, 2}
Output: 2
Explanation: The subarray {3, 4} is the longest subarray of consecutive elementsInput: arr[] = {36, 41, 56, 32, 33, 34, 35, 43, 32, 42}
Output: 4
Explanation: The subarray {32, 33, 34, 35} is the longest subarray of consecutive elements
Approach: The idea is to run a loop and keep a count and max (both initially zero). Follow the steps mentioned below:
- Run a loop from start to end.
- If the current element is not equal to the (previous element+1) then set the count to 1.
- Else increase the count.
- Update max with a maximum of count and max.
Below is the implementation of the above approach.
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> |
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!