给定一个由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 整除。因此,打印“否” 。
- 如果发现步骤 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。