Length of longest increasing circular subarray
Last Updated :
27 Jul, 2022
Given an array containing n numbers. The problem is to find the length of the longest contiguous subarray in a circular manner such that every element in the subarray is strictly greater than its previous element in the same subarray. Time Complexity should be O(n).
Examples:
Input : arr[] = {2, 3, 4, 5, 1}
Output : 5
{2, 3, 4, 5, 1} is the subarray if we circularly
start from the last element and then take the
first four elements. This will give us an increasing
subarray {1, 2, 3, 4, 5} in a circular manner.
Input : arr[] = {2, 3, 8, 4, 6, 7, 10, 12, 9, 1}
Output : 5
Method 1 (Using extra space): Make a temp[] array of size 2*n. Copy the elements of arr[] in temp[] two times. Now, find length of Longest increasing subarray in temp[].
Method 2 (Without using extra space):
Following are the steps:
- If n == 1, return 1.
- Find length of longest increasing subarray starting with first element of arr[]. Let its length be startLen.
- Starting from the next element where the first increasing subarray ends, find the length of the longest increasing subarray. Let it be max.
- Consider the length of the increasing subarray that ends with the last element of arr[]. Let it be endLen.
- If arr[n-1] < arr[0], then endLen = endLen + startLen.
- Finally, return maximum of (max, endLen, startLen).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int longlenCircularSubarr( int arr[], int n)
{
if (n == 1)
return 1;
int startLen = 1, i;
int len = 1, max = 0;
for (i = 1; i < n; i++) {
if (arr[i - 1] < arr[i])
startLen++;
else
break ;
}
if (max < startLen)
max = startLen;
for ( int j = i + 1; j < n; j++) {
if (arr[j - 1] < arr[j])
len++;
else {
if (max < len)
max = len;
len = 1;
}
}
if (arr[n - 1] < arr[0])
len += startLen;
if (max < len)
max = len;
return max;
}
int main()
{
int arr[] = { 2, 3, 4, 5, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Length = "
<< longlenCircularSubarr(arr, n);
return 0;
}
|
Java
class Circular
{
public static int longlenCircularSubarr( int arr[],
int n)
{
if (n == 1 )
return 1 ;
int startLen = 1 , i;
int len = 1 , max = 0 ;
for (i = 1 ; i < n; i++) {
if (arr[i - 1 ] < arr[i])
startLen++;
else
break ;
}
if (max < startLen)
max = startLen;
for ( int j = i + 1 ; j < n; j++) {
if (arr[j - 1 ] < arr[j])
len++;
else {
if (max < len)
max = len;
len = 1 ;
}
}
if (arr[n - 1 ] < arr[ 0 ])
len += startLen;
if (max < len)
max = len;
return max;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 5 , 1 };
int n = 5 ;
System.out.print( "Length = " +
longlenCircularSubarr(arr, n));
}
}
|
Python3
def longlenCircularSubarr (arr, n):
if n = = 1 :
return 1
startLen = 1
len = 1
max = 0
for i in range ( 1 , n):
if arr[i - 1 ] < arr[i]:
startLen + = 1
else :
break
if max < startLen:
max = startLen
for j in range (i + 1 , n):
if arr[j - 1 ] < arr[j]:
len + = 1
else :
if max < len :
max = len
len = 1
if arr[n - 1 ] < arr[ 0 ]:
len + = startLen
if max < len :
max = len
return max
arr = [ 2 , 3 , 4 , 5 , 1 ]
n = len (arr)
print ( "Length = " ,longlenCircularSubarr(arr, n))
|
C#
using System;
public class GFG {
static int longlenCircularSubarr( int [] arr, int n)
{
if (n == 1)
return 1;
int startLen = 1, i;
int len = 1, max = 0;
for (i = 1; i < n; i++) {
if (arr[i - 1] < arr[i])
startLen++;
else
break ;
}
if (max < startLen)
max = startLen;
for ( int j = i + 1; j < n; j++) {
if (arr[j - 1] < arr[j])
len++;
else {
if (max < len)
max = len;
len = 1;
}
}
if (arr[n - 1] < arr[0])
len += startLen;
if (max < len)
max = len;
return max;
}
static public void Main()
{
int [] arr = { 2, 3, 4, 5, 1 };
int n = arr.Length;
Console.WriteLine( "Length = " +
longlenCircularSubarr(arr, n));
}
}
|
PHP
<?php
function longlenCircularSubarr(& $arr , $n )
{
if ( $n == 1)
return 1;
$startLen = 1;
$len = 1;
$max = 0;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $arr [ $i - 1] < $arr [ $i ])
$startLen ++;
else
break ;
}
if ( $max < $startLen )
$max = $startLen ;
for ( $j = $i + 1; $j < $n ; $j ++)
{
if ( $arr [ $j - 1] < $arr [ $j ])
$len ++;
else
{
if ( $max < $len )
$max = $len ;
$len = 1;
}
}
if ( $arr [ $n - 1] < $arr [0])
$len += $startLen ;
if ( $max < $len )
$max = $len ;
return $max ;
}
$arr = array ( 2, 3, 4, 5, 1 );
$n = sizeof( $arr );
echo "Length = " . longlenCircularSubarr( $arr , $n );
?>
|
Javascript
<script>
function longlenCircularSubarr(arr,n)
{
if (n == 1)
return 1;
let startLen = 1, i;
let len = 1, max = 0;
for (i = 1; i < n; i++) {
if (arr[i - 1] < arr[i])
startLen++;
else
break ;
}
if (max < startLen)
max = startLen;
for (let j = i + 1; j < n; j++) {
if (arr[j - 1] < arr[j])
len++;
else {
if (max < len)
max = len;
len = 1;
}
}
if (arr[n - 1] < arr[0])
len += startLen;
if (max < len)
max = len;
return max;
}
let arr=[2, 3, 4, 5, 1 ];
let n = 5;
document.write( "Length = " +
longlenCircularSubarr(arr, n));
</script>
|
Time Complexity: O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...