给定数组arr [] ,任务是检查是否有可能通过重复选择i和j不同的三元组( i , j , k ),并从arr [i]中减去k来使所有数组元素相等。并将k添加到arr [j] 。
例子:
Input: arr[] = {1, 5, 6, 4}
Output: Yes
Explanation:
Operations performed:
Choose i = 2, j = 0, k = 2 and perform the given operations. The array arr[] modifies to {3, 5, 4, 4}.
Choose i = 1, j = 0, k = 1 and perform the given operations. The array arr[] modifies to {4, 4, 4, 4}.
Now, all array elements are equal. Therefore, print Yes.
Input: arr[] = {2, 5, 3, 2, 2}
Output: No
天真的方法: 最简单的方法是基于以下观察:修改后的数组的总和将等于OI初始阵列的总和。请按照以下步骤解决此问题:
- 在使所有数组元素相等之后,将Y视为所有数组元素的值。因此, Y * N (其中N是数组大小)必须等于给定数组的总和。
- 迭代到数组中的最大值,并检查Y的可能值。如果发现满足指定条件,则打印“是” 。否则,打印“否” 。
证明:
- Choose some i, j and k at any step.
- Assume sum of the array elements to be equal to sum.
- Sum of array elements after removing arr[i] and arr[j] is sum – arr[i] – arr[j].
- Now, adding arr[i] – k and arr[j] + k into the array modifies the sum of the array to sum – arr[i] – arr[j] + arr[i] – k + arr[j] + k = sum.
时间复杂度: O(max(arr [i]))
辅助空间: O(1)
高效方法:最佳方法是检查给定数组的总和是否为N的因数。请按照以下步骤解决问题:
- 假设在使所有数组元素相等之后,数组元素将修正为X ,则X应该是一个整数,以使数组的总和可被N整除。
- 如果总和不能被N整除,则X将不是整数,并且不可能使所有数组元素相等。如果找不到整数,则打印“ No ”。否则,打印“是” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if its possible to
// make all array elements equal or not
void arrayElementEqual(int arr[], int N)
{
// Stores the sum of the array
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
sum += arr[i];
}
// If sum is divisible by N
if (sum % N == 0) {
cout << "Yes";
}
// Otherwise, not possible to make
// all array elements equal
else {
cout << "No" << endl;
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 1, 5, 6, 4 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
arrayElementEqual(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if its possible to
// make all array elements equal or not
static void arrayElementEqual(int arr[], int N)
{
// Stores the sum of the array
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
sum += arr[i];
}
// If sum is divisible by N
if (sum % N == 0)
{
System.out.print("Yes");
}
// Otherwise, not possible to make
// all array elements equal
else
{
System.out.print("No" +"\n");
}
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 1, 5, 6, 4 };
// Size of the array
int N = arr.length;
arrayElementEqual(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to check if its possible to
# make all array elements equal or not
def arrayElementEqual(arr, N):
# Stores the sum of the array
sum = 0
# Traverse the array
for i in range(N):
sum += arr[i]
# If sum is divisible by N
if (sum % N == 0):
print('Yes')
# Otherwise, not possible to make
# all array elements equal
else:
print("No")
# Driver Code
# Given array
arr = [ 1, 5, 6, 4 ]
# Size of the array
N = len(arr)
arrayElementEqual(arr, N)
# This code is contributed by rohitsingh07052
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to check if its possible to
// make all array elements equal or not
static void arrayElementEqual(int[] arr, int N)
{
// Stores the sum of the array
int sum = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
sum += arr[i];
}
// If sum is divisible by N
if (sum % N == 0)
{
Console.WriteLine("Yes");
}
// Otherwise, not possible to make
// all array elements equal
else
{
Console.Write("No" +"\n");
}
}
// Driver Code
static public void Main()
{
// Given array
int[] arr = { 1, 5, 6, 4 };
// Size of the array
int N = arr.Length;
arrayElementEqual(arr, N);
}
}
// This code is contributed by sanjoy_62.
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)