Two Sum solution

    
def twoSum(nums, target):
    seen = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            return [seen[complement], i]
        seen[num] = i
    return []

nums = [2, 7, 11, 15]
target = 9
print(twoSum(nums, target))


Explanation: The "Two Sum" problem asks to find two numbers in an array that add up to a given target value.
The Python, Java, and C++ solutions follow a similar approach using a hash map (or dictionary) to store previously seen numbers and their indices.

Iterate through the array, and for each number num: Calculate the complement as target - num.
Check if the complement exists in the hash map. If it does, return the indices of the two numbers.
If the complement doesn't exist, add the current number num to the hash map with its index.
If no two numbers are found, return an empty array.
The time complexity of the solution is O(n), where n is the number of elements in the array.
I hope this helps! Let me know if you have any further questions.


         
    import java.util.HashMap;
    import java.util.Map;
    
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                int complement = target - nums[i];
                if (map.containsKey(complement)) {
                    return new int[]{map.get(complement), i};
                }
                map.put(nums[i], i);
            }
            return new int[]{};
        }
    
        public static void main(String[] args) {
            int[] nums = {2, 7, 11, 15};
            int target = 9;
            Solution solution = new Solution();
            int[] result = solution.twoSum(nums, target);
            for (int num : result) {
                System.out.print(num + " ");
            }
        }
    }
    

Explanation: The "Two Sum" problem asks to find two numbers in an array that add up to a given target value.
The Python, Java, and C++ solutions follow a similar approach using a hash map (or dictionary) to store previously seen numbers and their indices.

Iterate through the array, and for each number num: Calculate the complement as target - num.
Check if the complement exists in the hash map. If it does, return the indices of the two numbers.
If the complement doesn't exist, add the current number num to the hash map with its index.
If no two numbers are found, return an empty array.
The time complexity of the solution is O(n), where n is the number of elements in the array.
I hope this helps! Let me know if you have any further questions.


         
    #include <iostream>
    #include <vector>
    #include <unordered_map>
    using namespace std;
        
    vector twoSum(vector& nums, int target) {
        unordered_map map;
        for (int i = 0; i < nums.size(); i++) {
            int complement = target - nums[i];
            if (map.count(complement)) {
                return {map[complement], i};
            }
            map[nums[i]] = i;
        }
            return {};
    }
        
    int main() {
        vector nums = {2, 7, 11, 15};
        int target = 9;
        vector result = twoSum(nums, target);
        for (int num : result) {
            cout << num << " ";
        }
        return 0;
    }
        
        

Explanation: The "Two Sum" problem asks to find two numbers in an array that add up to a given target value.
The Python, Java, and C++ solutions follow a similar approach using a hash map (or dictionary) to store previously seen numbers and their indices.

Iterate through the array, and for each number num: Calculate the complement as target - num.
Check if the complement exists in the hash map. If it does, return the indices of the two numbers.
If the complement doesn't exist, add the current number num to the hash map with its index.
If no two numbers are found, return an empty array.
The time complexity of the solution is O(n), where n is the number of elements in the array.
I hope this helps! Let me know if you have any further questions.

>