Length of longest increasing circular subarray

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:

  1. If n == 1, return 1.
  2. Find length of longest increasing subarray starting with first element of arr[]. Let its length be startLen.
  3. Starting from the next element where the first increasing subarray ends, find the length of the longest increasing subarray. Let it be max.
  4. Consider the length of the increasing subarray that ends with the last element of arr[]. Let it be endLen.
  5. If arr[n-1] < arr[0], then endLen = endLen + startLen.
  6. Finally, return maximum of (max, endLen, startLen).

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find length of longest
// increasing circular subarry
#include <bits/stdc++.h>
using namespace std;
  
// function to find length of longest
// increasing circular subarry
int longlenCircularSubarr(int arr[], int n)
{
    // if there is only one element
    if (n == 1)
        return 1;
  
    // 'startLen' stores the length of the longest
    // increasing subarray which starts from
    // first element
    int startLen = 1, i;
    int len = 1, max = 0;
  
    // finding the length of the longest
    // increasing subarray starting from
    // first element
    for (i = 1; i < n; i++) {
        if (arr[i - 1] < arr[i])
            startLen++;
        else
            break;
    }
  
    if (max < startLen)
        max = startLen;
  
    // traverse the array index (i+1)
    for (int j = i + 1; j < n; j++) {
  
        // if current element if greater than previous
        // element, then this element helps in building
        // up the previous increasing subarray encountered
        // so far
        if (arr[j - 1] < arr[j])
            len++;
        else {
  
            // check if 'max' length is less than the length
            // of the current increasing subarray. If true,
            // then update 'max'
            if (max < len)
                max = len;
  
            // reset 'len' to 1 as from this element
            // again the length of the new increasing
            // subarray is being calculated
            len = 1;
        }
    }
  
    // if true, then add length of the increasing
    // subarray ending at last element with the
    // length of the increasing subarray starting
    // from first element - This is done for
    // circular rotation
    if (arr[n - 1] < arr[0])
        len += startLen;
  
    // check if 'max' length is less than the length
    // of the current increasing subarray. If true,
    // then update 'max'
    if (max < len)
        max = len;
  
    return max;
}
  
// Driver program to test above
int main()
{
    int arr[] = { 2, 3, 4, 5, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Length = "
         << longlenCircularSubarr(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find length
// of longest increasing circular subarry
  
class Circular
{
    // function to find length of longest
    // increasing circular subarry
    public static int longlenCircularSubarr(int arr[], 
                                               int n)
    {
        // if there is only one element
        if (n == 1)
            return 1;
  
        // 'startLen' stores the length of the 
        // longest increasing subarray which 
        // starts from first element
        int startLen = 1, i;
        int len = 1, max = 0;
  
        // finding the length of the longest
        // increasing subarray starting from
        // first element
        for (i = 1; i < n; i++) {
            if (arr[i - 1] < arr[i])
                startLen++;
            else
                break;
        }
  
        if (max < startLen)
            max = startLen;
  
        // traverse the array index (i+1)
        for (int j = i + 1; j < n; j++) {
  
            // if current element if greater than
            // previous element, then this element 
            // helps in building up the previous 
            // increasing subarray encountered so far
            if (arr[j - 1] < arr[j])
                len++;
            else {
  
                // check if 'max' length is less than 
                // the length of the current increasing 
                // subarray. If true, then update 'max'
                if (max < len)
                    max = len;
      
                // reset 'len' to 1 as from this element
                // again the length of the new increasing
                // subarray is being calculated
                len = 1;
            }
        }
  
        // if true, then add length of the increasing
        // subarray ending at last element with the
        // length of the increasing subarray starting
        // from first element - This is done for
        // circular rotation
        if (arr[n - 1] < arr[0])
            len += startLen;
  
        // check if 'max' length is less than the 
        // length of the current increasing subarray. 
        // If true, then update 'max'
        if (max < len)
            max = len;
  
        return max;
    }
      
    // driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 4, 5, 1 };
        int n = 5;
        System.out.print("Length = "
                      longlenCircularSubarr(arr, n));
    }
}
  
// This code is contributed by rishabh_jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find length
# of longest increasing circular subarry
  
# function to find length of longest
# increasing circular subarry
def longlenCircularSubarr (arr, n):
      
    # if there is only one element
    if n == 1:
        return 1
      
    # 'startLen' stores the length of the 
    # longest increasing subarray which
    # starts from first element
    startLen = 1
      
    len = 1
    max = 0
      
    # finding the length of the longest
    # increasing subarray starting from
    # first element
    for i in range(1, n):
        if arr[i - 1] < arr[i]:
            startLen+=1
        else:
            break
      
    if max < startLen:
        max = startLen
          
    # traverse the array index (i+1)
    for j in range(i + 1, n):
          
        # if current element if greater than
        # previous element, then this element 
        # helps in building up the previous 
        # increasing subarray encountered 
        # so far
        if arr[j - 1] < arr[j]:
            len+=1
        else:
            # check if 'max' length is less 
            # than the length of the current
            # increasing subarray. If true,
            # then update 'max'
            if max < len:
                max = len
              
            # reset 'len' to 1 as from this 
            # element again the length of the
            # new increasing subarray is 
            # being calculated
            len = 1
              
    # if true, then add length of the increasing
    # subarray ending at last element with the
    # length of the increasing subarray starting
    # from first element - This is done for
    # circular rotation
    if arr[n - 1] < arr[0]:
        len += startLen
      
    # check if 'max' length is less than the
    # length of the current increasing subarray.
    # If true, then update 'max'
    if max < len:
        max = len
      
    return max
  
# Driver code to test above
arr = [ 2, 3, 4, 5, 1 ]
n = len(arr)
print("Length = ",longlenCircularSubarr(arr, n))
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find length
// of longest increasing circular subarry
using System;
  
public class GFG {
  
    // function to find length of longest
    // increasing circular subarry
    static int longlenCircularSubarr(int[] arr, int n)
    {
        // if there is only one element
        if (n == 1)
            return 1;
  
        // 'startLen' stores the length of the longest
        // increasing subarray which starts from
        // first element
        int startLen = 1, i;
        int len = 1, max = 0;
  
        // finding the length of the longest
        // increasing subarray starting from
        // first element
        for (i = 1; i < n; i++) {
            if (arr[i - 1] < arr[i])
                startLen++;
            else
                break;
        }
  
        if (max < startLen)
            max = startLen;
  
        // traverse the array index (i+1)
        for (int j = i + 1; j < n; j++) {
  
            // if current element if greater than previous
            // element, then this element helps in building
            // up the previous increasing subarray encountered
            // so far
            if (arr[j - 1] < arr[j])
                len++;
            else {
  
                // check if 'max' length is less than the length
                // of the current increasing subarray. If true,
                // then update 'max'
                if (max < len)
                    max = len;
  
                // reset 'len' to 1 as from this element
                // again the length of the new increasing
                // subarray is being calculated
                len = 1;
            }
        }
  
        // if true, then add length of the increasing
        // subarray ending at last element with the
        // length of the increasing subarray starting
        // from first element - This is done for
        // circular rotation
        if (arr[n - 1] < arr[0])
            len += startLen;
  
        // check if 'max' length is less than the length
        // of the current increasing subarray. If true,
        // then update 'max'
        if (max < len)
            max = len;
  
        return max;
    }
  
    // Driver program to test above
    static public void Main()
    {
        int[] arr = { 2, 3, 4, 5, 1 };
        int n = arr.Length;
        Console.WriteLine("Length = " +
                           longlenCircularSubarr(arr, n));
        // Code
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP implementation to find length of longest
// increasing circular subarry
  
// function to find length of longest
// increasing circular subarry
function longlenCircularSubarr(&$arr, $n)
{
    // if there is only one element
    if ($n == 1)
        return 1;
  
    // 'startLen' stores the length of the longest
    // increasing subarray which starts from
    // first element
    $startLen = 1;
    $len = 1;
    $max = 0;
  
    // finding the length of the longest
    // increasing subarray starting from
    // first element
    for ($i = 1; $i < $n; $i++) 
    {
        if ($arr[$i - 1] < $arr[$i])
            $startLen++;
        else
            break;
    }
  
    if ($max < $startLen)
        $max = $startLen;
  
    // traverse the array index (i+1)
    for ($j = $i + 1; $j < $n; $j++) 
    {
  
        // if current element if greater than 
        // previous element, then this element 
        // helps in building up the previous 
        // increasing subarray encountered
        // so far
        if ($arr[$j - 1] < $arr[$j])
            $len++;
        else 
        {
  
            // check if 'max' length is less than 
            // the length of the current increasing 
            // subarray. If true, then update 'max'
            if ($max < $len)
                $max = $len;
  
            // reset 'len' to 1 as from this element
            // again the length of the new increasing
            // subarray is being calculated
            $len = 1;
        }
    }
  
    // if true, then add length of the increasing
    // subarray ending at last element with the
    // length of the increasing subarray starting
    // from first element - This is done for
    // circular rotation
    if ($arr[$n - 1] < $arr[0])
        $len += $startLen;
  
    // check if 'max' length is less than the length
    // of the current increasing subarray. If true,
    // then update 'max'
    if ($max < $len)
        $max = $len;
  
    return $max;
}
  
// Driver Code
$arr = array( 2, 3, 4, 5, 1 );
$n = sizeof($arr);
echo "Length = " . longlenCircularSubarr($arr, $n);
  
// This code is contributed by ita_c
?>

chevron_right



Output:

Length = 5

Time Complexity: O(n).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : chitranayal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.