


Consider ‘arr’ = [1,2,-2,-1], the largest value of ‘K’ is 2, since a negative of 2 is also present in the array. 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 an integer ‘N’, denoting the number of elements in the array.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
For each test case, print the largest number, ‘K’, such that -’K’ is also present in the array.
The output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^6
1 <= arr[i] <= 10^9
Time limit: 1 sec
The simplest approach to solve the given problem is to iterate over the arrays and for each element, traverse the remaining array to check if its negative value exists in the array or not. After complete traversal of the array, print the maximum such number obtained.
Start traversing the array from index 0
The above approach can be modified using the sorting and 2-pointer approach.
We can sort the array, and then take a left pointer initialized to 0, and a right pointer initialized to the index of the last element.
Then we can do the following operations:
We will use a hashmap in this approach. We will store each number in the hashmap as we iterate through the array. At each step, we check whether the negative of the current element is present in the array.
Algorithm :