From cddd83b6b54ff34c33bc1efc3cadebe54c734bac Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 18:13:41 +0530 Subject: [PATCH 1/7] Add print statements to all NearestElementTest methods --- .../datastructures/stacks/NearestElement.java | 75 +++++++++++++++++++ .../stacks/NearestElementTest.java | 47 ++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java create mode 100644 src/test/java/com/thealgorithms/stacks/NearestElementTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java b/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java new file mode 100644 index 000000000000..6d06219573ea --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java @@ -0,0 +1,75 @@ +package datastructures.stacks; + +import java.util.Arrays; +import java.util.Stack; + +/** + * The {@code NearestElement} class provides static utility methods to find the nearest greater or smaller elements + * to the left or right of each element in an integer array using stack-based algorithms. + * + *

Each method runs in O(n) time complexity by maintaining a monotonic stack: + *

+ * If no such element exists for a position, -1 is returned at that index. + */ +public class NearestElement { + public static int[] nearestGreaterToRight(int[] arr) { + int n = arr.length; + int[] res = new int[n]; + Stack st = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { + while (!st.isEmpty() && st.peek() <= arr[i]) st.pop(); + res[i] = st.isEmpty() ? -1 : st.peek(); + st.push(arr[i]); + } + return res; + } + + public static int[] nearestGreaterToLeft(int[] arr) { + int n = arr.length; + int[] res = new int[n]; + Stack st = new Stack<>(); + for (int i = 0; i < n; i++) { + while (!st.isEmpty() && st.peek() <= arr[i]) st.pop(); + res[i] = st.isEmpty() ? -1 : st.peek(); + st.push(arr[i]); + } + return res; + } + + public static int[] nearestSmallerToRight(int[] arr) { + int n = arr.length; + int[] res = new int[n]; + Stack st = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { + while (!st.isEmpty() && st.peek() >= arr[i]) st.pop(); + res[i] = st.isEmpty() ? -1 : st.peek(); + st.push(arr[i]); + } + return res; + } + + public static int[] nearestSmallerToLeft(int[] arr) { + int n = arr.length; + int[] res = new int[n]; + Stack st = new Stack<>(); + for (int i = 0; i < n; i++) { + while (!st.isEmpty() && st.peek() >= arr[i]) st.pop(); + res[i] = st.isEmpty() ? -1 : st.peek(); + st.push(arr[i]); + } + return res; + } + + public static void main(String[] args) { + int[] arr = {4, 5, 2, 10, 8}; + System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(arr))); + System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(arr))); + System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(arr))); + System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(arr))); + } +} 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..7a71d7aa2808 --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -0,0 +1,47 @@ +package com.thealgorithms.stacks; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + +import datastructures.stacks.NearestElement; + +class NearestElementTest { + + @Test + void testNearestGreaterToRight() { + int[] arr = {4, 5, 2, 10, 8}; + int[] result = NearestElement.nearestGreaterToRight(arr); + System.out.println("nearestGreaterToRight: " + Arrays.toString(result)); + int[] expected = {5, 10, 10, -1, -1}; + assertArrayEquals(expected, result); + } + + @Test + void testNearestGreaterToLeft() { + int[] arr = {4, 5, 2, 10, 8}; + int[] result = NearestElement.nearestGreaterToLeft(arr); + System.out.println("nearestGreaterToLeft: " + Arrays.toString(result)); + int[] expected = {-1, -1, 5, -1, 10}; + assertArrayEquals(expected, result); + } + + @Test + void testNearestSmallerToRight() { + int[] arr = {4, 5, 2, 10, 8}; + int[] result = NearestElement.nearestSmallerToRight(arr); + System.out.println("nearestSmallerToRight: " + Arrays.toString(result)); + int[] expected = {2, 2, -1, 8, -1}; + assertArrayEquals(expected, result); + } + + @Test + void testNearestSmallerToLeft() { + int[] arr = {4, 5, 2, 10, 8}; + int[] result = NearestElement.nearestSmallerToLeft(arr); + System.out.println("nearestSmallerToLeft: " + Arrays.toString(result)); + int[] expected = {-1, 4, -1, 2, 2}; + assertArrayEquals(expected, result); + } +} From c5c31c80b56273698f72cb027bef3d4fedc2b020 Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 18:43:08 +0530 Subject: [PATCH 2/7] fixed checkstyle violation in NearestElement.java --- .../datastructures/stacks/NearestElement.java | 77 ++++++++++--------- .../stacks/NearestElementTest.java | 2 +- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java b/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java index 6d06219573ea..ea0c434b6678 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java @@ -1,12 +1,11 @@ -package datastructures.stacks; +package com.thealgorithms.datastructures.stacks; -import java.util.Arrays; import java.util.Stack; /** * The {@code NearestElement} class provides static utility methods to find the nearest greater or smaller elements * to the left or right of each element in an integer array using stack-based algorithms. - * + * *

Each method runs in O(n) time complexity by maintaining a monotonic stack: *

    *
  • {@code nearestGreaterToRight}: Finds the nearest greater element to the right of each element.
  • @@ -14,62 +13,66 @@ *
  • {@code nearestSmallerToRight}: Finds the nearest smaller element to the right of each element.
  • *
  • {@code nearestSmallerToLeft}: Finds the nearest smaller element to the left of each element.
  • *
- * If no such element exists for a position, -1 is returned at that index. */ -public class NearestElement { +public final class NearestElement { + + private NearestElement() { + throw new UnsupportedOperationException("Utility class"); + } + public static int[] nearestGreaterToRight(int[] arr) { int n = arr.length; - int[] res = new int[n]; - Stack st = new Stack<>(); + int[] result = new int[n]; + Stack stack = new Stack<>(); for (int i = n - 1; i >= 0; i--) { - while (!st.isEmpty() && st.peek() <= arr[i]) st.pop(); - res[i] = st.isEmpty() ? -1 : st.peek(); - st.push(arr[i]); + while (!stack.isEmpty() && stack.peek() <= arr[i]) { + stack.pop(); + } + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } - return res; + return result; } public static int[] nearestGreaterToLeft(int[] arr) { int n = arr.length; - int[] res = new int[n]; - Stack st = new Stack<>(); + int[] result = new int[n]; + Stack stack = new Stack<>(); for (int i = 0; i < n; i++) { - while (!st.isEmpty() && st.peek() <= arr[i]) st.pop(); - res[i] = st.isEmpty() ? -1 : st.peek(); - st.push(arr[i]); + while (!stack.isEmpty() && stack.peek() <= arr[i]) { + stack.pop(); + } + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } - return res; + return result; } public static int[] nearestSmallerToRight(int[] arr) { int n = arr.length; - int[] res = new int[n]; - Stack st = new Stack<>(); + int[] result = new int[n]; + Stack stack = new Stack<>(); for (int i = n - 1; i >= 0; i--) { - while (!st.isEmpty() && st.peek() >= arr[i]) st.pop(); - res[i] = st.isEmpty() ? -1 : st.peek(); - st.push(arr[i]); + while (!stack.isEmpty() && stack.peek() >= arr[i]) { + stack.pop(); + } + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } - return res; + return result; } public static int[] nearestSmallerToLeft(int[] arr) { int n = arr.length; - int[] res = new int[n]; - Stack st = new Stack<>(); + int[] result = new int[n]; + Stack stack = new Stack<>(); for (int i = 0; i < n; i++) { - while (!st.isEmpty() && st.peek() >= arr[i]) st.pop(); - res[i] = st.isEmpty() ? -1 : st.peek(); - st.push(arr[i]); + while (!stack.isEmpty() && stack.peek() >= arr[i]) { + stack.pop(); + } + result[i] = stack.isEmpty() ? -1 : stack.peek(); + stack.push(arr[i]); } - return res; - } - - public static void main(String[] args) { - int[] arr = {4, 5, 2, 10, 8}; - System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(arr))); - System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(arr))); - System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(arr))); - System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(arr))); + return result; } } diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 7a71d7aa2808..1625e5064ec9 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.Test; -import datastructures.stacks.NearestElement; +import com.thealgorithms.datastructures.stacks.NearestElement; class NearestElementTest { From 0ed6d3b885331d5fe84b5d48365abd26e98af960 Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 18:55:20 +0530 Subject: [PATCH 3/7] fixed checkstyle violation in NearestElementTest.java --- .../thealgorithms/stacks/NearestElementTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 1625e5064ec9..2c274ed99102 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -1,5 +1,6 @@ package com.thealgorithms.stacks; +import java.lang.reflect.Constructor; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -44,4 +45,16 @@ void testNearestSmallerToLeft() { int[] expected = {-1, 4, -1, 2, 2}; assertArrayEquals(expected, result); } + + @Test + void testPrivateConstructor() throws Exception { + Constructor constructor + = NearestElement.class.getDeclaredConstructor(); + constructor.setAccessible(true); + try { + constructor.newInstance(); + } catch (Exception ignored) { + // Expected exception: constructor throws UnsupportedOperationException + } + } } From c715675634ebe5a0da8959608ec1c60fd96e2d49 Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 18:59:18 +0530 Subject: [PATCH 4/7] fixed checkstyle violation in NearestElementTest.java --- .../java/com/thealgorithms/stacks/NearestElementTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 2c274ed99102..39131a1766d8 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -48,8 +48,8 @@ void testNearestSmallerToLeft() { @Test void testPrivateConstructor() throws Exception { - Constructor constructor - = NearestElement.class.getDeclaredConstructor(); + Constructor constructor = + NearestElement.class.getDeclaredConstructor(); constructor.setAccessible(true); try { constructor.newInstance(); From 4dd48856d6ffec5d1b884313a602edc9775659ca Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 19:10:17 +0530 Subject: [PATCH 5/7] fixed checkstyle violation in NearestElementTest.java --- .../datastructures/stacks/NearestElement.java | 20 ++++++++++++------- .../stacks/NearestElementTest.java | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java b/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java index ea0c434b6678..8fe8c2821ccb 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/NearestElement.java @@ -3,15 +3,21 @@ import java.util.Stack; /** - * The {@code NearestElement} class provides static utility methods to find the nearest greater or smaller elements - * to the left or right of each element in an integer array using stack-based algorithms. + * The {@code NearestElement} class provides static utility methods to find the + * nearest greater or smaller elements to the left or right of each element in + * an integer array using stack-based algorithms. * - *

Each method runs in O(n) time complexity by maintaining a monotonic stack: + *

+ * Each method runs in O(n) time complexity by maintaining a monotonic stack: *

    - *
  • {@code nearestGreaterToRight}: Finds the nearest greater element to the right of each element.
  • - *
  • {@code nearestGreaterToLeft}: Finds the nearest greater element to the left of each element.
  • - *
  • {@code nearestSmallerToRight}: Finds the nearest smaller element to the right of each element.
  • - *
  • {@code nearestSmallerToLeft}: Finds the nearest smaller element to the left of each element.
  • + *
  • {@code nearestGreaterToRight}: Finds the nearest greater element to the + * right of each element.
  • + *
  • {@code nearestGreaterToLeft}: Finds the nearest greater element to the + * left of each element.
  • + *
  • {@code nearestSmallerToRight}: Finds the nearest smaller element to the + * right of each element.
  • + *
  • {@code nearestSmallerToLeft}: Finds the nearest smaller element to the + * left of each element.
  • *
*/ public final class NearestElement { diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 39131a1766d8..2c274ed99102 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -48,8 +48,8 @@ void testNearestSmallerToLeft() { @Test void testPrivateConstructor() throws Exception { - Constructor constructor = - NearestElement.class.getDeclaredConstructor(); + Constructor constructor + = NearestElement.class.getDeclaredConstructor(); constructor.setAccessible(true); try { constructor.newInstance(); From e4df1cf70442cb94e69ebe42df3a4e764c805918 Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 19:17:09 +0530 Subject: [PATCH 6/7] fixed checkstyle violation in NearestElementTest.java --- .../java/com/thealgorithms/stacks/NearestElementTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 2c274ed99102..092854c39f74 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -48,13 +48,11 @@ void testNearestSmallerToLeft() { @Test void testPrivateConstructor() throws Exception { - Constructor constructor - = NearestElement.class.getDeclaredConstructor(); + Constructor constructor = NearestElement.class.getDeclaredConstructor(); constructor.setAccessible(true); try { constructor.newInstance(); } catch (Exception ignored) { - // Expected exception: constructor throws UnsupportedOperationException } } } From 3daf78e5ac0f664462de8daf603ea656250ad9a4 Mon Sep 17 00:00:00 2001 From: Sahilagarwal623 Date: Sun, 5 Oct 2025 19:19:55 +0530 Subject: [PATCH 7/7] fixed checkstyle violation in NearestElementTest.java --- src/test/java/com/thealgorithms/stacks/NearestElementTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java index 092854c39f74..e6b30e826883 100644 --- a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -8,6 +8,7 @@ import com.thealgorithms.datastructures.stacks.NearestElement; + class NearestElementTest { @Test