You are given an array “arr” of length ‘n’. You have to find the length of the longest increasing subsequence of this array in a circular manner, i.e., if you start from the ith position in the array, then your array will be ‘i’ to n - 1 then 0 to i - 1.
An array ‘a’ is a subsequence of an array ‘b’, if ‘a’ can be obtained from ‘b’ by deleting several (possibly, zero or all) elements from the array.
For Example :arr = {10, 7, 3}
In this example, the longest increasing subsequence can be {3, 10}, {7, 10} or {3, 7}. Hence the answer is 2.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the total number of elements in the array.
The next line contains ‘n’ integers denoting the elements of the array.
Output Format :
For each test case, print a single integer “ans” denoting the length of the longest increasing subsequence.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 300
0 <= arr[i] <= 1000
Time limit: 1 sec
2
4
11 9 5 3
3
5 7 3
2
3
In the first test case, there are 3 LIS(Longest Increasing Subsequence) of length 2.
{3, 11}, {5, 11}, {9, 11}.
Hence, the answer is 2.
In the second test case, the longest increasing subsequence is 3, 5, 7.
Hence the answer is 3.
2
5
13 57 23 27 43
4
1 2 3 4
4
4
Calculate longest increasing subsequence for every element of the array using recursion.
In this approach, we will find the LCS for every element of the array and return the maximum possible LCS from it.
The steps are as follows:
O( N * 2 ^ N ), where N is the size of the array “arr”.
As we are iterating through the array and for each element checking every possible case.
Hence the time complexity is O( N * 2 ^ N ).
O( 1 )
No extra space is used.
Hence the space complexity is O(1).