📌  相关文章
📜  在 Array 中完成的最小增量使得 arr[i] 可以使所有其他元素相等

📅  最后修改于: 2022-05-13 01:56:08.411000             🧑  作者: Mango

在 Array 中完成的最小增量使得 arr[i] 可以使所有其他元素相等

给定一个大小为N的数组arr[] ,任务是找到数组中元素所需的最小增量,使得如果任何元素说arr[i]分布在其他元素中,则使N – 1 个元素相等。

例子:

方法:解决这个问题的想法是基于以下观察:如果(N-1)*mx其中mx是数组的最大元素大于数组的总和,那么答案就是(N-1) *mx - 总和。否则,如果(N-1)*mx小于或等于 sum 然后取一个临时变量,比如tempsum / N – 1分配它。如果sum mod N -1不等于0 ,则将 temp 增加1并取一个计数器来表示count ,其中count是 temp 和mx的差。更新ans = (count + mx) * (N – 1)并按 count 递增 sum。如果sum等于(N-1)*mx返回count ,否则返回 count 以(N-1)*mx的差值递增和总和。请按照以下步骤解决问题:

  • 将变量mxsum初始化为0以找到数组的总和和最大元素。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • mx的值更新为mxarr[i]的最大值,并将arr [i] 的值添加到变量sum。
  • 将变量ans初始化为(N-1)*mx。
  • 如果ans大于sum则返回ans-sum作为答案。
  • 否则,将变量temp初始化为sum/(N-1) + sum%(N-1)。
  • 将变量count初始化为temp – mx
  • ans的值设置为(count + mx)*(N-1)。
  • count的值添加到变量sum 中。
  •  如果sum等于ans,则返回count的值作为答案,否则返回count + ans – sum。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
int minIncrement(int arr[], int N)
{
    // Variable for max element
    // and sum
    int mx = 0, sum = 0;
    for (int i = 0; i < N; i++) {
        mx = max(mx, arr[i]);
        sum += arr[i];
    }
 
    // Calculate ans
    int ans = (N - 1) * mx;
 
    // If ans is greater than sum
    // return its difference
    if (ans > sum) {
        return (ans - sum);
    }
 
    // If ans is less or equal to sum
    else {
        int temp = sum / (N - 1);
        if (sum % (N - 1) != 0) {
            temp++;
        }
        int count = temp - mx;
        ans = (count + mx) * (N - 1);
        sum += count;
 
        // If sum is equal to ans
        // return the count
        if (sum == ans) {
            return count;
        }
 
        // Else return the summation
        // of count and difference
        // of ans and sum
        else {
            return (count + (ans - sum));
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 3, 1, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << minIncrement(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
static int minIncrement(int arr[], int N)
{
   
    // Variable for max element
    // and sum
    int mx = 0, sum = 0;
    for (int i = 0; i < N; i++) {
        mx = Math.max(mx, arr[i]);
        sum += arr[i];
    }
 
    // Calculate ans
    int ans = (N - 1) * mx;
 
    // If ans is greater than sum
    // return its difference
    if (ans > sum) {
        return (ans - sum);
    }
 
    // If ans is less or equal to sum
    else {
        int temp = sum / (N - 1);
        if (sum % (N - 1) != 0) {
            temp++;
        }
        int count = temp - mx;
        ans = (count + mx) * (N - 1);
        sum += count;
 
        // If sum is equal to ans
        // return the count
        if (sum == ans) {
            return count;
        }
 
        // Else return the summation
        // of count and difference
        // of ans and sum
        else {
            return (count + (ans - sum));
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 4, 3, 1, 6 };
    int N = arr.length;
 
    System.out.print(minIncrement(arr, N));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# python program for the above approach
 
# Function to find minimum increment
# required to an element to make N-1
# elements equal by distributing
# any element
def minIncrement(arr, N):
 
        # Variable for max element
        # and sum
    mx = 0
    sum = 0
    for i in range(0, N):
        mx = max(mx, arr[i])
        sum += arr[i]
 
        # Calculate ans
    ans = (N - 1) * mx
 
    # If ans is greater than sum
    # return its difference
    if (ans > sum):
        return (ans - sum)
 
        # If ans is less or equal to sum
    else:
        temp = sum // (N - 1)
        if (sum % (N - 1) != 0):
            temp += 1
 
        count = temp - mx
        ans = (count + mx) * (N - 1)
        sum += count
 
        # If sum is equal to ans
        # return the count
        if (sum == ans):
            return count
 
            # Else return the summation
            # of count and difference
            # of ans and sum
        else:
            return (count + (ans - sum))
 
# Driver Code
if __name__ == "__main__":
 
    arr = [4, 3, 1, 6]
    N = len(arr)
 
    print(minIncrement(arr, N))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG {
 
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
static int minIncrement(int []arr, int N)
{
   
    // Variable for max element
    // and sum
    int mx = 0, sum = 0;
    for (int i = 0; i < N; i++) {
        mx = Math.Max(mx, arr[i]);
        sum += arr[i];
    }
 
    // Calculate ans
    int ans = (N - 1) * mx;
 
    // If ans is greater than sum
    // return its difference
    if (ans > sum) {
        return (ans - sum);
    }
 
    // If ans is less or equal to sum
    else {
        int temp = sum / (N - 1);
        if (sum % (N - 1) != 0) {
            temp++;
        }
        int count = temp - mx;
        ans = (count + mx) * (N - 1);
        sum += count;
 
        // If sum is equal to ans
        // return the count
        if (sum == ans) {
            return count;
        }
 
        // Else return the summation
        // of count and difference
        // of ans and sum
        else {
            return (count + (ans - sum));
        }
    }
}
 
// Driver Code
public static void Main()
{
    int []arr = { 4, 3, 1, 6 };
    int N = arr.Length;
 
    Console.Write(minIncrement(arr, N));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
4

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