Problem ยท Sorting

Maximizing Difference in Array Elements with Moves ๐Ÿน

Learn this problem
โ— HardMicrosoftFULLTIMEOA
See Microsoft hiring insights

Problem statement

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

maximizeDifference(A: int[], K: int) โ†’ int

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

Examples

Example 1

A = [8, 8, 8, 7, 7, 7, 7, 7, 7, 7, -8, -8]K = 1return = 1
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! ๐Ÿซถ

Example 2

A = [-5, 1, 1, 4, 4, 4, 7, 4, 6]K = 6return = 7
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! ๐Ÿซถ

Example 3

A = [-7, -6, -3, -2, -2, -2, -2, -2, -2, -2, -2, -1]K = 5return = 3
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! ๐Ÿซถ

Example 4

A = [6, 6, 6, 6, 6, 6]K = 3return = 1
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

  • 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].

More Microsoft problems

drafts saved locally
public int maximizeDifference(int[] A, int K) {
    // write your code here
}
A[8, 8, 8, 7, 7, 7, 7, 7, 7, 7, -8, -8]
K1
expected1
checking account