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

📅  最后修改于: 2021-09-04 08:01:37             🧑  作者: Mango

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

例子:

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

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

证明:

时间复杂度: O(max(arr[i]))
辅助空间: O(1)

有效的方法:最佳的想法是检查给定数组的总和是否是N的因子。请按照以下步骤解决问题:

  • 假设在所有数组元素都相等后数组元素修改为XX应该是一个整数,使得数组的总和可以被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