# Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1

Given an array **A** containing **N** elements (**N** is divisible by **3**), the task is to split the numbers into groups of 3, let the group have 3 elements X1, X2 and X3, the following conditions should be true for the group:

- X1, X2 and X3 are pairwise distinct
- X3 is divisible by X2
- X2 is divisible by X1

Print -1 if splitting the array into **N/3** Such groups is not possible.

**Note:** Elements of the array will lie in the range 1 to 6 (inclusive).

**Examples:**

Input :N = 6, A[] = {2, 2, 1, 1, 4, 6}Output: 1 2 4 1 2 6Explanation:Group 1: Pairs = {(1,2), (2,4), (1,4)} All pairs are distinct, 4 is divisible by 2 and 2 by 1.Group 2: Pairs = {(1,2), (2,6), (1,6)} All pairs are distinct, 6 is divisible by 2 and 2 by 1.Input :N = 6, A[] = {1, 1, 1, 6, 6, 3}Output :-1

**Approach:**

Since the values of the array are between 1 and 6, only the following kind of groups can be made:

- 1 2 4
- 1 2 6
- 1 3 6

Start of by counting the frequency of each element. Since 1 is common across all groups, it must occur exactly **N/3** times. 4 can be put into only the first kind of group, which always contains 2. So the count of 2 should be greater than the count of 4. The remaining 2 can be put in only the second kind of groups. Now, the remaining numbers have to be put in the third kind of groups. If at any point the count is less than required, the answer would be -1.

Below is the implementation of the above approach:

## C++

`// C++ program to split array in groups of 3 ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the groups after ` `// spliting array in groups of 3 ` `void` `printGroups(` `int` `n, ` `int` `a[]) ` `{ ` ` ` `int` `ct[7] = { 0 }, grps = n / 3, i; ` ` ` ` ` `// Count occurence of each element ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `ct[a[i]]++; ` ` ` ` ` `// Check if it is possible to form the groups ` ` ` `if` `(ct[1] != grps || (ct[4] + ct[6]) != grps ` ` ` `|| (ct[2] + ct[3]) != grps || ct[4] > ct[2]) ` ` ` `{ ` ` ` `cout << -1; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print groups that end at 4 ` ` ` `for` `(i = 0; i < ct[4]; i++) ` ` ` `cout << ` `"1 2 4\n"` `; ` ` ` ` ` `// Print groups that end at 6, with 2 ` ` ` `// in the middle ` ` ` `for` `(i = 0; i < ct[2] - ct[4]; i++) ` ` ` `cout << ` `"1 2 6\n"` `; ` ` ` ` ` `// Print groups that have a 3 in the middle ` ` ` `for` `(i = 0; i < ct[3]; i++) ` ` ` `cout << ` `"1 3 6\n"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` `int` `a[n] = { 2, 2, 1, 1, 4, 6 }; ` ` ` ` ` `printGroups(n, a); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to split array in groups of 3 ` `class` `GFG ` `{ ` ` ` ` ` `// Function to print the groups after ` ` ` `// spliting array in groups of 3 ` ` ` `static` `void` `printGroups(` `int` `n, ` `int` `a[]) ` ` ` `{ ` ` ` `int` `ct[] = ` `new` `int` `[` `7` `], grps = n / ` `3` `, i; ` ` ` ` ` `// Count occurence of each element ` ` ` `for` `(i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `ct[a[i]]++; ` ` ` `} ` ` ` ` ` `// Check if it is possible to form the groups ` ` ` `if` `(ct[` `1` `] != grps || (ct[` `4` `] + ct[` `6` `]) != grps ` ` ` `|| (ct[` `2` `] + ct[` `3` `]) != grps || ct[` `4` `] > ct[` `2` `]) ` ` ` `{ ` ` ` `System.out.print(-` `1` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print groups that end at 4 ` ` ` `for` `(i = ` `0` `; i < ct[` `4` `]; i++) ` ` ` `{ ` ` ` `System.out.print(` `"1 2 4\n"` `); ` ` ` `} ` ` ` ` ` `// Print groups that end at 6, with 2 ` ` ` `// in the middle ` ` ` `for` `(i = ` `0` `; i < ct[` `2` `] - ct[` `4` `]; i++) ` ` ` `{ ` ` ` `System.out.print(` `"1 2 6\n"` `); ` ` ` `} ` ` ` ` ` `// Print groups that have a 3 in the middle ` ` ` `for` `(i = ` `0` `; i < ct[` `3` `]; i++) ` ` ` `{ ` ` ` `System.out.print(` `"1 3 6\n"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` `int` `a[] = {` `2` `, ` `2` `, ` `1` `, ` `1` `, ` `4` `, ` `6` `}; ` ` ` ` ` `printGroups(n, a); ` ` ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to split array in ` `# groups of 3 ` ` ` `# Function to pr the groups after ` `# spliting array in groups of 3 ` `def` `printGroups(n, a): ` ` ` ` ` `ct ` `=` `[` `0` `for` `i ` `in` `range` `(` `7` `)] ` ` ` `grps ` `=` `n ` `/` `/` `3` ` ` `i ` `=` `0` ` ` ` ` `# Count occurence of each element ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `ct[a[i]] ` `+` `=` `1` ` ` ` ` `# Check if it is possible to ` ` ` `# form the groups ` ` ` `if` `(ct[` `1` `] !` `=` `grps ` `or` `(ct[` `4` `] ` `+` `ct[` `6` `]) !` `=` `grps ` `or` ` ` `(ct[` `2` `] ` `+` `ct[` `3` `]) !` `=` `grps ` `or` `ct[` `4` `] > ct[` `2` `]): ` ` ` `print` `(` `-` `1` `) ` ` ` `return` ` ` ` ` `# Pr groups that end at 4 ` ` ` `for` `i ` `in` `range` `(ct[` `4` `]): ` ` ` `print` `(` `"1 2 4"` `) ` ` ` ` ` `# Pr groups that end at 6, with 2 ` ` ` `# in the middle ` ` ` `for` `i ` `in` `range` `(ct[` `2` `] ` `-` `ct[` `4` `]): ` ` ` `print` `(` `"1 2 6"` `) ` ` ` ` ` `# Pr groups that have a 3 in the middle ` ` ` `for` `i ` `in` `range` `(ct[` `3` `]): ` ` ` `print` `(` `"1 3 6"` `) ` ` ` `# Driver Code ` `n ` `=` `6` `a ` `=` `[` `2` `, ` `2` `, ` `1` `, ` `1` `, ` `4` `, ` `6` `] ` ` ` `printGroups(n, a) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to split array in groups of 3 ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to print the groups after ` ` ` `// spliting array in groups of 3 ` ` ` `static` `void` `printGroups(` `int` `n, ` `int` `[]a) ` ` ` `{ ` ` ` `int` `[]ct = ` `new` `int` `[7]; ` ` ` `int` `grps = n / 3, i; ` ` ` ` ` `// Count occurence of each element ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `ct[a[i]]++; ` ` ` `} ` ` ` ` ` `// Check if it is possible to form the groups ` ` ` `if` `(ct[1] != grps || (ct[4] + ct[6]) != grps || ` ` ` `(ct[2] + ct[3]) != grps || ct[4] > ct[2]) ` ` ` `{ ` ` ` `Console.Write(-1); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Print groups that end at 4 ` ` ` `for` `(i = 0; i < ct[4]; i++) ` ` ` `{ ` ` ` `Console.Write(` `"1 2 4\n"` `); ` ` ` `} ` ` ` ` ` `// Print groups that end at 6, with 2 ` ` ` `// in the middle ` ` ` `for` `(i = 0; i < ct[2] - ct[4]; i++) ` ` ` `{ ` ` ` `Console.Write(` `"1 2 6\n"` `); ` ` ` `} ` ` ` ` ` `// Print groups that have a 3 in the middle ` ` ` `for` `(i = 0; i < ct[3]; i++) ` ` ` `{ ` ` ` `Console.Write(` `"1 3 6\n"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 6; ` ` ` `int` `[]a = {2, 2, 1, 1, 4, 6}; ` ` ` ` ` `printGroups(n, a); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

$ct[2])

{

echo -1;

return;

}

// Print groups that end at 4

for ($i = 0; $i < $ct[4]; $i++)
echo "1 2 4\n";
// Print groups that end at 6, with 2
// in the middle
for ($i = 0; $i < $ct[2] - $ct[4]; $i++)
echo "1 2 6\n";
// Print groups that have a 3 in the middle
for ($i = 0; $i < $ct[3]; $i++)
echo "1 3 6\n";
}
// Driver Code
$n = 6;
$a = array(2, 2, 1, 1, 4, 6);
printGroups($n, $a);
// This code is contributed
// by Akanksha Rai
?>

**Output:**

1 2 4 1 2 6

**Time Complexity:** O(N)

## Recommended Posts:

- Ways to split array into two groups of same XOR value
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Split N^2 numbers into N groups of equal sum
- Python | Split string in groups of n consecutive characters
- Count pairs in array whose sum is divisible by 4
- Print array elements that are divisible by at-least one other
- Count divisible pairs in an array
- Subsequences of size three in an array whose sum is divisible by m
- Count pairs in array whose sum is divisible by K
- Largest divisible subset in array
- Sum of all the elements in an array divisible by a given number K
- Count elements that are divisible by at-least one element in another array
- Possible to make a divisible by 3 number using all digits in an array
- Check if the sum of primes is divisible by any prime from the array
- Count all prefixes of the given binary array which are divisible by x

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.