Skip to content

Adds Longest Arithmetic Subsequence Implementation #5501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0ea6558
Added LongestArthmeticSubsequence
tejaswi0910 Oct 1, 2024
45cedb7
Renamed the file
tejaswi0910 Oct 1, 2024
970008f
removed main method from the file
tejaswi0910 Oct 2, 2024
0fff273
Added Parameterized tests
tejaswi0910 Oct 2, 2024
f465550
formatted tests arguments
tejaswi0910 Oct 2, 2024
65c2f58
clang format changes
tejaswi0910 Oct 2, 2024
bd24402
Added exception for null case
tejaswi0910 Oct 2, 2024
eded33d
Added test for null case
tejaswi0910 Oct 2, 2024
f6cd843
clang format changes done
tejaswi0910 Oct 2, 2024
0a51008
Check fixes
tejaswi0910 Oct 2, 2024
e1a1fb4
clang format fixes
tejaswi0910 Oct 2, 2024
2b3ea12
fixes
tejaswi0910 Oct 2, 2024
79cf9cd
buildfailure fixes
tejaswi0910 Oct 2, 2024
d103e84
imported test class from junit
tejaswi0910 Oct 2, 2024
47cccfe
clang format changes
tejaswi0910 Oct 2, 2024
6244682
Merge branch 'master' into longestArthSeq
tejaswi0910 Oct 2, 2024
7cb9fd9
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 Oct 2, 2024
3be82b8
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 Oct 2, 2024
8657b3a
Added the original tests
tejaswi0910 Oct 2, 2024
2cd5151
Merge branch 'longestArthSeq' of https://github.com/tejaswi0910/Java …
tejaswi0910 Oct 2, 2024
677bbad
clang fixes
tejaswi0910 Oct 2, 2024
85a850b
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 Oct 3, 2024
b93f734
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 Oct 3, 2024
0951727
Update src/test/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 Oct 3, 2024
225307e
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 Oct 3, 2024
d5c2fe3
Added constant sequence test
tejaswi0910 Oct 3, 2024
70ba32e
clang fixes
tejaswi0910 Oct 3, 2024
539bca8
clang fixes
tejaswi0910 Oct 3, 2024
d8c0e0c
Merge branch 'master' into longestArthSeq
tejaswi0910 Oct 3, 2024
0910171
Trigger GitHub Actions
tejaswi0910 Oct 3, 2024
cc9421f
Merge branch 'master' into longestArthSeq
vil02 Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.thealgorithms.dynamicprogramming;

import java.util.HashMap;

final class LongestArithmeticSubsequence {

private LongestArithmeticSubsequence() {
}

/**
* Returns the length of the longest arithmetic subsequence in the given array.
*
* A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value
* (for 0 <= i < seq.length - 1).
*
* @param nums the input array of integers
* @return the length of the longest arithmetic subsequence
*/
public static int getLongestArithmeticSubsequenceLength(int[] nums) {
if (nums == null) {
throw new IllegalArgumentException("Input array cannot be null");
}

if (nums.length == 0) {
return 0;
}

if (nums.length == 1) {
return 1;
}

HashMap<Integer, Integer>[] dp = new HashMap[nums.length];
int maxLength = 2;

// Initialize dp array
for (int i = 0; i < nums.length; i++) {
dp[i] = new HashMap<>();
for (int j = 0; j < i; j++) {
final int diff = nums[i] - nums[j];
dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);
maxLength = Math.max(maxLength, dp[i].get(diff));
}
}

return maxLength;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class LongestArithmeticSubsequenceTest {
@ParameterizedTest
@MethodSource("provideTestCases")
void testGetLongestArithmeticSubsequenceLength(int[] nums, int expected) {
assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums));
}
@ParameterizedTest
@MethodSource("provideTestCases")
void testGetLongestArithmeticSubsequenceLengthReversedInput(int[] nums, int expected) {
ArrayUtils.reverse(nums);
assertEquals(expected, LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums));
}

@Test
void testNullInput() {
// Verify that an IllegalArgumentException is thrown when nums is null
assertThrows(IllegalArgumentException.class, () -> LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(null));
}

private static Stream<Arguments> provideTestCases() {
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));
}
}