Last Updated: 4 Aug, 2022

Longest Alternating Subarray

Easy

Problem statement

You are given a 0-indexed array of integers ‘NUMS’ of length ‘N’. For each index ‘i’ ( i >=0 & i < ‘N’ ) find the length of the longest subarray starting from ‘i’.

Alternating subarray is a subarray in which the adjacent numbers are of different signs.

Example:
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}.
Input Format :
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.
Output format :
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’.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
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

Approaches

01 Approach

In this approach, from each index, we can run a nested loop to find the longest alternating subarray.


 

The steps are as follows:-

Function longestAlternatingSubarray( [ int ] nums):

  1. Initialize an array 'ANS' of length 'N' with value 1.
  2. Run a loop from 'i' = 0...'N' - 1:
    • Initialize a variable 'curLen' with a value of 1.
    • Run a loop from 'j' = 'i' + 1... 'N' - 1:
      • If the signs of value of 'NUMS' at 'j' and 'j-1' are not the same then increment the value of 'curLen' by 1.
      • Else break out the loop.
    • Assign the value of 'curLen' to 'ANS' at index 'i'.
  3. Return the 'ANS' array.

02 Approach

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.


 

The steps are as follows:-

Function longestAlternatingSubarray( [ int ] nums):

  1. Initialize two variables 'curLen' and 'FLAG' with value 0.
  2. Initialize an array 'ANS' of length 'N' with value 1.
  3. Run a loop from 'i' = 'N' - 1...0:
    • If the value of 'NUMS' at 'i' is positive and the value of 'FLAG' is not equal to 1.
      • Increment the value of 'curLen' by 1.
      • Modify the value of 'FLAG' to 1.
    • Else if the value of 'NUMS' at 'i' is negative and the value of 'FLAG' is not equal to 0.
      • Increment the value of 'curLen' by 1.
      • Modify the value of 'FLAG' to 0.
    • Else
      • If the value of 'NUMS' at 'i' is positive then modify the value of  'FLAG' to 1 else modify it to '0'. Also, modify the value  'curLen' to 1.
    • Assign the value of 'curLen' to 'ANS' at 'i'.
  4. Return the 'ANS' array.