📌  相关文章
📜  修改数组的最小增量,以便可以拆分任何数组元素的值以使所有剩余元素相等

📅  最后修改于: 2021-10-26 06:52:15             🧑  作者: Mango

给定一个由N 个元素组成的数组arr[] ,任务是找到需要在给定数组上执行的最小增量数,以便在选择任何索引处的任何数组元素并将其值拆分为其他数组元素后,所有其他N – 1 个元素相等。

例子:

方法:按照以下步骤解决问题:

  1. 找到给定数组元素和该数组中存在的最大元素的总和,并将其存储在变量中,例如summaxelement
  2. 所有剩余的N – 1 个元素必须等于ceil(sum / N-1) 。将该值设为K
  3. 由于元素只能增加1 ,如果maxelement大于K ,则设置K等于maxelement
  4. 现在,每个N – 1值应该等于K 。因此,最终和应该是K * (N-1)
  5. 因此,所需的移动总数为K*(N – 1) – sum

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count minimum moves
void minimumMoves(int arr[], int N)
{
    // Stores sum of given array
    int sum = 0;
 
    // Stores maximum array element
    int maxelement = -1;
 
    // Base Case
    if (N == 2) {
 
        // If N is 2, the answer
        // will always be 0
        cout << 0;
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Calculate sum of the array
        sum += arr[i];
 
        // Finding maximum element
        maxelement = max(maxelement, arr[i]);
    }
 
    // Calculate ceil(sum/N-1)
    int K = (sum + N - 2) / (N - 1);
 
    // If k is smaller than maxelement
    K = max(maxelement, K);
 
    // Final sum - original sum
    int ans = K * (N - 1) - sum;
 
    // Print the minimum number
    // of increments required
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 3, 7 };
 
    // Size of given array
    int N = 3;
 
    // Function Call
    minimumMoves(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the minimum moves
    public static void minimumMoves(
        int[] arr, int N)
    {
        // Stores the sum of the array
        int sum = 0;
 
        // Store the maximum element
        int maxelement = -1;
 
        // Base Case
        if (N == 2) {
 
            // If N is 2, the answer
            // will always be 0
            System.out.print("0");
            return;
        }
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            // Calculate sum of the array
            sum += arr[i];
 
            // Finding maximum element
            maxelement = Math.max(
                maxelement, arr[i]);
        }
 
        // Calculate ceil(sum/N-1)
        int k = (sum + N - 2) / (N - 1);
 
        // If k is smaller than maxelement
        k = Math.max(maxelement, k);
 
        // Final sum - original sum
        int ans = k * (N - 1) - sum;
 
        // Print the minimum number
        // of increments required
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array
        int[] arr = { 2, 3, 7 };
 
        // Size of given array
        int N = arr.length;
 
        // Function Call
        minimumMoves(arr, N);
    }
}


Python3
# Python3 program for the above approach
 
# Function to count minimum moves
def minimumMoves(arr, N):
     
    # Stores sum of given array
    sum = 0
 
    # Stores maximum array element
    maxelement = -1
 
    # Base Case
    if (N == 2):
         
        # If N is 2, the answer
        # will always be 0
        print(0, end = "")
         
    # Traverse the array
    for i in range(N):
         
        # Calculate sum of the array
        sum += arr[i]
 
        # Finding maximum element
        maxelement = max(maxelement, arr[i])
 
    # Calculate ceil(sum/N-1)
    K = (sum + N - 2) // (N - 1)
 
    # If k is smaller than maxelement
    K = max(maxelement, K)
 
    # Final sum - original sum
    ans = K * (N - 1) - sum
 
    # Print the minimum number
    # of increments required
    print(ans)
     
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 2, 3, 7 ]
 
    # Size of given array
    N = 3
 
    # Function Call
    minimumMoves(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
     
  // Function to find the minimum moves
  static void minimumMoves(int[] arr, int N)
  {
 
    // Stores the sum of the array
    int sum = 0;
 
    // Store the maximum element
    int maxelement = -1;
 
    // Base Case
    if (N == 2)
    {
 
      // If N is 2, the answer
      // will always be 0
      Console.Write("0");
      return;
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // Calculate sum of the array
      sum += arr[i];
 
      // Finding maximum element
      maxelement = Math.Max(
        maxelement, arr[i]);
    }
 
    // Calculate ceil(sum/N-1)
    int k = (sum + N - 2) / (N - 1);
 
    // If k is smaller than maxelement
    k = Math.Max(maxelement, k);
 
    // Final sum - original sum
    int ans = k * (N - 1) - sum;
 
    // Print the minimum number
    // of increments required
    Console.WriteLine(ans);
  }
   
  // Driver code
  static void Main()
  {
     
    // Given array
    int[] arr = { 2, 3, 7 };
 
    // Size of given array
    int N = arr.Length;
 
    // Function Call
    minimumMoves(arr, N);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
2

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

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