Split integers into three groups based on their remainder modulo 3 so that every group sum is divisible by 3.
You are given an array of integers. Reorder or redistribute the elements into three groups according to their remainder when divided by 3, and determine whether it is possible to make each group have a sum divisible by 3 using the available elements.
A valid arrangement must satisfy the divisibility condition for every group. Your task is to work with the counts of numbers in each remainder class and decide the outcome requested by the problem.
Input Format
- The first line contains an integer .
- The second line contains integers .
Notes
- The exact operation is based on grouping by remainder modulo 3.
Output Format
- Print the required result for the grouping/divisibility condition.
- The answer is typically a single word such as
YESorNO, depending on whether the condition can be satisfied.
Constraints
- Array values fit in standard 32-bit signed integers.
- Time limit-friendly solutions should run in linear time.
Example 1
Input
6 1 2 3 4 5 6
Output
YES
Explanation
The numbers can be grouped so that each group has sum divisible by 3; for example, pairs like (1,2), (3), (4,5), and (6) each contribute sums divisible by 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.