Skip to content

Commit c22f6b0

Browse files
authored
Merge branch 'master' into refactor/ReverseString
2 parents 448cb29 + 0c8616e commit c22f6b0

File tree

4 files changed

+58
-69
lines changed

4 files changed

+58
-69
lines changed
Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,34 @@
11
package com.thealgorithms.stacks;
22

3-
import java.util.Arrays;
43
import java.util.Stack;
54

6-
/*
7-
Given an array "input" you need to print the first greater element for each element.
8-
For a given element x of an array, the Next greater element of that element is the
9-
first greater element to the right 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-
Next greater element between (1 to n) is 7
15-
At i = 1
16-
Next greater element between (2 to n) is 8
17-
At i = 2
18-
Next greater element between (3 to n) is 5
19-
At i = 3
20-
Next greater element between (4 to n) is 6
21-
At i = 4
22-
Next greater element between (5 to n) is 6
23-
At i = 5
24-
Next greater element between (6 to n) is 8
25-
At i = 6
26-
Next greater element between (6 to n) is -1
27-
28-
result : [7, 8, 5, 6, 6, 8, -1]
29-
30-
1. If the stack is empty Push an element in the stack.
31-
2. If the stack is not empty:
32-
a. compare the top element of the stack with next.
33-
b. If next is greater than the top element, Pop element from the stack.
34-
next is the next greater element for the popped element.
35-
c. Keep popping from the stack while the popped element is smaller
36-
than next. next becomes the next greater element for all such
37-
popped elements.
38-
d. Finally, push the next in the stack.
39-
40-
3. If elements are left in stack after completing while loop then their Next greater element is
41-
-1.
5+
/**
6+
* Utility class to find the next greater element for each element in a given integer array.
7+
*
8+
* <p>The next greater element for an element x is the first greater element on the right side of x in the array.
9+
* If no such element exists, the result will contain 0 for that position.</p>
10+
*
11+
* <p>Example:</p>
12+
* <pre>
13+
* Input: {2, 7, 3, 5, 4, 6, 8}
14+
* Output: {7, 0, 5, 6, 6, 8, 0}
15+
* </pre>
4216
*/
43-
4417
public final class NextGreaterElement {
4518
private NextGreaterElement() {
4619
}
4720

21+
/**
22+
* Finds the next greater element for each element in the given array.
23+
*
24+
* @param array the input array of integers
25+
* @return an array where each element is replaced by the next greater element on the right side in the input array,
26+
* or 0 if there is no greater element.
27+
* @throws IllegalArgumentException if the input array is null
28+
*/
4829
public static int[] findNextGreaterElements(int[] array) {
4930
if (array == null) {
50-
return array;
31+
throw new IllegalArgumentException("Input array cannot be null");
5132
}
5233

5334
int[] result = new int[array.length];
@@ -62,10 +43,4 @@ public static int[] findNextGreaterElements(int[] array) {
6243

6344
return result;
6445
}
65-
66-
public static void main(String[] args) {
67-
int[] input = {2, 7, 3, 5, 4, 6, 8};
68-
int[] result = findNextGreaterElements(input);
69-
System.out.println(Arrays.toString(result));
70-
}
7146
}

src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/**
44
* Reverse String using Recursion
55
*/
6-
76
public final class ReverseStringRecursive {
87
private ReverseStringRecursive() {
98
}
9+
1010
/**
1111
* @param str string to be reversed
1212
* @return reversed string
Lines changed: 29 additions & 0 deletions
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 NextGreaterElementTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
void testFindNextGreaterElements(int[] input, int[] expected) {
17+
assertArrayEquals(expected, NextGreaterElement.findNextGreaterElements(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[] {7, 8, 5, 6, 6, 8, 0}), Arguments.of(new int[] {5}, new int[] {0}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {2, 3, 4, 5, 0}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {0, 0, 0, 0, 0}),
22+
Arguments.of(new int[] {4, 5, 2, 25}, new int[] {5, 25, 25, 0}), Arguments.of(new int[] {}, new int[] {}));
23+
}
24+
25+
@Test
26+
void testNullInput() {
27+
assertThrows(IllegalArgumentException.class, () -> NextGreaterElement.findNextGreaterElements(null));
28+
}
29+
}

src/test/java/com/thealgorithms/strings/ReverseStringRecursiveTest.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class ReverseStringRecursiveTest {
8-
@Test
9-
void shouldAcceptWhenEmptyStringIsPassed() {
10-
String expected = "";
11-
String reversed = ReverseStringRecursive.reverse("");
12-
13-
assertEquals(expected, reversed);
14-
}
15-
16-
@Test
17-
void shouldAcceptNotWhenWhenSingleCharacterIsPassed() {
18-
String expected = "a";
19-
String reversed = ReverseStringRecursive.reverse("a");
20-
21-
assertEquals(expected, reversed);
22-
}
23-
24-
@Test
25-
void shouldAcceptWhenStringIsPassed() {
26-
String expected = "dlroWolleH";
27-
String reversed = ReverseStringRecursive.reverse("HelloWorld");
28-
29-
assertEquals(expected, reversed);
9+
@ParameterizedTest
10+
@CsvSource({"'Hello World', 'dlroW olleH'", "'helloworld', 'dlrowolleh'", "'123456789', '987654321'", "'', ''", "'A', 'A'", "'!123 ABC xyz!', '!zyx CBA 321!'", "'Abc 123 Xyz', 'zyX 321 cbA'", "'12.34,56;78:90', '09:87;65,43.21'", "'abcdEFGHiJKL', 'LKJiHGFEdcba'",
11+
"'MixOf123AndText!', '!txeTdnA321fOxiM'"})
12+
public void
13+
testReverseString(String input, String expectedOutput) {
14+
assertEquals(expectedOutput, ReverseStringRecursive.reverse(input));
3015
}
3116
}

0 commit comments

Comments
 (0)