From fc320b5d5d8c80ab5747fe9214d2939b780dbcb8 Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 11:01:11 +0530 Subject: [PATCH 1/7] feat: add nearest element stack algorithms with JUnit tests --- .../thealgorithms/stacks/NearestElement.java | 73 +++++++++++++++++++ .../stacks/NearestElementTest.java | 35 +++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/main/java/com/thealgorithms/stacks/NearestElement.java create mode 100644 src/test/java/com/thealgorithms/stacks/NearestElementTest.java diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java new file mode 100644 index 000000000000..78d8a2479d25 --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -0,0 +1,73 @@ +package com.thealgorithms.datastructures.stacks; + +import java.util.Stack; +import java.util.Arrays; + +public final class NearestElement { + + private NearestElement() {} + + public static int[] nearestGreaterToRight(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { + while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static int[] nearestGreaterToLeft(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = 0; i < n; i++) { + while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static int[] nearestSmallerToRight(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { + while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static int[] nearestSmallerToLeft(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = 0; i < n; i++) { + while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static void main(String[] args) { + int[] sampleArray = {4, 5, 2, 10, 8}; + System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(sampleArray))); + System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(sampleArray))); + System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(sampleArray))); + System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(sampleArray))); + } +} diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java new file mode 100644 index 000000000000..13920348c0c5 --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -0,0 +1,35 @@ +package com.thealgorithms.datastructures.stacks; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class NearestElementTest { + + @Test + public void testNearestGreaterToRight() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {5, 10, 10, -1, -1}; + assertArrayEquals(expected, NearestElement.nearestGreaterToRight(arr)); + } + + @Test + public void testNearestGreaterToLeft() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {-1, -1, 5, -1, 10}; + assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(arr)); + } + + @Test + public void testNearestSmallerToRight() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {2, 2, -1, 8, -1}; + assertArrayEquals(expected, NearestElement.nearestSmallerToRight(arr)); + } + + @Test + public void testNearestSmallerToLeft() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {-1, 4, -1, 2, 2}; + assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr)); + } +} From db3fd3551d607953a056d5f37e69ccc0468d43aa Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 11:15:41 +0530 Subject: [PATCH 2/7] test: increase coverage for edge cases and private constructor --- .../stacks/NearestElementTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 13920348c0c5..e77e3f0ca82b 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -32,4 +32,27 @@ public void testNearestSmallerToLeft() { int[] expected = {-1, 4, -1, 2, 2}; assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr)); } + + @Test + void testEmptyArray() { + int[] arr = {}; + assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToRight(arr)); + assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToLeft(arr)); +} + + @Test + void testAllEqualElements() { + int[] arr = {5, 5, 5, 5}; + assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToRight(arr)); + assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToLeft(arr)); +} + + @Test + void testPrivateConstructor() throws Exception { + var constructor = NearestElement.class.getDeclaredConstructor(); + constructor.setAccessible(true); + constructor.newInstance(); +} + + } From b4739f88be8fbf212ac5d986e4c79e55ab5f6ceb Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 11:20:03 +0530 Subject: [PATCH 3/7] fix: correct package declaration for NearestElement and update imports in test --- .../java/com/thealgorithms/stacks/NearestElement.java | 2 +- .../com/thealgorithms/stacks/NearestElementTest.java | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java index 78d8a2479d25..7cb4b103896b 100644 --- a/src/main/java/com/thealgorithms/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -1,4 +1,4 @@ -package com.thealgorithms.datastructures.stacks; +package com.thealgorithms.stacks; import java.util.Stack; import java.util.Arrays; diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index e77e3f0ca82b..57fae10d5f96 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms.datastructures.stacks; +package com.thealgorithms.stacks; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -38,21 +38,19 @@ void testEmptyArray() { int[] arr = {}; assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToRight(arr)); assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToLeft(arr)); -} + } @Test void testAllEqualElements() { int[] arr = {5, 5, 5, 5}; assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToRight(arr)); assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToLeft(arr)); -} + } @Test void testPrivateConstructor() throws Exception { var constructor = NearestElement.class.getDeclaredConstructor(); constructor.setAccessible(true); constructor.newInstance(); -} - - + } } From 47ed73c3d54f4e3f1336e8f701fc0e657bd0e30c Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 19:12:22 +0530 Subject: [PATCH 4/7] feat: Add Nearest Greater and Smaller Element Algorithms --- .../thealgorithms/stacks/NearestElement.java | 86 +++++++++++------ .../stacks/NearestElementTest.java | 93 ++++++++++++------- 2 files changed, 114 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java index 7cb4b103896b..3724f37387bb 100644 --- a/src/main/java/com/thealgorithms/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -1,73 +1,101 @@ package com.thealgorithms.stacks; import java.util.Stack; -import java.util.Arrays; +/** + * This class provides four algorithms to find the nearest greater or smaller elements + * on either side of each element in an array using a stack. + * + *

These algorithms are fundamental examples of the monotonic stack approach, commonly + * used in competitive programming and technical interviews. Each method runs in O(n) + * time complexity and O(n) space complexity. + */ public final class NearestElement { + // Private constructor to prevent instantiation private NearestElement() {} + /** + * Finds the nearest greater element to the right for each element in the array. + * + * @param arr the input array + * @return an array containing the nearest greater element to the right for each element + */ public static int[] nearestGreaterToRight(int[] arr) { int n = arr.length; int[] result = new int[n]; - Stack indexStack = new Stack<>(); + Stack stack = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { - while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) { - indexStack.pop(); + while (!stack.isEmpty() && stack.peek() <= arr[i]) { + stack.pop(); } - result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; - indexStack.push(i); + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } return result; } + /** + * Finds the nearest greater element to the left for each element in the array. + * + * @param arr the input array + * @return an array containing the nearest greater element to the left for each element + */ public static int[] nearestGreaterToLeft(int[] arr) { int n = arr.length; int[] result = new int[n]; - Stack indexStack = new Stack<>(); + Stack stack = new Stack<>(); + for (int i = 0; i < n; i++) { - while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) { - indexStack.pop(); + while (!stack.isEmpty() && stack.peek() <= arr[i]) { + stack.pop(); } - result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; - indexStack.push(i); + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } return result; } + /** + * Finds the nearest smaller element to the right for each element in the array. + * + * @param arr the input array + * @return an array containing the nearest smaller element to the right for each element + */ public static int[] nearestSmallerToRight(int[] arr) { int n = arr.length; int[] result = new int[n]; - Stack indexStack = new Stack<>(); + Stack stack = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { - while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) { - indexStack.pop(); + while (!stack.isEmpty() && stack.peek() >= arr[i]) { + stack.pop(); } - result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; - indexStack.push(i); + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } return result; } + /** + * Finds the nearest smaller element to the left for each element in the array. + * + * @param arr the input array + * @return an array containing the nearest smaller element to the left for each element + */ public static int[] nearestSmallerToLeft(int[] arr) { int n = arr.length; int[] result = new int[n]; - Stack indexStack = new Stack<>(); + Stack stack = new Stack<>(); + for (int i = 0; i < n; i++) { - while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) { - indexStack.pop(); + while (!stack.isEmpty() && stack.peek() >= arr[i]) { + stack.pop(); } - result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; - indexStack.push(i); + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } return result; } - - public static void main(String[] args) { - int[] sampleArray = {4, 5, 2, 10, 8}; - System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(sampleArray))); - System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(sampleArray))); - System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(sampleArray))); - System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(sampleArray))); - } } diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 57fae10d5f96..de8cd8eb6d4c 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -1,56 +1,77 @@ package com.thealgorithms.stacks; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -public class NearestElementTest { +class NearestElementTest { - @Test - public void testNearestGreaterToRight() { - int[] arr = {4, 5, 2, 10, 8}; - int[] expected = {5, 10, 10, -1, -1}; - assertArrayEquals(expected, NearestElement.nearestGreaterToRight(arr)); + @ParameterizedTest + @MethodSource("provideGreaterToRightTestCases") + void testNearestGreaterToRight(int[] input, int[] expected) { + assertArrayEquals(expected, NearestElement.nearestGreaterToRight(input)); } - @Test - public void testNearestGreaterToLeft() { - int[] arr = {4, 5, 2, 10, 8}; - int[] expected = {-1, -1, 5, -1, 10}; - assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(arr)); + static Stream provideGreaterToRightTestCases() { + return Stream.of( + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) + ); } - @Test - public void testNearestSmallerToRight() { - int[] arr = {4, 5, 2, 10, 8}; - int[] expected = {2, 2, -1, 8, -1}; - assertArrayEquals(expected, NearestElement.nearestSmallerToRight(arr)); + @ParameterizedTest + @MethodSource("provideGreaterToLeftTestCases") + void testNearestGreaterToLeft(int[] input, int[] expected) { + assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(input)); } - @Test - public void testNearestSmallerToLeft() { - int[] arr = {4, 5, 2, 10, 8}; - int[] expected = {-1, 4, -1, 2, 2}; - assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr)); + static Stream provideGreaterToLeftTestCases() { + return Stream.of( + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) + ); } - @Test - void testEmptyArray() { - int[] arr = {}; - assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToRight(arr)); - assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToLeft(arr)); + @ParameterizedTest + @MethodSource("provideSmallerToRightTestCases") + void testNearestSmallerToRight(int[] input, int[] expected) { + assertArrayEquals(expected, NearestElement.nearestSmallerToRight(input)); } - @Test - void testAllEqualElements() { - int[] arr = {5, 5, 5, 5}; - assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToRight(arr)); - assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToLeft(arr)); + static Stream provideSmallerToRightTestCases() { + return Stream.of( + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) + ); + } + + @ParameterizedTest + @MethodSource("provideSmallerToLeftTestCases") + void testNearestSmallerToLeft(int[] input, int[] expected) { + assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(input)); + } + + static Stream provideSmallerToLeftTestCases() { + return Stream.of( + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) + ); } @Test - void testPrivateConstructor() throws Exception { - var constructor = NearestElement.class.getDeclaredConstructor(); - constructor.setAccessible(true); - constructor.newInstance(); + void testNullInput() { + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestGreaterToRight(null)); + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestGreaterToLeft(null)); + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToRight(null)); + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToLeft(null)); } } From 4c6f15a32e6a9cddce09399388e226c8350e462c Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 19:22:23 +0530 Subject: [PATCH 5/7] Fix NearestElement.java & add comprehensive tests for stack algorithms --- .../thealgorithms/stacks/NearestElement.java | 46 +++++++------------ .../stacks/NearestElementTest.java | 24 +++++----- 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java index 3724f37387bb..c5258b1ec192 100644 --- a/src/main/java/com/thealgorithms/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -3,25 +3,23 @@ import java.util.Stack; /** - * This class provides four algorithms to find the nearest greater or smaller elements - * on either side of each element in an array using a stack. + * Implementation of classic stack-based algorithms: + * 1. Nearest Greater to Right + * 2. Nearest Greater to Left + * 3. Nearest Smaller to Right + * 4. Nearest Smaller to Left * - *

These algorithms are fundamental examples of the monotonic stack approach, commonly - * used in competitive programming and technical interviews. Each method runs in O(n) - * time complexity and O(n) space complexity. + * These algorithms are fundamental for technical interviews and array-stack problems. */ public final class NearestElement { // Private constructor to prevent instantiation - private NearestElement() {} + private NearestElement() { + } - /** - * Finds the nearest greater element to the right for each element in the array. - * - * @param arr the input array - * @return an array containing the nearest greater element to the right for each element - */ public static int[] nearestGreaterToRight(int[] arr) { + if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + int n = arr.length; int[] result = new int[n]; Stack stack = new Stack<>(); @@ -36,13 +34,9 @@ public static int[] nearestGreaterToRight(int[] arr) { return result; } - /** - * Finds the nearest greater element to the left for each element in the array. - * - * @param arr the input array - * @return an array containing the nearest greater element to the left for each element - */ public static int[] nearestGreaterToLeft(int[] arr) { + if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + int n = arr.length; int[] result = new int[n]; Stack stack = new Stack<>(); @@ -57,13 +51,9 @@ public static int[] nearestGreaterToLeft(int[] arr) { return result; } - /** - * Finds the nearest smaller element to the right for each element in the array. - * - * @param arr the input array - * @return an array containing the nearest smaller element to the right for each element - */ public static int[] nearestSmallerToRight(int[] arr) { + if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + int n = arr.length; int[] result = new int[n]; Stack stack = new Stack<>(); @@ -78,13 +68,9 @@ public static int[] nearestSmallerToRight(int[] arr) { return result; } - /** - * Finds the nearest smaller element to the left for each element in the array. - * - * @param arr the input array - * @return an array containing the nearest smaller element to the left for each element - */ public static int[] nearestSmallerToLeft(int[] arr) { + if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + int n = arr.length; int[] result = new int[n]; Stack stack = new Stack<>(); diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index de8cd8eb6d4c..e7fbacdbd925 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -19,9 +19,9 @@ void testNearestGreaterToRight(int[] input, int[] expected) { static Stream provideGreaterToRightTestCases() { return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) ); } @@ -33,9 +33,9 @@ void testNearestGreaterToLeft(int[] input, int[] expected) { static Stream provideGreaterToLeftTestCases() { return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) ); } @@ -47,9 +47,9 @@ void testNearestSmallerToRight(int[] input, int[] expected) { static Stream provideSmallerToRightTestCases() { return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) ); } @@ -61,9 +61,9 @@ void testNearestSmallerToLeft(int[] input, int[] expected) { static Stream provideSmallerToLeftTestCases() { return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), + Arguments.of(new int[] {5}, new int[] {-1}), + Arguments.of(new int[] {}, new int[] {}) ); } From 6c7d8da6169a78124d26acfcf746159ea70baf40 Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 19:36:22 +0530 Subject: [PATCH 6/7] Fix NearestElement.java & add comprehensive tests and formatted with clang for stack algorithms --- .../thealgorithms/stacks/NearestElement.java | 36 +++++++++++++------ .../stacks/NearestElementTest.java | 26 +++----------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java index c5258b1ec192..0360e277556a 100644 --- a/src/main/java/com/thealgorithms/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -3,22 +3,23 @@ import java.util.Stack; /** - * Implementation of classic stack-based algorithms: + * Implements classic stack-based algorithms to find nearest elements. + * + * Algorithms included: * 1. Nearest Greater to Right * 2. Nearest Greater to Left * 3. Nearest Smaller to Right * 4. Nearest Smaller to Left - * - * These algorithms are fundamental for technical interviews and array-stack problems. */ public final class NearestElement { - // Private constructor to prevent instantiation - private NearestElement() { - } + private NearestElement() {} + /** Finds the nearest greater element to the right for each element in the array. */ public static int[] nearestGreaterToRight(int[] arr) { - if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + if (arr == null) { + throw new IllegalArgumentException("Input array cannot be null"); + } int n = arr.length; int[] result = new int[n]; @@ -31,11 +32,15 @@ public static int[] nearestGreaterToRight(int[] arr) { result[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(arr[i]); } + return result; } + /** Finds the nearest greater element to the left for each element in the array. */ public static int[] nearestGreaterToLeft(int[] arr) { - if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + if (arr == null) { + throw new IllegalArgumentException("Input array cannot be null"); + } int n = arr.length; int[] result = new int[n]; @@ -48,11 +53,15 @@ public static int[] nearestGreaterToLeft(int[] arr) { result[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(arr[i]); } + return result; } + /** Finds the nearest smaller element to the right for each element in the array. */ public static int[] nearestSmallerToRight(int[] arr) { - if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + if (arr == null) { + throw new IllegalArgumentException("Input array cannot be null"); + } int n = arr.length; int[] result = new int[n]; @@ -65,11 +74,15 @@ public static int[] nearestSmallerToRight(int[] arr) { result[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(arr[i]); } + return result; } + /** Finds the nearest smaller element to the left for each element in the array. */ public static int[] nearestSmallerToLeft(int[] arr) { - if (arr == null) throw new IllegalArgumentException("Input array cannot be null"); + if (arr == null) { + throw new IllegalArgumentException("Input array cannot be null"); + } int n = arr.length; int[] result = new int[n]; @@ -82,6 +95,7 @@ public static int[] nearestSmallerToLeft(int[] arr) { result[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(arr[i]); } + return result; } -} +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index e7fbacdbd925..ab24954267b0 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -18,11 +18,7 @@ void testNearestGreaterToRight(int[] input, int[] expected) { } static Stream provideGreaterToRightTestCases() { - return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) - ); + return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {})); } @ParameterizedTest @@ -32,11 +28,7 @@ void testNearestGreaterToLeft(int[] input, int[] expected) { } static Stream provideGreaterToLeftTestCases() { - return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) - ); + return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {})); } @ParameterizedTest @@ -46,11 +38,7 @@ void testNearestSmallerToRight(int[] input, int[] expected) { } static Stream provideSmallerToRightTestCases() { - return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) - ); + return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {})); } @ParameterizedTest @@ -60,11 +48,7 @@ void testNearestSmallerToLeft(int[] input, int[] expected) { } static Stream provideSmallerToLeftTestCases() { - return Stream.of( - Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), - Arguments.of(new int[] {5}, new int[] {-1}), - Arguments.of(new int[] {}, new int[] {}) - ); + return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {})); } @Test @@ -74,4 +58,4 @@ void testNullInput() { assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToRight(null)); assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToLeft(null)); } -} +} \ No newline at end of file From 52c4f7ab8b8bd35ba29f9bb7fe9848fa08fc5f4b Mon Sep 17 00:00:00 2001 From: sgindeed Date: Tue, 7 Oct 2025 22:42:51 +0530 Subject: [PATCH 7/7] Fix Checkstyle NeedBraces and format NearestElement.java --- src/main/java/com/thealgorithms/stacks/NearestElement.java | 5 +++-- .../java/com/thealgorithms/stacks/NearestElementTest.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java index 0360e277556a..a4a0854840b8 100644 --- a/src/main/java/com/thealgorithms/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -13,7 +13,8 @@ */ public final class NearestElement { // Private constructor to prevent instantiation - private NearestElement() {} + private NearestElement() { + } /** Finds the nearest greater element to the right for each element in the array. */ public static int[] nearestGreaterToRight(int[] arr) { @@ -98,4 +99,4 @@ public static int[] nearestSmallerToLeft(int[] arr) { return result; } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index ab24954267b0..817163cd1a8a 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -58,4 +58,4 @@ void testNullInput() { assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToRight(null)); assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToLeft(null)); } -} \ No newline at end of file +}