FastPrepFastPrep
Problem Brief

Sum Max Plus Min After Decrement Operations

INTERNOA
See Amazon online assessment and hiring insights

You are given an integer array arr and an integer requests.

Repeat the following operation exactly requests times:

  1. Find the current maximum value and the current minimum value in arr.
  2. Add their sum to the answer.
  3. Choose one occurrence of the maximum value and decrease it by 1.

Return the final accumulated answer.

Function Description

Complete the function sumMaxPlusMinAfterOperations in the editor below.

sumMaxPlusMinAfterOperations has the following parameters:

  1. int[] arr: the initial values
  2. int requests: the number of operations

Returns

long: the accumulated sum.

1Example 1

Input
arr = [1, 2], requests = 2
Output
5
Explanation

First add 1 + 2 = 3 and decrement the 2 to 1. Then add 1 + 1 = 2. The total is 5.

2Example 2

Input
arr = [3, 3, 3], requests = 1
Output
6
Explanation

The current maximum and minimum are both 3, so the answer increases by 6.

Constraints

Limits and guarantees your solution can rely on.

  • requests >= 0
  • Each operation decreases one occurrence of the current maximum value by exactly 1.
  • Use a wide enough integer type for the accumulated total.
public long sumMaxPlusMinAfterOperations(int[] arr, int requests) {
    // write your code here
}
Input

arr

[1, 2]

requests

2

Output

5

Sign in to submit your solution.