#C4017. Final Array Length after Consecutive Equal Pairs Removal
Final Array Length after Consecutive Equal Pairs Removal
Final Array Length after Consecutive Equal Pairs Removal
You are given an array of integers for each test case. Your task is to repeatedly remove any adjacent pair of equal elements from the array until no such pair exists. The final array may sometimes be empty. You are required to determine the length of the array after all possible removals.
Process:
- For each test case, first read an integer n which represents the number of elements in the array.
- Then, read n integers which are the elements of the array.
- Using a stack or similar technique, remove adjacent equal pairs iteratively.
- Output the final length of the reduced array.
Example:
Consider the array: [3, 3, 2, 2, 1, 1].
After removing 3-3, 2-2, and 1-1, the resulting array is empty and its length is 0.
Your solution should read input from standard input and print results to standard output.
inputFormat
The input begins with a single integer T
indicating the number of test cases. For each test case:
- The first line contains an integer
n
— the number of elements in the array. - The second line contains
n
space-separated integers representing the array.
outputFormat
For each test case, output a single line with the final length of the array after all possible adjacent equal pairs have been removed.
## sample4
6
3 3 2 2 1 1
5
1 2 2 3 3
3
1 1 2
4
2 2 2 2
0
1
1
0
</p>