From 5a5bcd9691fffaa0e4e8d5335334f40577de8df2 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Thu, 30 Oct 2025 02:00:40 +0530 Subject: [PATCH 01/14] Add Trapping Rainwater problem implementation (Two Pointer Approach) --- .../searches/TrappingRainwater.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/TrappingRainwater.java diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java new file mode 100644 index 000000000000..90b01394166d --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java @@ -0,0 +1,58 @@ +package com.thealgorithms.searches; + +/** + * Trapping Rainwater Problem + * Given an array of non-negative integers representing the height of bars, + * compute how much water it can trap after raining. + * + * Example: + * Input: [4,2,0,3,2,5] + * Output: 9 + * + * Time Complexity: O(n) + * Space Complexity: O(1) + */ +public class TrappingRainwater { + + /** + * Calculates the total trapped rainwater. + * + * @param height an array representing elevation map + * @return total units of water trapped + */ + public static int trap(int[] height) { + if (height == null || height.length == 0) { + return 0; + } + + int left = 0, right = height.length - 1; + int leftMax = 0, rightMax = 0; + int trappedWater = 0; + + while (left < right) { + if (height[left] < height[right]) { + if (height[left] >= leftMax) { + leftMax = height[left]; + } else { + trappedWater += leftMax - height[left]; + } + left++; + } else { + if (height[right] >= rightMax) { + rightMax = height[right]; + } else { + trappedWater += rightMax - height[right]; + } + right--; + } + } + + return trappedWater; + } + + // Example test + public static void main(String[] args) { + int[] height = {4, 2, 0, 3, 2, 5}; + System.out.println("Total trapped water: " + trap(height)); // Output: 9 + } +} From 364bcd94da659a8dd15b3111600f2e74610d44ec Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Thu, 30 Oct 2025 02:04:05 +0530 Subject: [PATCH 02/14] Add Wikipedia reference link for Trapping Rainwater problem --- src/main/java/com/thealgorithms/searches/TrappingRainwater.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java index 90b01394166d..b0cc2558710f 100644 --- a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java +++ b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java @@ -11,6 +11,8 @@ * * Time Complexity: O(n) * Space Complexity: O(1) + * + * Reference: https://en.wikipedia.org/wiki/Trapping_rain_water */ public class TrappingRainwater { From b9a0653f229fbe1aa6430920beadd1a08ae4030f Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Thu, 30 Oct 2025 02:51:05 +0530 Subject: [PATCH 03/14] fix: format TrappingRainwater.java for CI check --- .../java/com/thealgorithms/searches/TrappingRainwater.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java index b0cc2558710f..7ce8bcf36c62 100644 --- a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java +++ b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java @@ -4,14 +4,14 @@ * Trapping Rainwater Problem * Given an array of non-negative integers representing the height of bars, * compute how much water it can trap after raining. - * + * * Example: * Input: [4,2,0,3,2,5] * Output: 9 - * + * * Time Complexity: O(n) * Space Complexity: O(1) - * + * * Reference: https://en.wikipedia.org/wiki/Trapping_rain_water */ public class TrappingRainwater { From 6d42fda305b4b380c2bb4a329a019f517a7f92d3 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Thu, 30 Oct 2025 03:04:40 +0530 Subject: [PATCH 04/14] fix: add package and resolve checkstyle errors for TrappingRainwater.java --- .../searches/TrappingRainwater.java | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java index 7ce8bcf36c62..3e24c37cf3e2 100644 --- a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java +++ b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java @@ -1,5 +1,4 @@ package com.thealgorithms.searches; - /** * Trapping Rainwater Problem * Given an array of non-negative integers representing the height of bars, @@ -16,45 +15,34 @@ */ public class TrappingRainwater { - /** - * Calculates the total trapped rainwater. - * - * @param height an array representing elevation map - * @return total units of water trapped - */ - public static int trap(int[] height) { - if (height == null || height.length == 0) { - return 0; - } + private TrappingRainwater() { + throw new UnsupportedOperationException("Utility class"); + } - int left = 0, right = height.length - 1; - int leftMax = 0, rightMax = 0; - int trappedWater = 0; + public static int trap(int[] height) { + int left = 0; + int right = height.length - 1; + int leftMax = 0; + int rightMax = 0; + int result = 0; while (left < right) { if (height[left] < height[right]) { if (height[left] >= leftMax) { leftMax = height[left]; } else { - trappedWater += leftMax - height[left]; + result += leftMax - height[left]; } left++; } else { if (height[right] >= rightMax) { rightMax = height[right]; } else { - trappedWater += rightMax - height[right]; + result += rightMax - height[right]; } right--; } } - - return trappedWater; - } - - // Example test - public static void main(String[] args) { - int[] height = {4, 2, 0, 3, 2, 5}; - System.out.println("Total trapped water: " + trap(height)); // Output: 9 + return result; } } From 3b569d63d009fc092ac359e3fc030d888dc5a6f1 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Thu, 30 Oct 2025 03:11:49 +0530 Subject: [PATCH 05/14] fix: declare TrappingRainwater as final to pass Checkstyle --- src/main/java/com/thealgorithms/searches/TrappingRainwater.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java index 3e24c37cf3e2..5cec9b8d98e1 100644 --- a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java +++ b/src/main/java/com/thealgorithms/searches/TrappingRainwater.java @@ -13,7 +13,7 @@ * * Reference: https://en.wikipedia.org/wiki/Trapping_rain_water */ -public class TrappingRainwater { +public final class TrappingRainwater { private TrappingRainwater() { throw new UnsupportedOperationException("Utility class"); From 09bd367e6d39736526f19d93e89caa6862bedd96 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Fri, 31 Oct 2025 22:35:08 +0530 Subject: [PATCH 06/14] Add test cases for TrappingRainwater algorithm --- .../searches/TrappingRainwaterTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java diff --git a/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java b/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java new file mode 100644 index 000000000000..d79a62a27059 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java @@ -0,0 +1,37 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +public class TrappingRainwaterTest { + + @Test + public void testExampleCase() { + int[] height = {4, 2, 0, 3, 2, 5}; + assertEquals(9, TrappingRainwater.trap(height)); + } + + @Test + public void testNoTrapping() { + int[] height = {1, 2, 3, 4, 5}; + assertEquals(0, TrappingRainwater.trap(height)); + } + + @Test + public void testFlatSurface() { + int[] height = {0, 0, 0, 0}; + assertEquals(0, TrappingRainwater.trap(height)); + } + + @Test + public void testSymmetricPit() { + int[] height = {3, 0, 2, 0, 3}; + assertEquals(7, TrappingRainwater.trap(height)); + } + + @Test + public void testSingleBar() { + int[] height = {5}; + assertEquals(0, TrappingRainwater.trap(height)); + } +} From 40b42976e5d2c3e2fb66642bc3a5acbf5119f43d Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Fri, 31 Oct 2025 22:41:28 +0530 Subject: [PATCH 07/14] Add test cases for TrappingRainwater algorithm --- .../java/com/thealgorithms/searches/TrappingRainwaterTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java b/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java index d79a62a27059..e2c35ab9e227 100644 --- a/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java +++ b/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.searches; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; public class TrappingRainwaterTest { From a611617ed7a0a4d777357278db04125e630ca811 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Sat, 1 Nov 2025 03:26:38 +0530 Subject: [PATCH 08/14] Move TrappingRainwater algorithm to stacks package --- pom.xml | 54 +++++++++---------- .../TrappingRainwater.java | 2 +- .../TrappingRainwaterTest.java | 2 +- 3 files changed, 26 insertions(+), 32 deletions(-) rename src/main/java/com/thealgorithms/{searches => stacks}/TrappingRainwater.java (97%) rename src/test/java/com/thealgorithms/{searches => stacks}/TrappingRainwaterTest.java (96%) diff --git a/pom.xml b/pom.xml index b7ca85e1407c..07c8c632361e 100644 --- a/pom.xml +++ b/pom.xml @@ -67,36 +67,30 @@ - org.apache.maven.plugins - maven-compiler-plugin - 3.14.1 - - 21 - - -Xlint:all - -Xlint:-auxiliaryclass - -Werror - - - - - org.jacoco - jacoco-maven-plugin - 0.8.14 - - - - prepare-agent - - - - generate-code-coverage-report - test - - report - - - + org.apache.maven.plugins + maven-compiler-plugin + 3.14.1 + + 21 + true + ${JAVA_HOME}/bin/javac + + -Xlint:all + -Xlint:-auxiliaryclass + -Werror + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + 21 + + 21 + + org.apache.maven.plugins diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java b/src/main/java/com/thealgorithms/stacks/TrappingRainwater.java similarity index 97% rename from src/main/java/com/thealgorithms/searches/TrappingRainwater.java rename to src/main/java/com/thealgorithms/stacks/TrappingRainwater.java index 5cec9b8d98e1..072665061b8e 100644 --- a/src/main/java/com/thealgorithms/searches/TrappingRainwater.java +++ b/src/main/java/com/thealgorithms/stacks/TrappingRainwater.java @@ -1,4 +1,4 @@ -package com.thealgorithms.searches; +package com.thealgorithms.stacks; /** * Trapping Rainwater Problem * Given an array of non-negative integers representing the height of bars, diff --git a/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java b/src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java similarity index 96% rename from src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java rename to src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java index e2c35ab9e227..909be6cd46da 100644 --- a/src/test/java/com/thealgorithms/searches/TrappingRainwaterTest.java +++ b/src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.searches; +package com.thealgorithms.stacks; import static org.junit.jupiter.api.Assertions.assertEquals; From a48fe3f3fa0736fc8f579042339b8c7b87d88bdb Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Sun, 2 Nov 2025 01:13:17 +0530 Subject: [PATCH 09/14] Fix: Move TrappingRainwater to stacks and normalize newline in JugglerSequence --- src/main/java/com/thealgorithms/maths/JugglerSequence.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/JugglerSequence.java b/src/main/java/com/thealgorithms/maths/JugglerSequence.java index 702310a1f295..ad64ca595d4a 100644 --- a/src/main/java/com/thealgorithms/maths/JugglerSequence.java +++ b/src/main/java/com/thealgorithms/maths/JugglerSequence.java @@ -20,7 +20,7 @@ private JugglerSequence() { * * @param inputNumber Number from which juggler sequence is to be started */ - public static void jugglerSequence(int inputNumber) { + public static String jugglerSequence(int inputNumber) { // Copy method argument to a local variable int n = inputNumber; List seq = new ArrayList<>(); @@ -43,7 +43,8 @@ public static void jugglerSequence(int inputNumber) { seq.add(n + ""); } String res = String.join(",", seq); - System.out.println(res); + System.out.print(res + "\n"); + return res; } // Driver code From b95962382e7b6a3bf5e445a1121b13dcc97dd004 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Sun, 2 Nov 2025 01:15:41 +0530 Subject: [PATCH 10/14] Fix: Normalize newline in JugglerSequence to ensure test consistency --- pom.xml | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 07c8c632361e..e101f46c3687 100644 --- a/pom.xml +++ b/pom.xml @@ -67,30 +67,22 @@ - org.apache.maven.plugins - maven-compiler-plugin - 3.14.1 - - 21 - true - ${JAVA_HOME}/bin/javac - - -Xlint:all - -Xlint:-auxiliaryclass - -Werror - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.13.0 - - 21 - - 21 - - + + org.apache.maven.plugins + maven-compiler-plugin + 3.14.1 + + 21 + true + + -Xlint:all + -Xlint:-auxiliaryclass + -Werror + + + 21 + + org.apache.maven.plugins From 4f9073db4a50734b127d1d5254100d54a3f8d894 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Sun, 2 Nov 2025 01:36:31 +0530 Subject: [PATCH 11/14] Fix: Normalize newline in JugglerSequence and remove return statement --- src/main/java/com/thealgorithms/maths/JugglerSequence.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/JugglerSequence.java b/src/main/java/com/thealgorithms/maths/JugglerSequence.java index ad64ca595d4a..6a91c2019746 100644 --- a/src/main/java/com/thealgorithms/maths/JugglerSequence.java +++ b/src/main/java/com/thealgorithms/maths/JugglerSequence.java @@ -20,7 +20,7 @@ private JugglerSequence() { * * @param inputNumber Number from which juggler sequence is to be started */ - public static String jugglerSequence(int inputNumber) { + public static void jugglerSequence(int inputNumber) { // Copy method argument to a local variable int n = inputNumber; List seq = new ArrayList<>(); From 2989ac1dd04ae188a8f7ffa2af56c24e2ec7cb72 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Sun, 2 Nov 2025 02:05:28 +0530 Subject: [PATCH 12/14] Add JaCoCo plugin for code coverage reporting --- pom.xml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pom.xml b/pom.xml index 0272958ab2f0..9ace6f27607e 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,25 @@ pmd-exclude.properties + + org.jacoco + jacoco-maven-plugin + 0.8.14 + + + + prepare-agent + + + + generate-code-coverage-report + test + + report + + + + From 76ff418e68a6d6ddb1a4e45d057c78c7553c4d00 Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Mon, 3 Nov 2025 19:44:50 +0530 Subject: [PATCH 13/14] Revert pom.xml to original version from master --- pom.xml | 67 ++++++++++++++++++++++++++------------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index 9ace6f27607e..b7ca85e1407c 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ org.junit junit-bom - 6.0.1 + 6.0.0 pom import @@ -67,22 +67,36 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.14.1 - - 21 - true - - -Xlint:all - -Xlint:-auxiliaryclass - -Werror - - - 21 - - + org.apache.maven.plugins + maven-compiler-plugin + 3.14.1 + + 21 + + -Xlint:all + -Xlint:-auxiliaryclass + -Werror + + + + + org.jacoco + jacoco-maven-plugin + 0.8.14 + + + + prepare-agent + + + + generate-code-coverage-report + test + + report + + + org.apache.maven.plugins @@ -139,25 +153,6 @@ pmd-exclude.properties - - org.jacoco - jacoco-maven-plugin - 0.8.14 - - - - prepare-agent - - - - generate-code-coverage-report - test - - report - - - - From 1dc4bff37f3c397a75aaab40262f1e0af4626c7e Mon Sep 17 00:00:00 2001 From: Arzoo1701 Date: Wed, 5 Nov 2025 19:07:26 +0530 Subject: [PATCH 14/14] Fix: Revert the pom.xml file --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b7ca85e1407c..7e936967208b 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ org.junit junit-bom - 6.0.0 + 6.0.1 pom import @@ -155,4 +155,4 @@ - + \ No newline at end of file