-
Notifications
You must be signed in to change notification settings - Fork 19.1k
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
Adds Longest Arithmetic Subsequence Implementation #5501
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5501 +/- ##
============================================
+ Coverage 52.29% 52.34% +0.04%
- Complexity 3257 3263 +6
============================================
Files 525 526 +1
Lines 15185 15200 +15
Branches 2887 2891 +4
============================================
+ Hits 7941 7956 +15
Misses 6920 6920
Partials 324 324 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the main method and add proper junit tests instead. Prefer ParameterizedTests.
sure @vil02 , removed the main function and added tests! |
src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java
Show resolved
Hide resolved
src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java
Outdated
Show resolved
Hide resolved
…hmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
…hmeticSubsequence.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
| if (nums.length == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (nums.length == 1) { | ||
| return 1; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (nums.length == 0) { | |
| return 0; | |
| } | |
| if (nums.length == 1) { | |
| return 1; | |
| } | |
| if (nums.length <= 1) { | |
| return nums.length; | |
| } |
| HashMap<Integer, Integer>[] dp = new HashMap[nums.length]; | ||
| int maxLength = 2; | ||
|
|
||
| // Initialize dp array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove it. Of:
| // Initialize dp array | |
| // fill the dp array |
| void testNullInput() { | ||
| // Verify that an IllegalArgumentException is thrown when nums is null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| void testNullInput() { | |
| // Verify that an IllegalArgumentException is thrown when nums is null | |
| void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() { |
| import java.util.HashMap; | ||
|
|
||
| final class LongestArithmeticSubsequence { | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } | ||
|
|
||
| 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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case, when the arithmetic sequence is constant, e.g.:
| 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)); | |
| 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)); |
Description
This PR implements the Longest Arithmetic Subsequence algorithm. The algorithm identifies the length of the longest arithmetic subsequence from a given array of integers, where a subsequence is defined as arithmetic if the differences between consecutive elements are the same.
Key Changes:
Added LongestArithmeticSubsequence.java under the dynamicprogramming directory.
The implementation uses dynamic programming with a HashMap to store the differences between elements and keep track of subsequences efficiently.
clang-format -i --style=file path/to/your/file.java