Skip to content
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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

tejaswi0910
Copy link

@tejaswi0910 tejaswi0910 commented Oct 1, 2024

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.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Oct 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 52.34%. Comparing base (0bd86b3) to head (677bbad).

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.
📢 Have feedback on the report? Share it here.

@tejaswi0910 tejaswi0910 marked this pull request as ready for review October 1, 2024 14:06
Copy link
Member

@vil02 vil02 left a 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.

@tejaswi0910
Copy link
Author

Please remove the main method and add proper junit tests instead. Prefer ParameterizedTests.

sure @vil02 , removed the main function and added tests!

@vil02 vil02 self-assigned this Oct 2, 2024
tejaswi0910 and others added 2 commits October 2, 2024 17:25
…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>
Comment on lines +24 to +30
if (nums.length == 0) {
return 0;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

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:

Suggested change
// Initialize dp array
// fill the dp array

Comment on lines +27 to +28
void testNullInput() {
// Verify that an IllegalArgumentException is thrown when nums is null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void testNullInput() {
// Verify that an IllegalArgumentException is thrown when nums is null
void testGetLongestArithmeticSubsequenceLengthThrowsForNullInput() {

import java.util.HashMap;

final class LongestArithmeticSubsequence {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

}

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));
Copy link
Member

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.:

Suggested change
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));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants