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

📅  最后修改于: 2021-05-17 22:03:55             🧑  作者: 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)