cswork / OA打开练习工作台

SalesforceOA #11

Find All Pairs of Integers

估计简单可在线判题

考点(AI 标注)位运算原地数组

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:

  1. min(|x−y|,|x+y|) ≤ min(|x|,|y|);
  2. 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.

输入格式

The first line contains n; the second line contains n integers arr[0..n−1]. 1≤n≤1000, −10^9≤arr[i]≤10^9.

输出格式

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.

样例 1

输入:

3
2 5 -3

输出:

2
2 -3
5 -3

样例 2

输入:

3
4 -4 0

输出:

1
4 -4

样例 3

输入:

3
1 3 7

输出:

0

样例说明

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.