Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Nov 22, 2025

Describe your change:

Adds an algorithm to fund two elements from an unsorted array whose sum is less than the target integer k

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added a Two Sum Less Than K algorithm to find the maximum sum of two array elements below a given threshold.
  • Bug Fixes

    • Two Sum now returns an empty list when no valid pair exists.
  • Documentation

    • Added README for the Two Sum Less Than K problem and updated directory/navigation to include it.
  • Tests

    • Added a test suite validating the Two Sum Less Than K behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Nov 22, 2025
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Array Array data structure Binary Search Binary Search Algorithm labels Nov 22, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 22, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Adds a new "Two Sum Less Than K" implementation, README, and tests; updates DIRECTORY.md to include the new entry; and changes two_sum to explicitly return an empty list when no pair is found.

Changes

Cohort / File(s) Summary
Navigation Updates
DIRECTORY.md
Adds a navigation entry/link for "Two Sum Less K".
Existing Algorithm Refinement
algorithms/arrays/two_sum/__init__.py
two_sum now explicitly returns [] when no pair is found (previously returned None implicitly).
Two Sum Less Than K Feature
algorithms/arrays/two_sum_less_k/README.md, algorithms/arrays/two_sum_less_k/__init__.py, algorithms/arrays/two_sum_less_k/test_two_sum.py
New problem docs and implementation: two_sum_less_than_k(nums, k) with a binary-search helper search(...) (O(n log n) approach), README with constraints/examples, and five unit tests.

Sequence Diagram(s)

sequenceDiagram participant Caller participant two_sum_less_than_k as "two_sum_less_than_k\n(module)" participant sort as "sort()" participant search as "search() (binary search)" Caller->>two_sum_less_than_k: two_sum_less_than_k(nums, k) two_sum_less_than_k->>sort: sort nums ascending sort-->>two_sum_less_than_k: sorted nums loop iterate x over indices two_sum_less_than_k->>search: search(sorted, k - nums[x], start=x+1) search-->>two_sum_less_than_k: index y or -1 alt y != -1 two_sum_less_than_k->>two_sum_less_than_k: update max_sum = max(max_sum, nums[x]+nums[y]) end end two_sum_less_than_k-->>Caller: return max_sum or -1 
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Check binary search correctness in algorithms/arrays/two_sum_less_k/__init__.py (off-by-one and strict < vs <=).
  • Verify tests in algorithms/arrays/two_sum_less_k/test_two_sum.py cover edge cases and align with README examples.
  • Confirm changing two_sum return from None to [] won't break callers expecting None.

Possibly related PRs

Poem

🐰 In sorted fields I hop and peek,
Seeking pairs whose sums are meek.
Binary hops, a quiet click—
Find the max that's less than K, quick! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding a new algorithm 'Two Sum Less K' in the arrays category with binary search optimization.
Description check ✅ Passed The description matches the template with all required sections completed, including a clear description of the change and all checklist items appropriately marked.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6aee55a and fa4af7b.

📒 Files selected for processing (1)
  • algorithms/arrays/two_sum_less_k/README.md (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
DIRECTORY.md (1)

26-27: Clarify the test link label to avoid confusion.

Line 27 uses the same label "Test Two Sum" as line 25, making it difficult to distinguish between the regular Two Sum tests and the Two Sum Less K tests.

Consider updating the label to be more specific:

 * Two Sum Less K - * [Test Two Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/two_sum_less_k/test_two_sum.py) + * [Test Two Sum Less K](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/two_sum_less_k/test_two_sum.py)
algorithms/arrays/two_sum_less_k/__init__.py (1)

30-56: Consider whether search should be a private helper.

The search function is currently exported from the module but appears to be an internal implementation detail used only by two_sum_less_than_k.

If this function is not intended for external use, consider prefixing it with an underscore to indicate it's private:

-def search(nums: List[int], target: int, start: int) -> int: +def _search(nums: List[int], target: int, start: int) -> int:

Then update the call on line 22:

- y = search(nums, k - nums[x], x + 1) + y = _search(nums, k - nums[x], x + 1)

However, if you intend for search to be part of the public API for reuse in other contexts, the current implementation is fine.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f74b0b9 and 6aee55a.

⛔ Files ignored due to path filters (4)
  • algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_1.png is excluded by !**/*.png
  • algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_2.png is excluded by !**/*.png
  • algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_3.png is excluded by !**/*.png
  • algorithms/arrays/two_sum_less_k/images/examples/two_sum_less_k_example_4.png is excluded by !**/*.png
📒 Files selected for processing (5)
  • DIRECTORY.md (1 hunks)
  • algorithms/arrays/two_sum/__init__.py (1 hunks)
  • algorithms/arrays/two_sum_less_k/README.md (1 hunks)
  • algorithms/arrays/two_sum_less_k/__init__.py (1 hunks)
  • algorithms/arrays/two_sum_less_k/test_two_sum.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
algorithms/arrays/two_sum_less_k/test_two_sum.py (1)
algorithms/arrays/two_sum_less_k/__init__.py (1)
  • two_sum_less_than_k (4-27)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

26-26: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


27-27: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)

🔇 Additional comments (2)
algorithms/arrays/two_sum/__init__.py (1)

30-30: LGTM! Good improvement for clarity.

Explicitly returning an empty list is more clear and consistent than relying on implicit None return. This makes the function's contract explicit.

algorithms/arrays/two_sum_less_k/test_two_sum.py (1)

1-48: LGTM! Comprehensive test coverage.

The test suite provides excellent coverage with five well-chosen test cases:

  • Standard scenarios with valid pairs
  • Edge cases where no valid pair exists (correctly expecting -1)
  • Arrays with duplicate values
  • Various input sizes

The tests are clear, well-documented, and follow unittest conventions.

Comment on lines +4 to +27
def two_sum_less_than_k(nums: List[int], k: int) -> int:
"""
Finds the maximum sum of two elements in a given list of numbers that is less than k.
Uses binary search to achieve a time complexity of O(n log n) and find the maximum sum of two elements
that is less than k. It takes the nums array and the target value k as input.
Args:
nums (List[int]): A sorted list of integers
k int: The target value to search for
Returns:
The maximum sum of two elements that is less than k
"""
max_sum = -1

# sort the numbers in ascending order to facilitate binary search
nums.sort()

for x in range(len(nums)):
# find the maximum sum of two elements that is less than k, with the first element being nums[x]
y = search(nums, k - nums[x], x + 1)
if y > x:
# update max_sum with the maximum sum found so far
max_sum = max(max_sum, nums[x] + nums[y])

return max_sum
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Fix misleading docstring about input requirements.

Line 10 states that nums should be "A sorted list of integers", but the function sorts the input on line 18. The input does not need to be pre-sorted.

Update the docstring to reflect the actual requirement:

 Args: - nums (List[int]): A sorted list of integers + nums (List[int]): A list of integers (will be sorted internally) k int: The target value to search for
🤖 Prompt for AI Agents
In algorithms/arrays/two_sum_less_k/__init__.py around lines 4 to 27, the docstring incorrectly says nums is "A sorted list of integers" while the function sorts nums internally; update the docstring to state that nums is an unsorted list of integers (or simply a list of integers) and that the function will sort it as part of its operation, and correct the Args section formatting for k (e.g., "k (int): target value") and the Returns description to clearly state it returns the maximum sum of two elements less than k or -1 if none exist. 
@BrianLusina BrianLusina merged commit b367a0b into main Nov 23, 2025
4 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Array Array data structure Binary Search Binary Search Algorithm Datastructures Datastructures Documentation Documentation Updates enhancement

2 participants