📌  相关文章
📜  最小化修改后的数组的最大值和最小值之间的差异

📅  最后修改于: 2021-04-21 23:21:21             🧑  作者: Mango

给定一个由n个整数和一个整数X组成的数组A。您可以选择以下任意整数-X\leq k\leq X ,然后将k分别添加到A [i] 0\leq i \leq n-1 。的任务是找到A的最大值和更新所述阵列A之后A的最小值之间的最小可能差。

例子:

Input: arr[] = {1, 3, 6}, x = 3
Output: 0
New array is [3, 3, 3] or [4, 4, 4].

Input: arr[] = {0, 10}, x = 2
Output: 6
New array is [2, 8] i.e add 2 to a[0] and subtract -2 from a[1].

方法:A为原始数组。为了最小化max(A)– min(A) ,让我们分别尝试最小化max(A)和最大化min(A)

max(A)的最小可能值为max(A)– K ,因为max(A)的值不能降低。类似地, min(A)的最大可能值是min(A)+ K。因此,数量max(A)– min(A)至少是ans =(max(A)– K)–(min(A)+ K)

我们可以通过以下修改来获得此值:

下面是上述方法的实现。

CPP
// C++ program to find the minimum difference.
#include 
using namespace std;
  
// Function to return required minimum difference
int minDiff(int n, int x, int A[])
{
    int mn = A[0], mx = A[0];
  
    // finding minimum and maximum values
    for (int i = 0; i < n; ++i) {
        mn = min(mn, A[i]);
        mx = max(mx, A[i]);
    }
  
    // returning minimum possible difference
    return max(0, mx - mn - 2 * x);
}
  
// Driver program
int main()
{
  
    int n = 3, x = 3;
    int A[] = { 1, 3, 6 };
  
    // function to return the answer
    cout << minDiff(n, x, A);
  
    return 0;
}


Java
// Java program to find the minimum difference.
  
import java.util.*;
class GFG
{
      
    // Function to return required minimum difference
    static int minDiff(int n, int x, int A[])
    {
        int mn = A[0], mx = A[0];
      
        // finding minimum and maximum values
        for (int i = 0; i < n; ++i) {
            mn = Math.min(mn, A[i]);
            mx = Math.max(mx, A[i]);
        }
      
        // returning minimum possible difference
        return Math.max(0, mx - mn - 2 * x);
    }
      
    // Driver program
    public static void main(String []args)
    {
      
        int n = 3, x = 3;
        int A[] = { 1, 3, 6 };
      
        // function to return the answer
        System.out.println(minDiff(n, x, A));
      
          
    }
  
}
  
// This code is contributed by ihritik


Python3
# Python program to find the minimum difference.
  
      
# Function to return required minimum difference
def minDiff( n,  x,  A):
   
    mn =  A[0] 
    mx =  A[0] 
  
    # finding minimum and maximum values
    for i in range(0,n):
         mn = min( mn,  A[ i]) 
         mx = max( mx,  A[ i]) 
       
  
    # returning minimum possible difference
    return max(0,  mx -  mn - 2 *  x) 
   
      
# Driver program
  
n = 3 
x = 3 
A = [1, 3, 6 ] 
  
# function to return the answer
print(minDiff( n,  x,  A))
  
# This code is contributed by ihritik


C#
// C# program to find the minimum difference.
  
using System;
class GFG
{
      
    // Function to return required minimum difference
    static int minDiff(int n, int x, int []A)
    {
        int mn = A[0], mx = A[0];
      
        // finding minimum and maximum values
        for (int i = 0; i < n; ++i) {
            mn = Math.Min(mn, A[i]);
            mx = Math.Max(mx, A[i]);
        }
      
        // returning minimum possible difference
        return Math.Max(0, mx - mn - 2 * x);
    }
      
    // Driver program
    public static void Main()
    {
      
        int n = 3, x = 3;
        int []A = { 1, 3, 6 };
      
        // function to return the answer
        Console.WriteLine(minDiff(n, x, A));
             
    }
}
  
// This code is contributed by ihritik


PHP


输出:
0

时间复杂度: O(n)