You are a Quality Assurance Engineer at a cutting-edge pharmaceutical company, "Zoblik International". Your team is developing a new class of compounds, and a critical stability check involves analyzing the 'reactivity indices' of components within a batch. For a batch to be considered stable and proceed to further testing, there must exist at least two distinct components whose reactivity indices sum up to exactly zero. These indices can be positive, negative, or zero.
You are given a list of reactivity indices for a batch of components. This list is always provided in sorted non-decreasing order. Your task is to quickly determine if any pair of components within this sorted list has reactivity indices that perfectly neutralize each other (i.e., their sum is zero).
If such a pair exists, report "true". Otherwise, report "false".
The first line contains an integer 'T' denoting the number of test cases.
For each test case:
For each test case, output "true" if a neutralizing pair exists, and "false" otherwise. Each output should be on a new line.
1 <= T <= 1002 <= n <= 104-107 <= Ai <= 1071 <= T <= 100; 2 <= n <= 10^4; -10^7<= Ai<= 10^7
Sample Input 1:
2 4 -3 1 3 4 4 -2 1 3 4Expected Output 1:
true falseExplanation for Sample Input 1:
Test Case 1:
Input array:
[-3, 1, 3, 4]We are looking for two numbers that sum to zero.
- Start with pointers at the beginning (
-3) and end (4). Sum =-3 + 4 = 1. Since1 > 0, we need a smaller sum. Move the right pointer left.- Pointers are now at
-3and3. Sum =-3 + 3 = 0.- Since the sum is
0, a neutralizing pair is found! The output is "true".Test Case 2:
Input array:
[-2, 1, 3, 4]We are looking for two numbers that sum to zero.
- Start with pointers at the beginning (
-2) and end (4). Sum =-2 + 4 = 2. Since2 > 0, move the right pointer left.- Pointers are now at
-2and3. Sum =-2 + 3 = 1. Since1 > 0, move the right pointer left.- Pointers are now at
-2and1. Sum =-2 + 1 = -1. Since-1 < 0, we need a larger sum. Move the left pointer right.- The left pointer (at
-2) would move to1. Now left and right pointers would both be at index 1 (value1). Sinceleft < rightcondition fails, the loop terminates.- No pair was found that sums to zero. The output is "false".
Two Pointers
Start coding and your submissions will appear here.