Find the Longest Increasing Subsequence in Circular manner
Given an array, the task is to find LIS (Longest Increasing Subsequence) in a circular way.
Examples :
Input : arr[] = {5, 4, 3, 2, 1}
Output : 2
Although there is no LIS in a given array
but in a circular form there can be
{1, 5}, {2, 5}, ......
Input : arr[]= {5, 6, 7, 1, 2, 3}
Output : 6
{1, 2, 3, 5, 6, 7} will be the LIS in the
circular manner.
- Append the same elements(i.e. whole array) with the given array.
- For every window of size n(no. of elements in the given array), perform LIS.
- Return maximum length.
For example : Given array is {1, 4, 6, 2, 3}
After appending elements resultant array
will be {1, 4, 6, 2, 3, 1, 4, 6, 2, 3}.
Now for every consecutive n elements perform LIS.
1- {1, 4, 6, 2, 3} --3 is length of LIS.
2- {4, 6, 2, 3, 1} --2 is length of LIS.
3- {6, 2, 3, 1, 4} --3
4- {2, 3, 1, 4, 6}-- 4 {2, 3, 4, 6}
5- {3, 1, 4, 6, 2} --3.
6- {1, 4, 6, 2, 3} Original list.
So, maximum length of LIS in circular manner is 4.
As in the last window we will have the same elements as in the given array which we don’t need to compute again, so we can append only n-1 elements to reduce the number of operations.
C++
#include <bits/stdc++.h>
using namespace std;
int computeLIS( int circBuff[], int start, int end, int n)
{
int LIS[end - start];
for ( int i = start; i < end; i++)
LIS[i] = 1;
for ( int i = start + 1; i < end; i++)
for ( int j = start; j < i; j++)
if (circBuff[i] > circBuff[j]
&& LIS[i] < LIS[j] + 1)
LIS[i] = LIS[j] + 1;
int res = INT_MIN;
for ( int i = start; i < end; i++)
res = max(res, LIS[i]);
return res;
}
int LICS( int arr[], int n)
{
int circBuff[2 * n];
for ( int i = 0; i < n; i++)
circBuff[i] = arr[i];
for ( int i = n; i < 2 * n; i++)
circBuff[i] = arr[i - n];
int res = INT_MIN;
for ( int i = 0; i < n; i++)
res = max(computeLIS(circBuff, i, i + n, n), res);
return res;
}
int main()
{
int arr[] = { 1, 4, 6, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Length of LICS is " << LICS(arr, n);
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
int max( int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
int computeLIS( int circBuff[], int start, int end, int n)
{
int LIS[end - start];
for ( int i = start; i < end; i++)
LIS[i] = 1;
for ( int i = start + 1; i < end; i++)
for ( int j = start; j < i; j++)
if (circBuff[i] > circBuff[j]
&& LIS[i] < LIS[j] + 1)
LIS[i] = LIS[j] + 1;
int res = INT_MIN;
for ( int i = start; i < end; i++)
res = max(res, LIS[i]);
return res;
}
int LICS( int arr[], int n)
{
int circBuff[2 * n];
for ( int i = 0; i < n; i++)
circBuff[i] = arr[i];
for ( int i = n; i < 2 * n; i++)
circBuff[i] = arr[i - n];
int res = INT_MIN;
for ( int i = 0; i < n; i++)
res = max(computeLIS(circBuff, i, i + n, n), res);
return res;
}
int main()
{
int arr[] = { 1, 4, 6, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Length of LICS is %d " , LICS(arr, n));
return 0;
}
|
Java
class Test
{
static int computeLIS( int circBuff[], int start, int end, int n)
{
int LIS[] = new int [n+end-start];
for ( int i = start; i < end; i++)
LIS[i] = 1 ;
for ( int i = start + 1 ; i < end; i++)
for ( int j = start; j < i; j++ )
if (circBuff[i] > circBuff[j] && LIS[i] < LIS[j] + 1 )
LIS[i] = LIS[j] + 1 ;
int res = Integer.MIN_VALUE;
for ( int i = start; i < end; i++)
res = Math.max(res, LIS[i]);
return res;
}
static int LICS( int arr[], int n)
{
int circBuff[] = new int [ 2 * n];
for ( int i = 0 ; i<n; i++)
circBuff[i] = arr[i];
for ( int i = n; i < 2 *n; i++)
circBuff[i] = arr[i-n];
int res = Integer.MIN_VALUE;
for ( int i= 0 ; i<n; i++)
res = Math.max(computeLIS(circBuff, i, i + n, n), res);
return res;
}
public static void main(String args[])
{
int arr[] = { 1 , 4 , 6 , 2 , 3 };
System.out.println( "Length of LICS is " + LICS( arr, arr.length));
}
}
|
Python3
def computeLIS(circBuff, start, end, n):
LIS = [ 0 for i in range (end)]
for i in range (start, end):
LIS[i] = 1
for i in range (start + 1 , end):
for j in range (start,i):
if (circBuff[i] > circBuff[j] and
LIS[i] < LIS[j] + 1 ):
LIS[i] = LIS[j] + 1
res = - 100000
for i in range (start, end):
res = max (res, LIS[i])
return res
def LICS(arr, n):
circBuff = [ 0 for i in range ( 2 * n)]
for i in range (n):
circBuff[i] = arr[i]
for i in range (n, 2 * n):
circBuff[i] = arr[i - n]
res = - 100000
for i in range (n):
res = max (computeLIS(circBuff, i,
i + n, n), res)
return res
arr = [ 1 , 4 , 6 , 2 , 3 ]
n = len (arr)
print ( "Length of LICS is" , LICS(arr, n))
|
C#
using System;
class Test
{
static int computeLIS( int []circBuff, int start,
int end, int n)
{
int []LIS = new int [n+end-start];
for ( int i = start; i < end; i++)
LIS[i] = 1;
for ( int i = start + 1; i < end; i++)
for ( int j = start; j < i; j++ )
if (circBuff[i] > circBuff[j] &&
LIS[i] < LIS[j] + 1)
LIS[i] = LIS[j] + 1;
int res = int .MinValue;
for ( int i = start; i < end; i++)
res = Math.Max(res, LIS[i]);
return res;
}
static int LICS( int []arr, int n)
{
int []circBuff = new int [2 * n];
for ( int i = 0; i<n; i++)
circBuff[i] = arr[i];
for ( int i = n; i < 2*n; i++)
circBuff[i] = arr[i-n];
int res = int .MinValue;
for ( int i=0; i<n; i++)
res = Math.Max(computeLIS(circBuff, i, i + n, n), res);
return res;
}
public static void Main()
{
int []arr = {1, 4, 6, 2, 3};
Console.Write( "Length of LICS is " +
LICS( arr, arr.Length));
}
}
|
PHP
<?php
function computeLIS( $circBuff ,
$start ,
$end , $n )
{
$LIS = Array();
for ( $i = $start ; $i < $end ; $i ++)
$LIS [ $i ] = 1;
for ( $i = $start + 1; $i < $end ; $i ++)
for ( $j = $start ; $j < $i ; $j ++ )
if ( $circBuff [ $i ] > $circBuff [ $j ] &&
$LIS [ $i ] < $LIS [ $j ] + 1)
$LIS [ $i ] = $LIS [ $j ] + 1;
$res = PHP_INT_MIN;
for ( $i = $start ; $i < $end ; $i ++)
$res = max( $res , $LIS [ $i ]);
return $res ;
}
function LICS( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
$circBuff [ $i ] = $arr [ $i ];
for ( $i = $n ; $i < 2 * $n ; $i ++)
$circBuff [ $i ] = $arr [ $i - $n ];
$res = PHP_INT_MIN;
for ( $i = 0; $i < $n ; $i ++)
$res = max(computeLIS( $circBuff , $i ,
$i + $n , $n ),
$res );
return $res ;
}
$arr = array (1, 4, 6, 2, 3);
$n = sizeof( $arr );
echo "Length of LICS is " ,
LICS( $arr , $n );
?>
|
Javascript
<script>
function computeLIS(circBuff, start, end, n)
{
let LIS = new Array(n+end-start);
for (let i = start; i < end; i++)
LIS[i] = 1;
for (let i = start + 1; i < end; i++)
for (let j = start; j < i; j++ )
if (circBuff[i] > circBuff[j] && LIS[i] < LIS[j] + 1)
LIS[i] = LIS[j] + 1;
let res = Number.MIN_VALUE;
for (let i = start; i < end; i++)
res = Math.max(res, LIS[i]);
return res;
}
function LICS(arr, n)
{
let circBuff = new Array(2 * n);
for (let i = 0; i<n; i++)
circBuff[i] = arr[i];
for (let i = n; i < 2*n; i++)
circBuff[i] = arr[i-n];
let res = Number.MIN_VALUE;
for (let i=0; i<n; i++)
res = Math.max(computeLIS(circBuff, i, i + n, n), res);
return res;
}
let arr = [1, 4, 6, 2, 3];
document.write( "Length of LICS is " + LICS( arr, arr.length));
</script>
|
Output :
Length of LICS is 4
Time Complexity: O(n3). It can be reduced O(n2 Log n) using O(n Log n) algorithm to find LIS.
Auxiliary Space: O(n)
Reference :
https://www.careercup.com/question?id=5942735794077696
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...