📌  相关文章
📜  通过重复从一个元素中添加 K 并从其他元素中减去 K 来使 Array 相等的最少操作

📅  最后修改于: 2021-10-27 06:18:44             🧑  作者: Mango

给定一个数组arr[]和一个整数K,任务是找到使数组arr[] 的所有元素相等的最少操作次数。在一个操作中,从一个元素中减去K,并将这个K加到另一个元素中。如果不可能,则打印-1

例子:

方法:这个问题可以使用贪心算法解决。首先检查数组arr[]的所有元素的总和是否可以被N整除。如果它不可整除,则意味着不可能使数组arr[] 的所有元素都相等。否则,尝试加减K的值,使每个元素等于arr[] / N 的总和。 请按照以下步骤解决此问题:

  • 将变量sum初始化为0以存储数组arr[]的所有元素的总和。
  • 使用变量i在范围[0, N-1] 中迭代并将sum更新为sum + arr[i]。
  • 如果sum % N不等于0,则打印-1返回。
  • 将变量valueAfterDivision初始化为sum/N以存储这么多值,数组arr[] 的每个元素都被存储并计数0以存储使所有数组元素相等所需的最小操作次数。
  • 使用变量i在范围[0, N-1] 中迭代
    • 如果 abs of valueAfterDivision – arr[i] % K不等于0,则打印-1返回。
    • 更新计数计数+ ABS valueAfterDivision – arr[i] / K。
  • 完成以上步骤后,打印count/2作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number of
// operations to make all the elements of
// the array equal
void miniOperToMakeAllEleEqual(int arr[], int n, int k)
{
    // Store the sum of the array arr[]
    int sum = 0;
 
    // Traverse through the array
    for (int i = 0; i < n; i++) {
 
        sum += arr[i];
    }
 
    // If it is not possible to make all
    // array element equal
    if (sum % n) {
        cout << -1;
        return;
    }
 
    int valueAfterDivision = sum / n;
 
    // Store the minimum number of operations needed
    int count = 0;
 
    // Traverse through the array
    for (int i = 0; i < n; i++) {
 
        if (abs(valueAfterDivision - arr[i]) % k != 0) {
            cout << -1;
            return;
        }
 
        count += abs(valueAfterDivision - arr[i]) / k;
    }
 
    // Finally, print the minimum number operation
    // to make array elements equal
    cout << count / 2 << endl;
}
 
// Driver Code
int main()
{
 
    // Given Input
    int n = 3, k = 3;
    int arr[3] = { 5, 8, 11 };
 
    // Function Call
    miniOperToMakeAllEleEqual(arr, n, k);
    // This code is contributed by Potta Lokesh
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to find the minimum number of
  // operations to make all the elements of
  // the array equal
  static void miniOperToMakeAllEleEqual(int arr[], int n, int k)
  {
    // Store the sum of the array arr[]
    int sum = 0;
 
    // Traverse through the array
    for (int i = 0; i < n; i++) {
 
      sum += arr[i];
    }
 
    // If it is not possible to make all
    // array element equal
    if (sum % n != 0) {
      System.out.println(-1);
      return;
    }
 
    int valueAfterDivision = sum / n;
 
    // Store the minimum number of operations needed
    int count = 0;
 
    // Traverse through the array
    for (int i = 0; i < n; i++) {
 
      if (Math.abs(valueAfterDivision - arr[i]) % k != 0) {
        System.out.println(-1);
        return;
      }
 
      count += Math.abs(valueAfterDivision - arr[i]) / k;
    }
 
    // Finally, print the minimum number operation
    // to make array elements equal
    System.out.println((int)count / 2);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given Input
    int n = 3, k = 3;
    int arr[] = { 5, 8, 11 };
 
    // Function Call
    miniOperToMakeAllEleEqual(arr, n, k);
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to find the minimum number of
# operations to make all the elements of
# the array equal
def miniOperToMakeAllEleEqual(arr, n, k):
     
    # Store the sum of the array arr[]
    sum = 0
 
    # Traverse through the array
    for i in range(n):
        sum += arr[i]
 
    # If it is not possible to make all
    # array element equal
    if (sum % n):
        print(-1)
        return
 
    valueAfterDivision = sum // n
 
    # Store the minimum number of operations needed
    count = 0
 
    # Traverse through the array
    for i in range(n):
        if (abs(valueAfterDivision - arr[i]) % k != 0):
            print(-1)
            return
 
        count += abs(valueAfterDivision - arr[i]) // k
 
    # Finally, print the minimum number operation
    # to make array elements equal
    print(count // 2)
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    n = 3
    k = 3
    arr = [ 5, 8, 11 ]
 
    # Function Call
    miniOperToMakeAllEleEqual(arr, n, k)
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG
{
   
    // Function to find the minimum number of
    // operations to make all the elements of
    // the array equal
    static void miniOperToMakeAllEleEqual(int[] arr, int n,
                                          int k)
    {
        // Store the sum of the array arr[]
        int sum = 0;
 
        // Traverse through the array
        for (int i = 0; i < n; i++) {
 
            sum += arr[i];
        }
 
        // If it is not possible to make all
        // array element equal
        if (sum % n != 0) {
            Console.WriteLine(-1);
            return;
        }
 
        int valueAfterDivision = sum / n;
 
        // Store the minimum number of operations needed
        int count = 0;
 
        // Traverse through the array
        for (int i = 0; i < n; i++) {
 
            if (Math.Abs(valueAfterDivision - arr[i]) % k
                != 0) {
                Console.WriteLine(-1);
                return;
            }
 
            count += Math.Abs(valueAfterDivision - arr[i])
                     / k;
        }
 
        // Finally, print the minimum number operation
        // to make array elements equal
        Console.WriteLine((int)count / 2);
    }
 
    static void Main()
    {
        // Given Input
        int n = 3, k = 3;
        int[] arr = { 5, 8, 11 };
 
        // Function Call
        miniOperToMakeAllEleEqual(arr, n, k);
    }
}
 
// This code is contributed by abhinavjain194


Javascript


输出
1

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程