aboutsummaryrefslogtreecommitdiff
path: root/challenge-206/lubos-kolouch/java/ch-2.java
blob: f41385c38b174d3875443e34dd51a6f55e9af737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Arrays;

public class MaxMinPairSum {
  public static int maxMinPairSum(int[] array) {
    // Sort the array in ascending order
    Arrays.sort(array);

    // Use pairwise iteration to get pairs of adjacent elements
    // (0,1), (2,3), (4,5), ...
    int sum = 0;
    for (int i = 0; i < array.length; i += 2) {
      sum += Math.min(array[i], array[i + 1]);
    }

    return sum;
  }

  public static void main(String[] args) {
    // Define test cases
    int[] test1 = {1, 2, 3, 4};
    int expected1 = 4;
    int[] test2 = {0, 2, 1, 3};
    int expected2 = 2;

    // Run tests
    assert (maxMinPairSum(test1) == expected1);
    assert (maxMinPairSum(test2) == expected2);
  }
}