Problem · Array

Minimize Maximum Group Difference

Learn this problem
MediumDRWOA

Problem statement

You are given an array A consisting of N integers. Divide all elements into three non-empty groups. Each element must belong to exactly one group.

For each group, define its difference as the largest integer in the group minus the smallest integer in the group.

Your goal is to make the maximum of these three group differences as small as possible.

Return the minimum possible value of that maximum difference.

Function

solution(A: int[]) → int

Examples

Example 1

A = [11,5,3,12,6,8,1,7,4]return = 3

One optimal division is [3,1,4], [5,6,8,7], and [11,12]. Their differences are 3, 3, and 1, so the maximum difference is 3.

Example 2

A = [10,14,12,1000,11,15,13,1]return = 5

One optimal division is [1], [10,14,12,11,15,13], and [1000]. The maximum group difference is 5.

Example 3

A = [4,5,7,10,10,12,12,12]return = 2

One optimal division is [4], [5,7], and [10,10,12,12,12]. The group differences are 0, 2, and 2.

More DRW problems

drafts saved locally
public int solution(int[] A) {
  // write your code here
}
A[11,5,3,12,6,8,1,7,4]
expected3
checking account