Given an array arr[] of positive integers, the task is to find the permutation of the array such that sum of adjacent elements is not divisible by 3.
Note: If there is no such permutation of the array print -1.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 4 1 3 5 2
Explaination:
Sum of adjacent elements =>
arr[0] + arr[1] = 4 + 1 = 5 % 3 != 0
arr[1] + arr[2] = 1 + 3 = 4 % 3 != 0
arr[2] + arr[3] = 3 + 5 = 8 % 3 != 0
arr[3] + arr[4] = 5 + 2 = 7 % 3 != 0Input: arr[] = {1, 24, 30, 42, 51}
Output: -1
Approach: The key observation in the problem is any that there can be only three types of the remainder for all the types of numbers That is {0, 1, 2}. Hence, we can segregate numbers into three parts and if we arrange the numbers having remainder as 0 with the numbers having remainder as 1 or 2. Then their sum is not divisible by 3. If there is no way to arrange all the numbers in this way then there is no permutation such that their sum of adjacent elements are not divisible by 3.
Below is the implementation of the above approach:
C++
// C++ implementation to find the // permutation of the array such that // sum of adjacent elements is not // divisible by 3 #include <bits/stdc++.h> using namespace std; #define hell 1000000007 #define N 100005 // Function to segregate numbers // based on their remainder // when divided by three void count_k( vector<int>& arr, int& c_0, int& c_1, int& c_2, stack<int>& ones, stack<int>& twos, stack<int>& zeros) { // Loop to iterate over the elements // of the given array for (int i = 0; i < arr.size(); i++) { // Condition to check the // remainder of the number if (arr[i] % 3 == 0) { c_0++; zeros.push(arr[i]); } else if (arr[i] % 3 == 1) { c_1++; ones.push(arr[i]); } else { c_2++; twos.push(arr[i]); } } return; } // Function to find the permutation // of the array such that sum of // adjacent elements is not divisible by 3 void printArrangement( vector<int>& arr, int& c_0, int& c_1, int& c_2, stack<int>& ones, stack<int>& twos, stack<int>& zeros) { // Condition to check when // it's impossible to arrange if ((c_0 == 0 && c_1 != 0 && c_2 != 0) or c_0 > c_1 + c_2 + 1) { cout << "-1"; return; } // Condition to check when // there are no zeros, and // only ones or only twos if (c_0 == 0) { for (int i = 0; i < arr.size(); i++) { cout << arr[i] << " "; } return; } // Array to store the permutation int i, j, ans[N]; memset(ans, -1, sizeof(ans)); // Place the ones on alternate places // in the answer array, // leaving spaces for zeros remainder // elements in the array for (i = 1, j = 0; j < c_1; i += 2, j++) { ans[i] = ones.top(); ones.pop(); } // Adding a zero to // connect it with a two ans[i - 1] = zeros.top(); zeros.pop(); c_0--; // Place the twos on alternate places // in the answer array, // leaving spaces for zeros for (j = 0; j < c_2; j++, i += 2) { ans[i] = twos.top(); twos.pop(); } // Fill the zeros finally, // between the ones and the twos for (int k = 0; c_0 > 0; k += 2) { if (ans[k] == -1) { ans[k] = zeros.top(); c_0--; } } // Print the arrangment of the array for (int i = 0; i < N; i++) { if (ans[i] != -1) cout << ans[i] << " "; } return; } // Function to solve the problem void solve(int n, vector<int>& arr) { // As there can be only 3 remainders stack<int> ones, zeros, twos; int c_0 = 0, c_1 = 0, c_2 = 0; count_k(arr, c_0, c_1, c_2, ones, twos, zeros); // Function Call printArrangement( arr, c_0, c_1, c_2, ones, twos, zeros); } // Driver Code signed main() { int n = 5; vector<int> arr{ 1, 2, 3, 4, 5 }; solve(n, arr); return 0; } |
4 1 3 5 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Find permutation of n which is divisible by 3 but not divisible by 6
- Permutation of Array such that products of all adjacent elements are even
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum elements to be removed such that sum of adjacent elements is always even
- Maximum sum in circular array such that no two elements are adjacent
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Count of elements not divisible by any other elements of Array
- Python | Number of elements to be removed such that product of adjacent elements is always even
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Elements of an array that are not divisible by any element of another array
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Lexicographically smallest permutation of {1, .. n} such that no. and position do not match
- Maximum sum such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

