|
3 | 3 | import java.util.Arrays;
|
4 | 4 | import java.util.Stack;
|
5 | 5 |
|
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> |
38 | 17 | */
|
39 |
| - |
40 | 18 | public final class NextSmallerElement {
|
41 | 19 | private NextSmallerElement() {
|
42 | 20 | }
|
43 | 21 |
|
| 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 | + */ |
44 | 30 | public static int[] findNextSmallerElements(int[] array) {
|
45 |
| - // base case |
46 | 31 | if (array == null) {
|
47 |
| - return array; |
| 32 | + throw new IllegalArgumentException("Input array cannot be null"); |
48 | 33 | }
|
49 |
| - Stack<Integer> stack = new Stack<>(); |
| 34 | + |
50 | 35 | 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) |
51 | 39 | Arrays.fill(result, -1);
|
52 | 40 |
|
| 41 | + // Traverse the array from left to right |
53 | 42 | 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]) { |
55 | 45 | stack.pop();
|
56 | 46 | }
|
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()) { |
60 | 50 | result[i] = stack.peek();
|
61 | 51 | }
|
| 52 | + |
| 53 | + // Push the current element onto the stack |
62 | 54 | stack.push(array[i]);
|
63 | 55 | }
|
64 |
| - return result; |
65 |
| - } |
66 | 56 |
|
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; |
71 | 58 | }
|
72 | 59 | }
|
0 commit comments