Permutation of Array such that sum of adjacent elements are not divisible by 3

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 != 0

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.