-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0ea6558
Added LongestArthmeticSubsequence
tejaswi0910 45cedb7
Renamed the file
tejaswi0910 970008f
removed main method from the file
tejaswi0910 0fff273
Added Parameterized tests
tejaswi0910 f465550
formatted tests arguments
tejaswi0910 65c2f58
clang format changes
tejaswi0910 bd24402
Added exception for null case
tejaswi0910 eded33d
Added test for null case
tejaswi0910 f6cd843
clang format changes done
tejaswi0910 0a51008
Check fixes
tejaswi0910 e1a1fb4
clang format fixes
tejaswi0910 2b3ea12
fixes
tejaswi0910 79cf9cd
buildfailure fixes
tejaswi0910 d103e84
imported test class from junit
tejaswi0910 47cccfe
clang format changes
tejaswi0910 6244682
Merge branch 'master' into longestArthSeq
tejaswi0910 7cb9fd9
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 3be82b8
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 8657b3a
Added the original tests
tejaswi0910 2cd5151
Merge branch 'longestArthSeq' of https://github.com/tejaswi0910/Java …
tejaswi0910 677bbad
clang fixes
tejaswi0910 85a850b
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 b93f734
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 0951727
Update src/test/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 225307e
Update src/main/java/com/thealgorithms/dynamicprogramming/LongestArit…
tejaswi0910 d5c2fe3
Added constant sequence test
tejaswi0910 70ba32e
clang fixes
tejaswi0910 539bca8
clang fixes
tejaswi0910 d8c0e0c
Merge branch 'master' into longestArthSeq
tejaswi0910 0910171
Trigger GitHub Actions
tejaswi0910 cc9421f
Merge branch 'master' into longestArthSeq
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 <= 1) { | ||
return nums.length; | ||
} | ||
|
||
HashMap<Integer, Integer>[] dp = new HashMap[nums.length]; | ||
int maxLength = 2; | ||
|
||
// fill the 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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() { | ||
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), | ||
Arguments.of(new int[] {1, 2, 2, 2, 2, 5}, 4)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.