2656. Maximum Sum With Exactly K Elements LeetCode Solution

In this guide, you will get 2656. Maximum Sum With Exactly K Elements LeetCode Solution with the best time and space complexity. The solution to Maximum Sum With Exactly K Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Sum With Exactly K Elements solution in C++
  4. Maximum Sum With Exactly K Elements solution in Java
  5. Maximum Sum With Exactly K Elements solution in Python
  6. Additional Resources
2656. Maximum Sum With Exactly K Elements LeetCode Solution image

Problem Statement of Maximum Sum With Exactly K Elements

You are given a 0-indexed integer array nums and an integer k. Your task is to perform the following operation exactly k times in order to maximize your score:

Select an element m from nums.
Remove the selected element m from the array.
Add a new element with a value of m + 1 to the array.
Increase your score by m.

Return the maximum score you can achieve after performing the operation exactly k times.

Example 1:

Input: nums = [1,2,3,4,5], k = 3
Output: 18
Explanation: We need to choose exactly 3 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
So, we will return 18.
It can be proven, that 18 is the maximum answer that we can achieve.

Example 2:

Input: nums = [5,5,5], k = 2
Output: 11
Explanation: We need to choose exactly 2 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
So, we will return 11.
It can be proven, that 11 is the maximum answer that we can achieve.

Constraints:

1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= k <= 100

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2656. Maximum Sum With Exactly K Elements LeetCode Solution in C++

class Solution {
 public:
  int maximizeSum(vector<int>& nums, int k) {
    // If x = max(nums), ans = x + (x + 1) + .. + (x + k - 1).
    const int x = ranges::max(nums);
    return x * k + k * (k - 1) / 2;
  }
};
/* code provided by PROGIEZ */

2656. Maximum Sum With Exactly K Elements LeetCode Solution in Java

class Solution {
  public int maximizeSum(int[] nums, int k) {
    // If x = max(nums), ans = x + (x + 1) + .. + (x + k - 1).
    final int x = Arrays.stream(nums).max().getAsInt();
    return x * k + k * (k - 1) / 2;
  }
}
// code provided by PROGIEZ

2656. Maximum Sum With Exactly K Elements LeetCode Solution in Python

class Solution:
  def maximizeSum(self, nums: list[int], k: int) -> int:
    return max(nums) * k + k * (k - 1) // 2
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.