📌  相关文章
📜  使所有数组元素相等所需的每对元素的最小K增量和减量

📅  最后修改于: 2021-04-17 18:52:20             🧑  作者: Mango

给定数组arr [] ,任务是检查是否有可能通过重复选择ij不同的三元组( ijk ),并从arr [i]中减去k来使所有数组元素相等。并将k添加到arr [j]

例子:

天真的方法: 最简单的方法是基于以下观察:修改后的数组的总和将等于OI初始阵列的总和。请按照以下步骤解决此问题:

  • 在使所有数组元素相等之后,将Y视为所有数组元素的值。因此, Y * N (其中N是数组大小)必须等于给定数组的总和。
  • 迭代到数组中的最大值,并检查Y的可能值。如果发现满足指定条件,则打印“是” 。否则,打印“否”

证明:

时间复杂度: 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)