在增加最多 K 个元素后最大化数组总和,然后将数组除以 X
给定一个数组arr[]和两个整数K和X,任务是在任意元素最多增加K个增量后最大化数组元素的总和,然后将所有元素除以X
例子:
Input: arr = {16, 15, 17}, K = 8, X = 10
Output: 5
Explanation: At most 8 increments are allowed. So, increment element at index 0 by 4 and element at index 2 by 3. So the modified array becomes {20/10, 15/10, 20/10} = {2, 1, 2}, therefore the sum is 2+1+2 = 5
Input: arr = {8, 13, 2, 4, 7}, K = 6, X = 5
Output: 7
方法:给定的问题可以通过使用贪心方法来解决。这个想法是通过调整比较器来对数组进行排序,以便首先遇到需要最小增量才能被X整除的元素。请按照以下步骤解决问题:
- 按照除以X后余数较大的元素首先遇到的顺序对 ArrayList 进行排序
- 迭代 ArrayList 并在每次迭代时:
- 求剩余值rem加到arr[i] 中,使其成为X的倍数
- 如果K < rem,则中断循环
- 否则用rem递增arr[i]并用rem递减K
- 求剩余值rem加到arr[i] 中,使其成为X的倍数
- 将变量sum初始化为 0
- 遍历 ArrayList 并在每次迭代时:
- 将arr[i] / X添加到总和
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
int x = 5;
static bool cmp(int a, int b) { return (b % x) < (a % x); }
int maxSumIncDiv(vector nums, int k)
{
// Sort in such a way that the
// numbers needing least increment
// to become multiple of x are
// encountered first
sort(nums.begin(), nums.end(), cmp);
// Iterate the ArrayList
for (int i = 0; i < nums.size(); i++) {
int rem = nums[i] % x;
// Find the value to increment
// current element such that it
// becomes a multiple of x
rem = x - rem;
// Incrementing any element with
// the remaining value of k will
// have no effect
if (k < rem) {
break;
}
else {
// Increment element so that
// it becomes multiple of x
nums[i] = nums[i] + rem;
// Decrement k by the
// remaining value
k -= rem;
}
}
// Initialize sum
int sum = 0;
// Traverse the ArrayList
for (int i = 0; i < nums.size(); i++) {
// calculate sum of elements
// divided by x
sum += nums[i] / x;
}
// Return the answer
return sum;
}
int main()
{
// Initializing list of nums
vector nums = { 8, 13, 2, 4, 7 };
int K = 6;
// Call the function and
// print the answer
cout << (maxSumIncDiv(nums, K));
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.util.*;
import java.io.*;
class GFG {
public static int maxSumIncDiv(
List nums, int k, int x)
{
// Sort in such a way that the
// numbers needing least increment
// to become multiple of x are
// encountered first
Collections.sort(
nums,
(a, b) -> b % x - a % x);
// Iterate the ArrayList
for (int i = 0; i < nums.size(); i++) {
int rem = nums.get(i) % x;
// Find the value to increment
// current element such that it
// becomes a multiple of x
rem = x - rem;
// Incrementing any element with
// the remaining value of k will
// have no effect
if (k < rem) {
break;
}
else {
// Increment element so that
// it becomes multiple of x
nums.set(i, nums.get(i) + rem);
// Decrement k by the
// remaining value
k -= rem;
}
}
// Initialize sum
int sum = 0;
// Traverse the ArrayList
for (int i = 0; i < nums.size(); i++) {
// calculate sum of elements
// divided by x
sum += nums.get(i) / x;
}
// Return the answer
return sum;
}
public static void main(String[] args)
{
// Initializing list of nums
List nums = new ArrayList<>();
int K = 6, X = 5;
nums.add(8);
nums.add(13);
nums.add(2);
nums.add(4);
nums.add(7);
// Call the function and
// print the answer
System.out.println(
maxSumIncDiv(nums, K, X));
}
}
Python3
# Python implementation for the above approach
import math
def maxSumIncDiv(nums, k, x):
# Sort in such a way that the
# numbers needing least increment
# to become multiple of x are
# encountered first
nums.sort();
# Iterate the ArrayList
for i in range(len(nums)):
rem = nums[i] % x;
# Find the value to increment
# current element such that it
# becomes a multiple of x
rem = x - rem;
# Incrementing any element with
# the remaining value of k will
# have no effect
if (k < rem):
break;
else:
# Increment element so that
# it becomes multiple of x
nums[i] = nums[i] + rem;
# Decrement k by the
# remaining value
k -= rem;
# Initialize sum
sum = 0;
# Traverse the ArrayList
for i in range(len(nums)):
# calculate sum of elements
# divided by x
sum += nums[i] / x;
# Return the answer
return math.floor(sum)
# Initializing list of nums
nums = [];
K = 6
X = 5
nums.append(8);
nums.append(13);
nums.append(2);
nums.append(4);
nums.append(7);
# Call the function and
# print the answer
print(maxSumIncDiv(nums, K, X));
# This code is contributed by gfgking.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
public static int maxSumIncDiv(
List nums, int k, int x)
{
// Sort in such a way that the
// numbers needing least increment
// to become multiple of x are
// encountered first
nums.Sort((a, b) => b % x - a % x);
// Iterate the ArrayList
for (int i = 0; i < nums.Count; i++) {
int rem = nums[i] % x;
// Find the value to increment
// current element such that it
// becomes a multiple of x
rem = x - rem;
// Incrementing any element with
// the remaining value of k will
// have no effect
if (k < rem) {
break;
}
else {
// Increment element so that
// it becomes multiple of x
nums[i] = nums[i] + rem;
// Decrement k by the
// remaining value
k -= rem;
}
}
// Initialize sum
int sum = 0;
// Traverse the ArrayList
for (int i = 0; i < nums.Count; i++) {
// calculate sum of elements
// divided by x
sum += nums[i] / x;
}
// Return the answer
return (sum);
}
// Driver Code
public static void Main(String[] args)
{
// Initializing list of nums
List nums = new List();
int K = 6, X = 5;
nums.Add(8);
nums.Add(13);
nums.Add(2);
nums.Add(4);
nums.Add(7);
// Call the function and
// print the answer
Console.Write(
maxSumIncDiv(nums, K, X));
}
}
// This code is contributed by code_hunt.
Javascript
输出
7
时间复杂度: O(N * log N)
辅助空间: O(N)