Input: ‘N’ = 5, ‘NUMS’ = {-1, 2, 3, -5, 2}.
Output: {2, 1, 3, 2, 1}.
The longest alternating subarray starting from index 0 is -1, 2.
The longest alternating subarray starting from index 1 is 2.
The longest alternating subarray starting from index 2 is 3, -5 2.
The longest alternating subarray starting from index 3 is -5, 2.
The longest alternating subarray starting from index 4 is 2.
Hence, the final output array is {2, 1, 3, 2, 1}.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains an integer ‘N’ denoting the length of the array ‘NUMS.
The second line of each test case contains ‘N’ space-separated integers.
For each test case, you don’t need to print anything just return an array containing the longest alternating subarray starting from each index of the array ‘NUMS’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
-10^6 <= NUMS[ i ] <= 10^6
Sum of N Over all the Test cases <= 10^5
Time Limit: 1 sec
In this approach, from each index, we can run a nested loop to find the longest alternating subarray.
In this approach, We can iterate from the back of the array and then keep track of the alternating sequence. If at any index the alternating sequence gets broken we can start the sequence again with length = 1.