Microsoft SDE NG OA February
Anonymous User
170

Maximizing Difference in Array Elements with Moves:

Given an array A of integers (size N, divisible by 3) and an integer K representing the maximum number of moves allowed, you can increase or decrease any element in A by 1 per move. The goal is to maximize the difference between the N/3-th largest and N/3-th smallest elements in A after using up to K moves.

Examples:

For A = [8, 8, 8, 7, 7, 7, 7, 7, 7, 7, -8, -8] and K = 1, return 1.
For A = [-5, 1, 1, 4, 4, 4, 7, 4, 6] and K = 6, return 7.
For A = [-7, -6, -3, -2, -2, -2, -2, -2, -2, -2, -2, -1] and K = 5, return 3.
For A = [6, 6, 6, 6, 6, 6] and K = 3, return 1.

Constraints:

N is an integer within [3..150,000), divisible by 3.
K is an integer within [0..500,000,000].
Each element of A is an integer within [-300,000,000..300,000,000].


I wonder if this would be a medium or hard.
I passed the listed test cases but I personally don't think I handled all cases.
Please share an optimal solution if you have one!

Comments (3)