通过在最多 K 次操作后成对移动 X 值来最大化 Array 中最大和最小之间的差异
给定一个数组 arr[]大小为N和一个正整数K ,任务是通过执行最大化数组中最大元素和最小元素之间的差异 最多K个操作,其中每个操作:
- 从数组中选择两个整数arr[i]和arr[j]并且
- 将arr[i]更新为arr[i] – X并将arr[j]更新为arr[j] + X ,其中 X 是[0, min(arr[i], arr[j])]范围内的任何整数。
例子:
Input: arr[] = {3, 3, 2, 3, 3}, K = 1
Output: 6
Explanation: In the 1st operation select the integers (arr[1], arr[0]) and X as 3. Hence arr[1] = arr[1] – X = 3 – 3 = 0. Similarly, arr[0] = arr[0] + X = 3 + 3 = 6. Hence, arr[] = {6, 0, 2, 3, 3} and the diffecence between smallest and largest element is 6 which is the maximum possible.
Input: arr[] = {7, 4, 8, 11, 2, 23, 67, 22, 5, 29, 6, 4, 56}, N = 13, X = 5
Output: 208
方法:给定的问题可以通过使用贪心方法来解决。这个想法是最大化数组中最大元素的值。还可以观察到,数组中可以实现的最小元素是0 。以下是要遵循的步骤:
- 按降序对给定数组arr[]进行排序。
- 遍历[1, N)范围内的数组并对X = arr[i]执行(arr[i], arr[0])的给定操作。
- 在执行了K次操作之后, arr[0]将是所需的答案,除了K =0的情况。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum difference
// between the largest and smallest
// element of arr[] after K operations
int maxDifference(int arr[], int N, int K)
{
// Sort A in descending order
sort(arr, arr + N, greater());
// Case where no operation
// is to be performed
if (K == 0) {
return arr[0] - arr[N - 1];
}
// Loop to iterate arr[]
for (int i = 1; i < N && K != 0; ++i) {
// Update arr[0]
// and arr[i]
arr[0] += arr[i];
arr[i] = 0;
// Decrement K
K--;
}
// Return Answer
return arr[0];
}
// Driver Code
int main()
{
int arr[] = { 3, 3, 2, 3, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 1;
cout << maxDifference(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find maximum difference
// between the largest and smallest
// element of arr[] after K operations
static int maxDifference(Integer[] arr, int N, int K)
{
// Sort A in descending order
Arrays.sort(arr, Collections.reverseOrder());
// Case where no operation
// is to be performed
if (K == 0) {
return arr[0] - arr[N - 1];
}
// Loop to iterate arr[]
for (int i = 1; i < N && K != 0; ++i) {
// Update arr[0]
// and arr[i]
arr[0] += arr[i];
arr[i] = 0;
// Decrement K
K--;
}
// Return Answer
return arr[0];
}
// Driver Code
public static void main (String[] args) {
Integer[] arr = { 3, 3, 2, 3, 3 };
int N = arr.length;
int K = 1;
System.out.print(maxDifference(arr, N, K));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python program for the above approach
# Function to find maximum difference
# between the largest and smallest
# element of arr[] after K operations
def maxDifference(arr, N, K):
# Sort A in descending order
arr.sort(reverse = True)
# Case where no operation
# is to be performed
if (K == 0):
return arr[0] - arr[N - 1]
# Loop to iterate arr[]
for i in range(1, N):
# Update arr[0]
# and arr[i]
if(K != 0):
arr[0] = arr[0] + arr[i]
arr[i] = 0
# Decrement K
K = K - 1
# Return Answer
return arr[0]
# Driver Code
arr = [3, 3, 2, 3, 3]
N = len(arr)
K = 1
print(maxDifference(arr, N, K))
# This code is contributed by Taranpreet
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find maximum difference
// between the largest and smallest
// element of arr[] after K operations
static int maxDifference(int[] arr, int N, int K)
{
// Sort A in descending order
Array.Sort(arr);
Array.Reverse(arr);
// Case where no operation
// is to be performed
if (K == 0) {
return arr[0] - arr[N - 1];
}
// Loop to iterate arr[]
for (int i = 1; i < N && K != 0; ++i) {
// Update arr[0]
// and arr[i]
arr[0] += arr[i];
arr[i] = 0;
// Decrement K
K--;
}
// Return Answer
return arr[0];
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 3, 2, 3, 3 };
int n = arr.Length;
int k = 1;
Console.Write(maxDifference(arr, n, k));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
6
时间复杂度: O(N*log N)
辅助空间: O(1)