Skip to content

Commit e7e6fba

Browse files
authored
Merge branch 'master' into feature
2 parents 57964cd + be8df21 commit e7e6fba

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import java.util.HashMap;
4+
5+
final class LongestArithmeticSubsequence {
6+
private LongestArithmeticSubsequence() {
7+
}
8+
9+
/**
10+
* Returns the length of the longest arithmetic subsequence in the given array.
11+
*
12+
* A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value
13+
* (for 0 <= i < seq.length - 1).
14+
*
15+
* @param nums the input array of integers
16+
* @return the length of the longest arithmetic subsequence
17+
*/
18+
public static int getLongestArithmeticSubsequenceLength(int[] nums) {
19+
if (nums == null) {
20+
throw new IllegalArgumentException("Input array cannot be null");
21+
}
22+
23+
if (nums.length <= 1) {
24+
return nums.length;
25+
}
26+
27+
HashMap<Integer, Integer>[] dp = new HashMap[nums.length];
28+
int maxLength = 2;
29+
30+
// fill the dp array
31+
for (int i = 0; i < nums.length; i++) {
32+
dp[i] = new HashMap<>();
33+
for (int j = 0; j < i; j++) {
34+
final int diff = nums[i] - nums[j];
35+
dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);
36+
maxLength = Math.max(maxLength, dp[i].get(diff));
37+
}
38+
}
39+
40+
return maxLength;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.apache.commons.lang3.ArrayUtils;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
public class LongestArithmeticSubsequenceTest {
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) {
17+
assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums));
18+
}
19+
@ParameterizedTest
20+
@MethodSource("provideTestCases")
21+
void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expected) {
22+
ArrayUtils.reverse(nums);
23+
assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums));
24+
}
25+
26+
@Test
27+
void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() {
28+
assertThrows(IllegalArgumentException.class, () -> LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(null));
29+
}
30+
31+
private static Stream<Arguments> provideTestCases() {
32+
return Stream.of(Arguments.of(new int[] {3, 6, 9, 12, 15}, 5), Arguments.of(new int[] {1, 7, 10, 13, 14, 19}, 4), Arguments.of(new int[] {1, 2, 3, 4}, 4), Arguments.of(new int[] {}, 0), Arguments.of(new int[] {10}, 1), Arguments.of(new int[] {9, 4, 7, 2, 10}, 3),
33+
Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4));
34+
}
35+
}

0 commit comments

Comments
 (0)