Skip to content

Commit 6edc009

Browse files
authored
test: LongestAlternatingSubsequenceTest (#5399)
1 parent 93e4175 commit 6edc009

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
/*
4-
5-
* Problem Statement: -
6-
* Find Longest Alternating Subsequence
7-
8-
* A sequence {x1, x2, .. xn} is alternating sequence if its elements satisfy one of the following
9-
relations :
10-
11-
x1 < x2 > x3 < x4 > x5 < …. xn or
12-
x1 > x2 < x3 > x4 < x5 > …. xn
3+
/**
4+
* Class for finding the length of the longest alternating subsequence in an array.
5+
*
6+
* <p>An alternating sequence is a sequence of numbers where the elements alternate
7+
* between increasing and decreasing. Specifically, a sequence is alternating if its elements
8+
* satisfy one of the following relations:
9+
*
10+
* <ul>
11+
* <li>{@code x1 < x2 > x3 < x4 > x5 < ... < xn}</li>
12+
* <li>{@code x1 > x2 < x3 > x4 < x5 > ... > xn}</li>
13+
* </ul>
14+
*
15+
* <p>This class provides a method to compute the length of the longest such subsequence
16+
* from a given array of integers.
1317
*/
1418
public final class LongestAlternatingSubsequence {
1519
private LongestAlternatingSubsequence() {
1620
}
1721

18-
/* Function to return longest alternating subsequence length*/
22+
/**
23+
* Finds the length of the longest alternating subsequence in the given array.
24+
*
25+
* @param arr an array of integers where the longest alternating subsequence is to be found
26+
* @param n the length of the array {@code arr}
27+
* @return the length of the longest alternating subsequence
28+
*
29+
* <p>The method uses dynamic programming to solve the problem. It maintains a 2D array
30+
* {@code las} where:
31+
* <ul>
32+
* <li>{@code las[i][0]} represents the length of the longest alternating subsequence
33+
* ending at index {@code i} with the last element being greater than the previous element.</li>
34+
* <li>{@code las[i][1]} represents the length of the longest alternating subsequence
35+
* ending at index {@code i} with the last element being smaller than the previous element.</li>
36+
* </ul>
37+
*
38+
* <p>The method iterates through the array and updates the {@code las} array based on
39+
* whether the current element is greater or smaller than the previous elements.
40+
* The result is the maximum value found in the {@code las} array.
41+
*/
1942
static int alternatingLength(int[] arr, int n) {
20-
/*
21-
22-
las[i][0] = Length of the longest
23-
alternating subsequence ending at
24-
index i and last element is
25-
greater than its previous element
26-
27-
las[i][1] = Length of the longest
28-
alternating subsequence ending at
29-
index i and last element is
30-
smaller than its previous
31-
element
32-
33-
*/
3443
int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence
3544

45+
// Initialize the dp array
3646
for (int i = 0; i < n; i++) {
3747
las[i][0] = 1;
3848
las[i][1] = 1;
3949
}
4050

4151
int result = 1; // Initialize result
4252

43-
/* Compute values in bottom up manner */
53+
// Compute values in a bottom-up manner
4454
for (int i = 1; i < n; i++) {
45-
/* Consider all elements as previous of arr[i]*/
4655
for (int j = 0; j < i; j++) {
47-
/* If arr[i] is greater, then check with las[j][1] */
56+
// If arr[i] is greater than arr[j], update las[i][0]
4857
if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) {
4958
las[i][0] = las[j][1] + 1;
5059
}
5160

52-
/* If arr[i] is smaller, then check with las[j][0]*/
61+
// If arr[i] is smaller than arr[j], update las[i][1]
5362
if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) {
5463
las[i][1] = las[j][0] + 1;
5564
}
5665
}
5766

58-
/* Pick maximum of both values at index i */
59-
if (result < Math.max(las[i][0], las[i][1])) {
60-
result = Math.max(las[i][0], las[i][1]);
61-
}
67+
// Pick the maximum of both values at index i
68+
result = Math.max(result, Math.max(las[i][0], las[i][1]));
6269
}
6370

6471
return result;
6572
}
66-
67-
public static void main(String[] args) {
68-
int[] arr = {10, 22, 9, 33, 49, 50, 31, 60};
69-
int n = arr.length;
70-
System.out.println("Length of Longest "
71-
+ "alternating subsequence is " + alternatingLength(arr, n));
72-
}
7373
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class LongestAlternatingSubsequenceTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
void testAlternatingLength(int[] arr, int expected) {
15+
assertEquals(expected, LongestAlternatingSubsequence.alternatingLength(arr, arr.length));
16+
}
17+
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(Arguments.of(new int[] {1}, 1), Arguments.of(new int[] {1, 2}, 2), Arguments.of(new int[] {2, 1}, 2), Arguments.of(new int[] {1, 3, 2, 4, 3, 5}, 6), Arguments.of(new int[] {1, 2, 3, 4, 5}, 2), Arguments.of(new int[] {5, 4, 3, 2, 1}, 2),
20+
Arguments.of(new int[] {10, 22, 9, 33, 49, 50, 31, 60}, 6), Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2));
21+
}
22+
}

0 commit comments

Comments
 (0)