Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
Last Updated :
15 Nov, 2022
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 6
Explanation:
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:
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++
#include <bits/stdc++.h>
using namespace std;
void printGroups( int n, int a[])
{
int ct[7] = { 0 }, grps = n / 3, i;
for (i = 0; i < n; i++)
ct[a[i]]++;
if (ct[1] != grps || (ct[4] + ct[6]) != grps
|| (ct[2] + ct[3]) != grps || ct[4] > ct[2])
{
cout << -1;
return ;
}
for (i = 0; i < ct[4]; i++)
cout << "1 2 4\n" ;
for (i = 0; i < ct[2] - ct[4]; i++)
cout << "1 2 6\n" ;
for (i = 0; i < ct[3]; i++)
cout << "1 3 6\n" ;
}
int main()
{
int n = 6;
int a[n] = { 2, 2, 1, 1, 4, 6 };
printGroups(n, a);
return 0;
}
|
Java
class GFG
{
static void printGroups( int n, int a[])
{
int ct[] = new int [ 7 ], grps = n / 3 , i;
for (i = 0 ; i < n; i++)
{
ct[a[i]]++;
}
if (ct[ 1 ] != grps || (ct[ 4 ] + ct[ 6 ]) != grps
|| (ct[ 2 ] + ct[ 3 ]) != grps || ct[ 4 ] > ct[ 2 ])
{
System.out.print(- 1 );
return ;
}
for (i = 0 ; i < ct[ 4 ]; i++)
{
System.out.print( "1 2 4\n" );
}
for (i = 0 ; i < ct[ 2 ] - ct[ 4 ]; i++)
{
System.out.print( "1 2 6\n" );
}
for (i = 0 ; i < ct[ 3 ]; i++)
{
System.out.print( "1 3 6\n" );
}
}
public static void main(String[] args)
{
int n = 6 ;
int a[] = { 2 , 2 , 1 , 1 , 4 , 6 };
printGroups(n, a);
}
}
|
Python3
def printGroups(n, a):
ct = [ 0 for i in range ( 7 )]
grps = n / / 3
i = 0
for i in range (n):
ct[a[i]] + = 1
if (ct[ 1 ] ! = grps or (ct[ 4 ] + ct[ 6 ]) ! = grps or
(ct[ 2 ] + ct[ 3 ]) ! = grps or ct[ 4 ] > ct[ 2 ]):
print ( - 1 )
return
for i in range (ct[ 4 ]):
print ( "1 2 4" )
for i in range (ct[ 2 ] - ct[ 4 ]):
print ( "1 2 6" )
for i in range (ct[ 3 ]):
print ( "1 3 6" )
n = 6
a = [ 2 , 2 , 1 , 1 , 4 , 6 ]
printGroups(n, a)
|
C#
using System;
class GFG
{
static void printGroups( int n, int []a)
{
int []ct = new int [7];
int grps = n / 3, i;
for (i = 0; i < n; i++)
{
ct[a[i]]++;
}
if (ct[1] != grps || (ct[4] + ct[6]) != grps ||
(ct[2] + ct[3]) != grps || ct[4] > ct[2])
{
Console.Write(-1);
return ;
}
for (i = 0; i < ct[4]; i++)
{
Console.Write( "1 2 4\n" );
}
for (i = 0; i < ct[2] - ct[4]; i++)
{
Console.Write( "1 2 6\n" );
}
for (i = 0; i < ct[3]; i++)
{
Console.Write( "1 3 6\n" );
}
}
public static void Main()
{
int n = 6;
int []a = {2, 2, 1, 1, 4, 6};
printGroups(n, a);
}
}
|
PHP
<?php
function printGroups( $n , $a )
{
$ct = array (); $grps = $n / 3;
for ( $i = 0; $i < $n ; $i ++)
$ct [ $a [ $i ]]++;
if ( $ct [1] != $grps || ( $ct [4] + $ct [6]) != $grps ||
( $ct [2] + $ct [3]) != $grps || $ct [4] > $ct [2])
{
echo -1;
return ;
}
for ( $i = 0; $i < $ct [4]; $i ++)
echo "1 2 4\n" ;
for ( $i = 0; $i < $ct [2] - $ct [4]; $i ++)
echo "1 2 6\n" ;
for ( $i = 0; $i < $ct [3]; $i ++)
echo "1 3 6\n" ;
}
$n = 6;
$a = array (2, 2, 1, 1, 4, 6);
printGroups( $n , $a );
?>
|
Javascript
<script>
function printGroups(n, a)
{
let ct = new Array(7).fill(0), grps = parseInt(n / 3), i;
for (i = 0; i < n; i++)
ct[a[i]]++;
if (ct[1] != grps || (ct[4] + ct[6]) != grps
|| (ct[2] + ct[3]) != grps || ct[4] > ct[2])
{
document.write(-1);
return ;
}
for (i = 0; i < ct[4]; i++)
document.write( "1 2 4<br>" );
for (i = 0; i < ct[2] - ct[4]; i++)
document.write( "1 2 6<br>" );
for (i = 0; i < ct[3]; i++)
document.write( "1 3 6<br>" );
}
let n = 6;
let a = [ 2, 2, 1, 1, 4, 6 ];
printGroups(n, a);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...