Skip to content

Commit af7c425

Browse files
alxkmalxkm
and
alxkm
authored
refactor: NextSmallerElement (#5412)
* refactor: NextSmallerElement * checkstyle: fix formatting * checkstyle: fix formatting * checkstyle: remove redundant new line --------- Co-authored-by: alxkm <[email protected]>
1 parent 0c8616e commit af7c425

File tree

2 files changed

+62
-46
lines changed

2 files changed

+62
-46
lines changed

src/main/java/com/thealgorithms/stacks/NextSmallerElement.java

+33-46
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,57 @@
33
import java.util.Arrays;
44
import java.util.Stack;
55

6-
/*
7-
Given an array "input" you need to print the first smaller element for each element to the left
8-
side of an array. For a given element x of an array, the Next Smaller element of that element is
9-
the first smaller element to the left side of it. If no such element is present print -1.
10-
11-
Example
12-
input = { 2, 7, 3, 5, 4, 6, 8 };
13-
At i = 0
14-
No elements to left of it : -1
15-
At i = 1
16-
Next smaller element between (0 , 0) is 2
17-
At i = 2
18-
Next smaller element between (0 , 1) is 2
19-
At i = 3
20-
Next smaller element between (0 , 2) is 3
21-
At i = 4
22-
Next smaller element between (0 , 3) is 3
23-
At i = 5
24-
Next smaller element between (0 , 4) is 4
25-
At i = 6
26-
Next smaller element between (0 , 5) is 6
27-
28-
result : [-1, 2, 2, 3, 3, 4, 6]
29-
30-
1) Create a new empty stack st
31-
32-
2) Iterate over array "input" , where "i" goes from 0 to input.length -1.
33-
a) We are looking for value just smaller than `input[i]`. So keep popping from "stack"
34-
till elements in "stack.peek() >= input[i]" or stack becomes empty.
35-
b) If the stack is non-empty, then the top element is our previous element. Else the
36-
previous element does not exist. c) push input[i] in stack. 3) If elements are left then their
37-
answer is -1
6+
/**
7+
* Utility class to find the next smaller element for each element in a given integer array.
8+
*
9+
* <p>The next smaller element for an element x is the first smaller element on the left side of x in the array.
10+
* If no such element exists, the result will contain -1 for that position.</p>
11+
*
12+
* <p>Example:</p>
13+
* <pre>
14+
* Input: {2, 7, 3, 5, 4, 6, 8}
15+
* Output: [-1, 2, 2, 3, 3, 4, 6]
16+
* </pre>
3817
*/
39-
4018
public final class NextSmallerElement {
4119
private NextSmallerElement() {
4220
}
4321

22+
/**
23+
* Finds the next smaller element for each element in the given array.
24+
*
25+
* @param array the input array of integers
26+
* @return an array where each element is replaced by the next smaller element on the left side in the input array,
27+
* or -1 if there is no smaller element.
28+
* @throws IllegalArgumentException if the input array is null
29+
*/
4430
public static int[] findNextSmallerElements(int[] array) {
45-
// base case
4631
if (array == null) {
47-
return array;
32+
throw new IllegalArgumentException("Input array cannot be null");
4833
}
49-
Stack<Integer> stack = new Stack<>();
34+
5035
int[] result = new int[array.length];
36+
Stack<Integer> stack = new Stack<>();
37+
38+
// Initialize all elements to -1 (in case there is no smaller element)
5139
Arrays.fill(result, -1);
5240

41+
// Traverse the array from left to right
5342
for (int i = 0; i < array.length; i++) {
54-
while (!stack.empty() && stack.peek() >= array[i]) {
43+
// Maintain the stack such that the top of the stack is the next smaller element
44+
while (!stack.isEmpty() && stack.peek() >= array[i]) {
5545
stack.pop();
5646
}
57-
if (stack.empty()) {
58-
result[i] = -1;
59-
} else {
47+
48+
// If stack is not empty, then the top is the next smaller element
49+
if (!stack.isEmpty()) {
6050
result[i] = stack.peek();
6151
}
52+
53+
// Push the current element onto the stack
6254
stack.push(array[i]);
6355
}
64-
return result;
65-
}
6656

67-
public static void main(String[] args) {
68-
int[] input = {2, 7, 3, 5, 4, 6, 8};
69-
int[] result = findNextSmallerElements(input);
70-
System.out.println(Arrays.toString(result));
57+
return result;
7158
}
7259
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
class NextSmallerElementTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
void testFindNextSmallerElements(int[] input, int[] expected) {
17+
assertArrayEquals(expected, NextSmallerElement.findNextSmallerElements(input));
18+
}
19+
20+
static Stream<Arguments> provideTestCases() {
21+
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}),
22+
Arguments.of(new int[] {4, 5, 2, 25}, new int[] {-1, 4, -1, 2}), Arguments.of(new int[] {}, new int[] {}));
23+
}
24+
25+
@Test
26+
void testFindNextSmallerElementsExceptions() {
27+
assertThrows(IllegalArgumentException.class, () -> NextSmallerElement.findNextSmallerElements(null));
28+
}
29+
}

0 commit comments

Comments
 (0)