Skip to content

Conversation

@rishanmenezes
Copy link
Contributor

@rishanmenezes rishanmenezes commented Oct 19, 2025

PR Title Format: 1. Two Sum.cpp

Intuition

The "Two Sum" problem asks us to find two numbers in an array that add up to a specific target. A brute-force approach would involve checking every possible pair, which is O(n^2). To optimize this, we can use a hash map (unordered_map in C++) to store numbers we've already seen along with their indices. This allows us to quickly check if the complement (target - current number) exists in the map.

Approach

    Initialize an unordered_map called numMap to store numbers and their indices.
    Iterate through the input array nums with an index i .
    For each number nums[i] , calculate the complement needed to reach the target (i.e., target - nums[i] ).
    Check if the complement exists as a key in numMap .
    • If it does, we have found the two numbers. Return the index of the complement (stored in numMap ) and the current index i .
    If the complement is not found, add the current number nums[i] and its index i to the numMap .
    If the loop completes without finding a pair (which shouldn't happen based on the problem constraints, as a solution is guaranteed), return an empty vector or handle as appropriate.

Code Solution (C++)

#include <vector>
#include <unordered_map>

class Solution {
public:
    std::vector<int> twoSum(std::vector<int>& nums, int target) {
        std::unordered_map<int,  int> numMap;
        int n = nums.size();

        for (int i = 0; i < n; ++i) 
        {
            int complement = target 
            - nums[i];
            if (numMap.count
            (complement)) {
                return {numMap
                [complement], i};
            }
            numMap[nums[i]] = i;
        }

        return {}; // Should not 
        happen based on problem 
        constraints
    }
};

Related Issues

#193

By submitting this PR, I confirm that:

  • This is my original work not totally AI generated
  • I have tested the solution thoroughly on leetcode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

Summary by Sourcery

New Features:

  • Implement Two Sum solution in C++ leveraging a hash map for complement lookup.

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 19, 2025

Reviewer's Guide

Introduces a linear-time C++ implementation for the Two Sum problem by leveraging an unordered_map to track previously seen numbers and their indices for constant-time complement lookups.

Class diagram for the new Solution class (Two Sum)

classDiagram
class Solution {
  +std::vector<int> twoSum(std::vector<int>& nums, int target)
}
Loading

File-Level Changes

Change Details Files
Add a hash map-optimized Two Sum algorithm
  • Initialize an unordered_map to map values to their indices
  • Iterate over the input vector and compute the complement for each element
  • Check and return the stored index when the complement is found
  • Insert the current element and its index into the map when no match is found
  • Include an unreachable empty-vector fallback return
1. Two Sum.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Consider using auto it = numMap.find(complement) and checking it != end() to avoid two hash lookups (count + operator[]).
  • Reserve space in numMap (numMap.reserve(nums.size())) before the loop to reduce rehashing overhead for large inputs.
  • Use size_t for the loop index and n (instead of int) to match nums.size() return type and avoid type mismatches.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider using auto it = numMap.find(complement) and checking it != end() to avoid two hash lookups (count + operator[]).
- Reserve space in numMap (numMap.reserve(nums.size())) before the loop to reduce rehashing overhead for large inputs.
- Use size_t for the loop index and n (instead of int) to match nums.size() return type and avoid type mismatches.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@SjxSubham
Copy link
Owner

@rishanmenezes raise an ISSUE as well,,, as it;'s not present at the existing ISSUE list,,,

@rishanmenezes
Copy link
Contributor Author

@rishanmenezes raise an ISSUE as well,,, as it;'s not present at the existing ISSUE list,,,

i have raised an issue as well

@SjxSubham
Copy link
Owner

@rishanmenezes raise an ISSUE as well,,, as it;'s not present at the existing ISSUE list,,,

i have raised an issue as well

  1. make the chnages which i mentioned on that issue 1. Two Sum (C++) #193
  2. as well as Star the repo ⭐
  3. Rename the PR title/PR name with Proper format Problem no. Problem name.cpp

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.

2 participants