aboutsummaryrefslogtreecommitdiff
path: root/challenge-206/lubos-kolouch/java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-206/lubos-kolouch/java')
-rw-r--r--challenge-206/lubos-kolouch/java/ch-1.java46
-rw-r--r--challenge-206/lubos-kolouch/java/ch-2.java29
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-206/lubos-kolouch/java/ch-1.java b/challenge-206/lubos-kolouch/java/ch-1.java
new file mode 100644
index 0000000000..18d9a6fa3b
--- /dev/null
+++ b/challenge-206/lubos-kolouch/java/ch-1.java
@@ -0,0 +1,46 @@
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class ShortestTimeTest {
+
+ public static int shortestTimeInMinutes(String[] times) {
+ int minDiff = 24 * 60; // maximum possible difference in minutes
+ for (int i = 0; i < times.length - 1; i++) {
+ for (int j = i + 1; j < times.length; j++) {
+ String[] parts1 = times[i].split(":");
+ String[] parts2 = times[j].split(":");
+ int h1 = Integer.parseInt(parts1[0]);
+ int m1 = Integer.parseInt(parts1[1]);
+ int h2 = Integer.parseInt(parts2[0]);
+ int m2 = Integer.parseInt(parts2[1]);
+ int diff = Math.abs((h1 - h2) * 60 + (m1 - m2));
+ if (diff > 12 * 60) { // account for circular time
+ diff = 24 * 60 - diff;
+ }
+ if (diff < minDiff) {
+ minDiff = diff;
+ }
+ }
+ }
+ return minDiff;
+ }
+
+ @Test
+ public void testExample1() {
+ String[] times = {"00:00", "23:55", "20:00"};
+ assertEquals(5, shortestTimeInMinutes(times));
+ }
+
+ @Test
+ public void testExample2() {
+ String[] times = {"01:01", "00:50", "00:57"};
+ assertEquals(4, shortestTimeInMinutes(times));
+ }
+
+ @Test
+ public void testExample3() {
+ String[] times = {"10:10", "09:30", "09:00", "09:55"};
+ assertEquals(15, shortestTimeInMinutes(times));
+ }
+}
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);
+ }
+}