#K41217. Find Pairs with Sum Divisible by 3
Find Pairs with Sum Divisible by 3
Find Pairs with Sum Divisible by 3
You are given an array of integers and an integer \(k\). Your task is to find \(k\) pairs of integers from the array such that the sum of the integers in each pair is divisible by 3. Each number from the array may be used at most once in forming pairs.
If there are multiple valid solutions, you can output any one of them. If it is impossible to form \(k\) such pairs, print NO
.
Example:
Input: 6 2 1 2 3 4 5 6</p>Output: YES 6 3 4 5
In the example above, one valid answer is to pair 6
with 3
(since \(6+3=9\) and \(9 \% 3 = 0\)) and 4
with 5
(since \(4+5=9\) and \(9 \% 3 = 0\)).
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains two integers \(n\) and \(k\), where \(n\) is the number of integers in the array and \(k\) is the number of pairs required.
- The second line contains \(n\) space-separated integers representing the array.
Constraints:
- Each integer in the array can be used at most once.
- You must output exactly \(k\) pairs if possible.
outputFormat
If it is possible to form \(k\) pairs according to the requirements, print YES
on the first line, followed by \(k\) lines each containing two space-separated integers representing a valid pair.
If it is impossible, print NO
.
Output is to be written to standard output (stdout).
## sample6 2
1 2 3 4 5 6
YES
6 3
4 5
</p>