给定一个由N个整数组成的数组arr [] ,任务是检查每个相邻元素对的和不能被3整除的数组元素是否存在任何排列。如果可能,请打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {1, 2, 3, 3}
Output: Yes
Explanation:
Since there exist at least 1 combination {3, 2, 3, 1} where sum of every adjacent pairs is not divisible by 3.
Input: arr[] = {3, 6, 1, 9}
Output: No
天真的方法:最简单的方法是生成给定数组的所有置换,并检查是否存在其中两个相邻元素之和不能被3整除的布置。如果发现是真的,则打印“是” 。否则,打印“否” 。
时间复杂度: O(N!)
辅助空间: O(1)
高效方法:为了优化上述方法,我们的想法是观察所有数组元素{{0,1,2}}的唯一可能余数。要将这三个数字分开,以使两个相邻元素的和不能被3整除,请按照以下步骤操作:
- 计数所有的数字成具有余数为0,1和2三个部分。设计数分别为a , b和c 。
- 现在,将余数为0的数与余数为1或2的数进行排列,以使它们的和不会被3整除。以下是可以满足此条件的条件:
- 如果a≥1且a≤b + c + 1
- 如果a和b都等于0且c> 0
- 如果a和c都等于0并且b> 0
- 如果无法以上述方式排列所有数字,则不会发生排列,以使它们的相邻元素之和不能被3整除。因此,请打印“ No” 。
- 如果发现步骤2中的条件为真,则打印“是” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to checks if any permutation
// of the array exists whose sum of
// adjacent pairs is not divisible by 3
void factorsOf3(int arr[], int N)
{
int a = 0, b = 0, c = 0;
for (int i = 0; i < N; i++) {
// Count remainder 0
if (arr[i] % 3 == 0)
a++;
// Count remainder 1
else if (arr[i] % 3 == 1)
b++;
// Count remainder 2
else if (arr[i] % 3 == 2)
c++;
}
// Condition for valid arrangements
if (a >= 1 && a <= b + c + 1)
cout << "Yes" << endl;
else if (a == 0 && b == 0 && c > 0)
cout << "Yes" << endl;
else if (a == 0 && c == 0 && b > 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 3, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
factorsOf3(arr, N);
return 0;
}
Java
// Java program for
// the above approach
class GFG{
// Function to checks if any permutation
// of the array exists whose sum of
// adjacent pairs is not divisible by 3
static void factorsOf3(int arr[], int N)
{
int a = 0, b = 0, c = 0;
for (int i = 0; i < N; i++)
{
// Count remainder 0
if (arr[i] % 3 == 0)
a++;
// Count remainder 1
else if (arr[i] % 3 == 1)
b++;
// Count remainder 2
else if (arr[i] % 3 == 2)
c++;
}
// Condition for valid arrangements
if (a >= 1 && a <= b + c + 1)
System.out.print("Yes" + "\n");
else if (a == 0 && b == 0 && c > 0)
System.out.print("Yes" + "\n");
else if (a == 0 && c == 0 && b > 0)
System.out.print("Yes" + "\n");
else
System.out.print("No" + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = {1, 2, 3, 3};
int N = arr.length;
// Function Call
factorsOf3(arr, N);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to checks if any permutation
# of the array exists whose sum of
# adjacent pairs is not divisible by 3
def factorsOf3(arr, N):
a = 0
b = 0
c = 0
for i in range(N):
# Count remainder 0
if (arr[i] % 3 == 0):
a += 1
# Count remainder 1
elif (arr[i] % 3 == 1):
b += 1
# Count remainder 2
elif (arr[i] % 3 == 2):
c += 1
# Condition for valid arrangements
if (a >= 1 and a <= b + c + 1):
print("Yes")
elif (a == 0 and b == 0 and c > 0):
print("Yes")
elif (a == 0 and c == 0 and b > 0):
print("Yes")
else:
print("No")
# Driver Code
# Given array arr[]
arr = [ 1, 2, 3, 3 ]
N = len(arr)
# Function call
factorsOf3(arr, N)
# This code is contributed by Shivam Singh
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to checks if any
// permutation of the array
// exists whose sum of
// adjacent pairs is not
// divisible by 3
static void factorsOf3(int []arr,
int N)
{
int a = 0, b = 0, c = 0;
for (int i = 0; i < N; i++)
{
// Count remainder 0
if (arr[i] % 3 == 0)
a++;
// Count remainder 1
else if (arr[i] % 3 == 1)
b++;
// Count remainder 2
else if (arr[i] % 3 == 2)
c++;
}
// Condition for valid arrangements
if (a >= 1 && a <= b + c + 1)
Console.Write("Yes" + "\n");
else if (a == 0 && b == 0 && c > 0)
Console.Write("Yes" + "\n");
else if (a == 0 && c == 0 && b > 0)
Console.Write("Yes" + "\n");
else
Console.Write("No" + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = {1, 2, 3, 3};
int N = arr.Length;
// Function Call
factorsOf3(arr, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)