Plus and Minus
Practice
5 (8 votes)
Algorithms
Binary search
Problem
73% Success 780 Attempts 30 Points 3s Time Limit 256MB Memory 1024 KB Max Code

Initially, you have an array of length \(2\), which is \(a = [a_1, a_2]\). Here \(a_1 \geq a_2 \geq 0\).


In each step, you must choose the two largest integers \(a_i\) and \(a_j\) in the array, where \(a_i \geq a_j\), and insert two integers \(a_i + a_j\) and \(a_i - a_j\) to the array (in any positions).

Thus, you can construct an array of any even length \(\geq 4\).

Now you are given an array of even length \(N\).

Please determine whether you can form this array by the steps. If so, then find out which is the initial array.

Constraints

  • \(1 \leq T \leq 1000\)
  • \(4 \leq N \leq 10^7\)
  • \(0 \leq a_i \leq 10^9\)
  •  \(\)The sum of \(N\) across all test cases does not exceed \(10^7\).

Input Format

  • The first line contains a single integer \(T\) —- the number of test cases.
  • For each testcase:
    • The first line contains one odd integer \(N\) —- the length of the arrays.
    • The second line contains \(N\) positive integers \(a_1, a_2, ... , a_N\).
  • The sum of \(N\) over all test cases does not exceed \(10^7\).

Output Format

  • For each testcase, output "\(YES\)" if you can construct the given array by the steps. Otherwise, output "\(NO\)".
  • If you can construct such an array, then print two integers \(a_1, a_2\). Note that \(a_1 \geq a_2\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
18 votes
Tags:
AlgorithmsBinary SearchMediumPrefix sumSearchingTwo pointerprefix-sum
Points:30
14 votes
Tags:
AlgorithmsBinary SearchMediumSearching
Points:30
6 votes
Tags:
Greedy algorithmBinary SearchSortingAlgorithms