Print the longest increasing consecutive subarray
Last Updated :
31 Jan, 2022
Given an array arr[] of size N, the task is to print the longest increasing subarray such that elements in the subarray are consecutive integers.
Examples:
Input: arr[] = {1, 9, 3, 4, 20, 2}
Output: {3, 4}
Explanation: The subarray {3, 4} is the longest subarray of consecutive elements
Input: arr[] = {36, 41, 56, 32, 33, 34, 35, 43, 32, 42}
Output: {32, 33, 34, 35}
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, and update the window’s start and endpoints
- Else increase the count
- Finally, print the elements of the window
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findLongestConseqSubarr(vector< int >& v)
{
int ans = 0, count = 0,
start = 0, end = 0, x, y;
for ( int i = 0; i < v.size(); i++) {
if (i > 0 && v[i] == v[i - 1] + 1) {
count++;
end = i;
}
else {
start = i;
count = 1;
}
if (ans < count) {
ans = count;
x = start;
y = end;
}
}
for ( int i = x; i <= y; i++)
cout << v[i] << ", " ;
}
int main()
{
vector< int > arr = { 1, 9, 3, 4, 20, 2 };
findLongestConseqSubarr(arr);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void findLongestConseqSubarr( int arr[ ])
{
int ans = 0 , count = 0 , start = 0 , end = 0 , x = 0 , y = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (i > 0 && arr[i] == arr[i - 1 ] + 1 ) {
count++;
end = i;
}
else {
start = i;
count = 1 ;
}
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);
}
}
|
Python3
def findLongestConseqSubarr(v):
ans = 0
count = 0
start = 0
end = 0
for i in range ( 0 , len (v)):
if (i > 0 and v[i] = = v[i - 1 ] + 1 ):
count = count + 1
end = i
else :
start = i
count = 1
if (ans < count):
ans = count
x = start
y = end
for i in range (x, y + 1 ):
print (v[i], end = ", " )
arr = [ 1 , 9 , 3 , 4 , 20 , 2 ]
findLongestConseqSubarr(arr)
|
C#
using System;
class GFG
{
static void findLongestConseqSubarr( int [] arr)
{
int ans = 0, count = 0, start = 0, end = 0, x = 0, y = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (i > 0 && arr[i] == arr[i - 1] + 1)
{
count++;
end = i;
}
else
{
start = i;
count = 1;
}
if (ans < count)
{
ans = count;
x = start;
y = end;
}
}
for ( int i = x; i <= y; i++)
Console.Write(arr[i] + ", " );
}
public static void Main()
{
int [] arr = { 1, 9, 3, 4, 20, 2 };
findLongestConseqSubarr(arr);
}
}
|
Javascript
<script>
function findLongestConseqSubarr(v)
{
let ans = 0, count = 0, start = 0, end = 0, x, y;
for (let i = 0; i < v.length; i++) {
if (i > 0 && v[i] == v[i - 1] + 1) {
count++;
end = i;
}
else {
start = i;
count = 1;
}
if (ans < count) {
ans = count;
x = start;
y = end;
}
}
for (let i = x; i <= y; i++)
document.write(v[i] + ", " );
}
let arr = [ 1, 9, 3, 4, 20, 2 ]
findLongestConseqSubarr(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...