给定一个数组arr[] ,任务是检查是否可以通过重复选择三元组 ( i , j , k ) 来使所有数组元素相等,其中i和j不同,并从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
天真的方法: 最简单的方法是基于以下观察:修改后的数组的总和将等于初始阵列的总和。请按照以下步骤解决此问题:
- 在使所有数组元素相等后,将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.
Javascript
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live