FastPrepFastPrep
Problem Brief

Maximizing Difference in Array Elements with Moves ๐Ÿน

FULLTIMEOA
See Microsoft online assessment and hiring insights

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.

Function Description

Complete the function maximizeDifference in the editor.

maximizeDifference has the following parameters:

  1. 1. int[] A: an array of integers
  2. 2. int K: the maximum number of moves allowed

Returns

int: the maximum difference that can be achieved

1Example 1

Input
A = [8, 8, 8, 7, 7, 7, 7, 7, 7, 7, -8, -8], K = 1
Output
1
Explanation
No explanation is provided for now ๐Ÿ˜ณ๐Ÿ”ซ As always, I will add it once find any. Or if you happen to know about it, feel free to dm Groot! Many thanks in advance! ๐Ÿซถ

2Example 2

Input
A = [-5, 1, 1, 4, 4, 4, 7, 4, 6], K = 6
Output
7
Explanation
No explanation is provided for now ๐Ÿ˜ณ๐Ÿ”ซ As always, I will add it once find any. Or if you happen to know about it, feel free to dm Groot! Many thanks in advance! ๐Ÿซถ

3Example 3

Input
A = [-7, -6, -3, -2, -2, -2, -2, -2, -2, -2, -2, -1], K = 5
Output
3
Explanation
No explanation is provided for now ๐Ÿ˜ณ๐Ÿ”ซ As always, I will add it once find any. Or if you happen to know about it, feel free to dm Groot! Many thanks in advance! ๐Ÿซถ

4Example 4

Input
A = [6, 6, 6, 6, 6, 6], K = 3
Output
1
Explanation
No explanation is provided for now ๐Ÿ˜ณ๐Ÿ”ซ As always, I will add it once find any. Or if you happen to know about it, feel free to dm Groot! Many thanks in advance! ๐Ÿซถ

Constraints

Limits and guarantees your solution can rely on.

  • 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].
public int maximizeDifference(int[] A, int K) {
    // write your code here
}
Input

A

[8, 8, 8, 7, 7, 7, 7, 7, 7, 7, -8, -8]

K

1

Output

1

Sign in to submit your solution.