📌  相关文章
📜  在 K 次操作后最大化最大和最小数组元素之间的差异

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

给定一个大小为N的数组arr[]和一个正整数K ,任务是通过将数组元素增加或减少1 K次来找到数组中最大元素和最小元素之间的最大差异。

例子:

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

  • 按降序对数组进行排序。
  • 遍历数组并计算前 K 个最大元素的总和。
  • 最后,打印数组中前 K 个最大元素的总和。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the maximum difference
// between the maximum and minimum in the
// array after K operations
int maxDiffLargSmallOper(int arr[],
                         int N, int K)
{
    // Stores maximum difference between
    // largest  and smallest array element
    int maxDiff = 0;
 
    // Sort the array in descending order
    sort(arr, arr + N, greater());
 
    // Traverse the array arr[]
    for (int i = 0; i <= min(K, N - 1);
         i++) {
 
        // Update maxDiff
        maxDiff += arr[i];
    }
 
    return maxDiff;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 7, 7, 7, 7 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
    int K = 1;
    cout << maxDiffLargSmallOper(arr, N, K);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Reverse array
static int[] reverse(int a[])
{
    int i, n = a.length, t;
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Function to find the maximum difference
// between the maximum and minimum in the
// array after K operations
static int maxDiffLargSmallOper(int arr[],
                                int N, int K)
{
     
    // Stores maximum difference between
    // largest  and smallest array element
    int maxDiff = 0;
 
    // Sort the array in descending order
    Arrays.sort(arr);
    arr = reverse(arr);
     
    // Traverse the array arr[]
    for(int i = 0; i <= Math.min(K, N - 1); i++)
    {
         
        // Update maxDiff
        maxDiff += arr[i];
    }
     
    return maxDiff;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 7, 7, 7, 7 };
    int N = arr.length;
    int K = 1;
     
    System.out.print(maxDiffLargSmallOper(arr, N, K));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum difference
# between the maximum and minimum in the
# array after K operations
def maxDiffLargSmallOper(arr, N, K):
     
     
    # Stores maximum difference between
    # largest  and smallest array element
    maxDiff = 0;
     
    # Sort the array in descending order
    arr.sort(reverse = True);
     
     
    # Traverse the array arr[]
    for i  in  range(min(K + 1, N)):
         
        # Update maxDiff
        maxDiff += arr[i];
     
    return maxDiff;
 
# Driver Code
if __name__ == "__main__": 
 
    arr = [ 7, 7, 7, 7 ];
    N = len(arr)
    K = 1;
    print(maxDiffLargSmallOper(arr, N, K));


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the maximum difference
// between the maximum and minimum in the
// array after K operations
static int maxDiffLargSmallOper(int []arr, int N,
                                int K)
{
     
    // Stores maximum difference between
    // largest and smallest array element
    int maxDiff = 0;
 
    // Sort the array in descending order
    Array.Sort(arr);
    Array.Reverse(arr);
     
    // Traverse the array arr[]
    for(int i = 0; i <= Math.Min(K, N - 1); i++)
    {
         
        // Update maxDiff
        maxDiff += arr[i];
    }
    return maxDiff;
}
 
// Driver code
public static void Main()
{
    int [] arr = new int[]{ 7, 7, 7, 7 };
    int N = arr.Length;
    int K = 1;
     
    Console.Write(maxDiffLargSmallOper(arr, N, K));
}
}
 
// This code is contributed by mohit kumar 29


Javascript


输出:
14

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

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