给定一个包含N个元素的数组A ( N可被3整除),任务是将数字分成3组,让该组具有3个元素X1,X2和X3,该组应满足以下条件:
- X1,X2和X3成对区分
- X3可被X2整除
- X2可被X1整除
如果将阵列分为N / 3,则打印-1。这样的组是不可能的。
注意:数组的元素将在1到6(含)范围内。
例子:
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
方法:
由于数组的值在1到6之间,因此只能进行以下类型的分组:
- 1 2 4
- 1 2 6
- 1 3 6
通过计算每个元素的频率开始。由于1在所有组中都是通用的,因此它必须精确地出现N / 3次。 4只能放入始终包含2的第一种组中。因此2的计数应大于4的计数。其余2只能放入第二种组中。现在,剩下的数字必须放在第三类中。如果在任何时候该计数都小于要求,则答案将为-1。
下面是上述方法的实现:
C++
// C++ program to split array in groups of 3
#include
using namespace std;
// Function to print the groups after
// splitting array in groups of 3
void printGroups(int n, int a[])
{
int ct[7] = { 0 }, grps = n / 3, i;
// Count occurrence 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;
}
Java
// Java program to split array in groups of 3
class GFG
{
// Function to print the groups after
// splitting array in groups of 3
static void printGroups(int n, int a[])
{
int ct[] = new int[7], grps = n / 3, i;
// Count occurrence 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 */
Python3
# Python3 program to split array in
# groups of 3
# Function to print the groups after
# splitting array in groups of 3
def printGroups(n, a):
ct = [0 for i in range(7)]
grps = n // 3
i = 0
# Count occurrence 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
# Print groups that end at 4
for i in range(ct[4]):
print("1 2 4")
# Print groups that end at 6, with 2
# in the middle
for i in range(ct[2] - ct[4]):
print("1 2 6")
# Print 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
C#
// C# program to split array in groups of 3
using System;
class GFG
{
// Function to print the groups after
// splitting array in groups of 3
static void printGroups(int n, int []a)
{
int []ct = new int[7];
int grps = n / 3, i;
// Count occurrence 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
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
?>
Javascript
输出:
1 2 4
1 2 6
时间复杂度: O(N)