SalesforceOA #11
Find All Pairs of Integers
Estimated EasyJudge-ready
Topics (AI-labelled)Bits & MathIn-place Arrays
Given an integer array arr, find all pairs (x,y) with x=arr[i], y=arr[j], and i<j that satisfy both of the following conditions:
- min(|x−y|,|x+y|) ≤ min(|x|,|y|);
- max(|x−y|,|x+y|) ≥ max(|x|,|y|).
An index cannot be paired with itself; elements with equal values but different indices can be paired.
Input
The first line contains n; the second line contains n integers arr[0..n−1]. 1≤n≤1000, −10^9≤arr[i]≤10^9.
Output
On the first line, output k, the number of pairs that satisfy the conditions. Then output k lines, each with two integers arr[i] arr[j], sorted by i ascending and, for equal i, by j ascending.
Example 1
Input:
3
2 5 -3
Output:
2
2 -3
5 -3
Example 2
Input:
3
4 -4 0
Output:
1
4 -4
Example 3
Input:
3
1 3 7
Output:
0
Example notes
Example 1: arr=[2,5,−3]. (2,−3): min(5,1)=1≤2 and max(5,1)=5≥3; (5,−3): min(8,2)=2≤3 and max(8,2)=8≥5; (2,5): min(3,7)=3>2, so it fails. Example 2: in [4,−4,0], (4,−4) qualifies; a pair containing 0 would require the other number to be 0 as well, so it fails. Example 3: in [1,3,7], no pair satisfies condition 1, so the output is 0.
Editorial
Loading solution…