You are given an integer array/list ‘ARR’ of size 'N' that is sorted in ascending order. Your task is to return '1' if and only if you can split it into one or more increasing subsequences such that each subsequence consists of consecutive integers and has a length of at least 3. Else, return '0'.
Note: An increasing sequence is a sequence where the difference between ith and i + 1th number is 1 exactly. For example : 1, 2, 3, 4 or 3, 4, 5 or -1, 0, 1 are all increasing subsequences.
For example:
Given ‘N’ = 4 and ‘ARR’[] = 1, 2, 3, 4.
The answer will be ‘1’ because an increasing subsequence of [1, 2, 3, 4] having length greater than 3 can be made.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer 'N', where ‘N’ is the number of elements of the array/list 'ARR'.
The second line of each test case contains ‘N’ single space-separated integers, denoting the 'ARR' elements.
Output format:
For each test case, print a single line containing '1' if you can split 'ARR' into one or more subsequences such that each subsequence consists of consecutive integers and has a length of at least 3. Else, print '0'.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 5000
-1250 <= ARR[ i ] <= 1250
Where ‘T’ is the total number of test cases, and 'N’ is the length of 'ARR' and ‘ARR[i]’ is the array element at index ‘i’.
Time limit: 1 second
2
4
1 2 3 4
3
1 2 2
1
0
For the first test case:
The answer will be 1 (true) because an increasing subsequence of [1, 2, 3, 4] can be made.
For the second test case:
The answer will be 0 (false) and this is because no valid increasing subsequence of length greater than 3 can be formed.
2
4
-1 0 1 2
6
-2 -1 0 7 8 9
1
1
Can we keep track of the next element we need in a subsequence?
The main idea is to maintain two hash-maps ‘frqMap’ and ‘seqMap’, which keep track of the frequency of each element in the array, and the following number that the sequence formed may need, respectively.
O(N), where ‘N’ is the length of the given array.
We have to array twice; therefore, the net time complexity will be O(N).
O(N), where ‘N’ is the length of the given array.
Since we are using any extra space to keep track of the elements, net space complexity will be O(N).