aboutsummaryrefslogtreecommitdiff
path: root/challenge-206/lubos-kolouch/java/ch-2.java
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-03-06 18:41:45 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-03-06 18:41:45 +0800
commit849fbc07ea10ad83d018bccc357c758e8855474e (patch)
treef3a77db28df57c8507010d1983f8dfe16eb07737 /challenge-206/lubos-kolouch/java/ch-2.java
parentd2cca4f3533f095d018affa34893ac385b6b02af (diff)
parentd75c83429332efc88f29eee14f988b199f2fa10c (diff)
downloadperlweeklychallenge-club-849fbc07ea10ad83d018bccc357c758e8855474e.tar.gz
perlweeklychallenge-club-849fbc07ea10ad83d018bccc357c758e8855474e.tar.bz2
perlweeklychallenge-club-849fbc07ea10ad83d018bccc357c758e8855474e.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-206/lubos-kolouch/java/ch-2.java')
-rw-r--r--challenge-206/lubos-kolouch/java/ch-2.java29
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-206/lubos-kolouch/java/ch-2.java b/challenge-206/lubos-kolouch/java/ch-2.java
new file mode 100644
index 0000000000..f41385c38b
--- /dev/null
+++ b/challenge-206/lubos-kolouch/java/ch-2.java
@@ -0,0 +1,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);
+ }
+}