Length of longest subarray with increasing contiguous elements
Last Updated :
29 Dec, 2023
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 the 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++
#include <bits/stdc++.h>
using namespace std;
int maxiConsecutiveSubarray( int arr[], int N)
{
int maxi = 0;
for ( int i = 0; i < N - 1; i++) {
int cnt = 1, j;
for (j = i; j < N; j++) {
if (arr[j + 1] == arr[j] + 1) {
cnt++;
}
else {
break ;
}
}
maxi = max(maxi, cnt);
i = j;
}
return maxi;
}
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
import java.util.*;
class GFG{
public static int maxiConsecutiveSubarray( int arr[],
int N)
{
int maxi = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
int cnt = 1 , j;
for (j = i; j < N - 1 ; j++)
{
if (arr[j + 1 ] == arr[j] + 1 )
{
cnt++;
}
else
{
break ;
}
}
maxi = Math.max(maxi, cnt);
i = j;
}
return maxi;
}
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));
}
}
|
Python3
def maxiConsecutiveSubarray(arr, N):
maxi = 0 ;
for i in range (N - 1 ):
cnt = 1 ;
for j in range (i, N - 1 ):
if (arr[j + 1 ] = = arr[j] + 1 ):
cnt + = 1 ;
else :
break ;
maxi = max (maxi, cnt);
i = j;
return maxi;
if __name__ = = '__main__' :
N = 11 ;
arr = [ 1 , 3 , 4 , 2 , 3 ,
4 , 2 , 3 , 5 , 6 , 7 ];
print (maxiConsecutiveSubarray(arr, N));
|
C#
using System;
class GFG{
public static int maxiConsecutiveSubarray( int []arr,
int N)
{
int maxi = 0;
for ( int i = 0; i < N - 1; i++)
{
int cnt = 1, j;
for (j = i; j < N - 1; j++)
{
if (arr[j + 1] == arr[j] + 1)
{
cnt++;
}
else
{
break ;
}
}
maxi = Math.Max(maxi, cnt);
i = j;
}
return maxi;
}
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));
}
}
|
Javascript
<script>
function maxiConsecutiveSubarray(arr, N)
{
let maxi = 0;
for (let i = 0; i < N - 1; i++)
{
let cnt = 1, j;
for (j = i; j < N - 1; j++)
{
if (arr[j + 1] == arr[j] + 1)
{
cnt++;
}
else
{
break ;
}
}
maxi = Math.max(maxi, cnt);
i = j;
}
return maxi;
}
let N = 11;
let arr = [ 1, 3, 4, 2, 3, 4,
2, 3, 5, 6, 7 ];
document.write(maxiConsecutiveSubarray(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...