You are given an integer array arr and an integer requests.
Repeat the following operation exactly requests times:
- Find the current maximum value and the current minimum value in
arr. - Add their sum to the answer.
- Choose one occurrence of the maximum value and decrease it by
1.
Return the final accumulated answer.
Complete the function sumMaxPlusMinAfterOperations in the editor below.
sumMaxPlusMinAfterOperations has the following parameters:
int[] arr: the initial valuesint requests: the number of operations
Returns
long: the accumulated sum.
Examples
01 · Example 1
arr = [1, 2] requests = 2 return = 5
First add 1 + 2 = 3 and decrement the 2 to 1. Then add 1 + 1 = 2. The total is 5.
02 · Example 2
arr = [3, 3, 3] requests = 1 return = 6
The current maximum and minimum are both 3, so the answer increases by 6.
Constraints
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.
More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public long sumMaxPlusMinAfterOperations(int[] arr, int requests) {
// write your code here
}
arr[1, 2]
requests2
expected5
sign in to submit