Repository: TheAlgorithms/Java
Pull request #5412 (2024-08-26 21:59:04)

refactor: `NextSmallerElement`
Class cleanup.
Readability improvements.
Added tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5412?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 48.94%. Comparing base [(`c5b73ec`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/c5b73ec742d0522d65331ee02a143f54041845ed?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`397ee61`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/397ee613ddb0e2103b3faf1fd6a24e4eee7e0de9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5412      +/-   ##
============================================
+ Coverage     48.85%   48.94%   +0.08%     
- Complexity     3039     3044       +5     
============================================
  Files           524      524              
  Lines         15256    15251       -5     
  Branches       2900     2900              
============================================
+ Hits           7454     7465      +11     
+ Misses         7494     7477      -17     
- Partials        308      309       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5412?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java b/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java
index 6eae06ad..254bb3b4 100644
--- a/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java
+++ b/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java
@@ -3,70 +3,57 @@ package com.thealgorithms.stacks;
 import java.util.Arrays;
 import java.util.Stack;
 
-/*
-    Given an array "input" you need to print the first smaller element for each element to the left
-   side of an array. For a given element x of an array, the Next Smaller element of that element is
-   the first smaller element to the left side of it. If no such element is present print -1.
-
-    Example
-    input = { 2, 7, 3, 5, 4, 6, 8 };
-    At i = 0
-    No elements to left of it : -1
-    At i = 1
-    Next smaller element between (0 , 0) is 2
-    At i = 2
-    Next smaller element between (0 , 1) is 2
-    At i = 3
-    Next smaller element between (0 , 2) is 3
-    At i = 4
-    Next smaller element between (0 , 3) is 3
-    At i = 5
-    Next smaller element between (0 , 4) is 4
-    At i = 6
-    Next smaller element between (0 , 5) is 6
-
-    result : [-1, 2, 2, 3, 3, 4, 6]
-
-    1) Create a new empty stack st
-
-    2) Iterate over array "input" , where "i"  goes from 0 to input.length -1.
-        a) We are looking for value just smaller than `input[i]`. So keep popping from "stack"
-           till elements in "stack.peek() >= input[i]" or stack becomes empty.
-        b) If the stack is non-empty, then the top element is our previous element. Else the
-   previous element does not exist. c) push input[i] in stack. 3) If elements are left then their
-   answer is -1
+/**
+ * Utility class to find the next smaller element for each element in a given integer array.
+ *
+ * <p>The next smaller element for an element x is the first smaller element on the left side of x in the array.
+ * If no such element exists, the result will contain -1 for that position.</p>
+ *
+ * <p>Example:</p>
+ * <pre>
+ * Input:  {2, 7, 3, 5, 4, 6, 8}
+ * Output: [-1, 2, 2, 3, 3, 4, 6]
+ * </pre>
  */
-
 public final class NextSmallerElement {
     private NextSmallerElement() {
     }
 
+    /**
+     * Finds the next smaller element for each element in the given array.
+     *
+     * @param array the input array of integers
+     * @return an array where each element is replaced by the next smaller element on the left side in the input array,
+     * or -1 if there is no smaller element.
+     * @throws IllegalArgumentException if the input array is null
+     */
     public static int[] findNextSmallerElements(int[] array) {
-        // base case
         if (array == null) {
-            return array;
+            throw new IllegalArgumentException("Input array cannot be null");
         }
-        Stack<Integer> stack = new Stack<>();
+
         int[] result = new int[array.length];
+        Stack<Integer> stack = new Stack<>();
+
+        // Initialize all elements to -1 (in case there is no smaller element)
         Arrays.fill(result, -1);
 
+        // Traverse the array from left to right
         for (int i = 0; i < array.length; i++) {
-            while (!stack.empty() && stack.peek() >= array[i]) {
+            // Maintain the stack such that the top of the stack is the next smaller element
+            while (!stack.isEmpty() && stack.peek() >= array[i]) {
                 stack.pop();
             }
-            if (stack.empty()) {
-                result[i] = -1;
-            } else {
+
+            // If stack is not empty, then the top is the next smaller element
+            if (!stack.isEmpty()) {
                 result[i] = stack.peek();
             }
+
+            // Push the current element onto the stack
             stack.push(array[i]);
         }
-        return result;
-    }
 
-    public static void main(String[] args) {
-        int[] input = {2, 7, 3, 5, 4, 6, 8};
-        int[] result = findNextSmallerElements(input);
-        System.out.println(Arrays.toString(result));
+        return result;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java b/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java
new file mode 100644
index 00000000..62578baa
--- /dev/null
+++ b/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java
@@ -0,0 +1,29 @@
+package com.thealgorithms.stacks;
+
+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;
+
+class NextSmallerElementTest {
+
+    @ParameterizedTest
+    @MethodSource("provideTestCases")
+    void testFindNextSmallerElements(int[] input, int[] expected) {
+        assertArrayEquals(expected, NextSmallerElement.findNextSmallerElements(input));
+    }
+
+    static Stream<Arguments> provideTestCases() {
+        return Stream.of(Arguments.of(new int[] {2, 7, 3, 5, 4, 6, 8}, new int[] {-1, 2, 2, 3, 3, 4, 6}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {-1, 1, 2, 3, 4}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {-1, -1, -1, -1, -1}),
+            Arguments.of(new int[] {4, 5, 2, 25}, new int[] {-1, 4, -1, 2}), Arguments.of(new int[] {}, new int[] {}));
+    }
+
+    @Test
+    void testFindNextSmallerElementsExceptions() {
+        assertThrows(IllegalArgumentException.class, () -> NextSmallerElement.findNextSmallerElements(null));
+    }
+}