Array Not Divisible by Three
InterviewTime limit1sMemory limit128 MB
Rearrange an array so no two adjacent elements sum to a multiple of 3, or report impossibility.
Problem
Given an array of positive integers, reorder all numbers so that the sum of every pair of adjacent numbers is not divisible by 3.
If several valid orders exist, print any one of them. Every given number must be used exactly once.
Input
The first line contains the size of the array, N. (1 <= N <= 10000)
The second line contains N positive integers separated by spaces. Each integer is at most 1000000.
Output
If the array can be reordered as required, print one valid reordered array on one line.
If it is impossible, print -1.