Link Search Menu Expand Document

1. Two Sum

Solution Code

C#

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        int n = nums.Length;
        
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (nums[i] + nums[j] == target)
                    return new int []{i, j};
            }
        }
        
        return new int []{-1, -1};
    }
}

C++

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n = nums.size();
        vector<int> ans;
        
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (nums[i] + nums[j] == target) {
                    ans.push_back(i);
                    ans.push_back(j);
                    
                    return ans;
                }
            }
        }
        
        return ans;
    }
};
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<pair<int, int>> valueIndexPair;
	vector<int> answer;	
	int index1, index2, sum;

        for(int k = 0; k < nums.size(); k++) {
            valueIndexPair.push_back(pair<int, int>(nums[k], k));
        }
        
        sort(valueIndexPair.begin(), valueIndexPair.end());
        
	index1 = 0;
	index2 = valueIndexPair.size() - 1;

        while(index1 < index2) {
            sum = valueIndexPair[index1].first + valueIndexPair[index2].first;
            
	    if(sum == target)
                break;
            else if(sum < target)
                index1++;
            else
                index2--;
        }
        
        answer.push_back(valueIndexPair[index1].second);
        answer.push_back(valueIndexPair[index2].second);
        
        return answer;
    }
};

Java

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (nums[i] + nums[j] == target)
                    return new int []{i, j};
            }
        }
        
        return new int []{-1, -1};
    }
}

Python

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        num_idx_map = {}

        for idx, num in enumerate(nums):
            difference = target - num            
            if difference in num_idx_map:                
                return [num_idx_map[difference], idx]
            else:
                num_idx_map[num] = idx
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        # creating a list with their respective indices
        num_with_idx = [(num, idx) for idx, num in enumerate(nums)]
        # sorting list of tuple based on their first value
        num_with_idx = sorted(num_with_idx, key=lambda x: x[0])

        # Two-Pointer approach
        left_idx = 0
        right_idx = len(nums) - 1

        while left_idx < right_idx:
            sum_result = num_with_idx[left_idx][0] + num_with_idx[right_idx][0]
            if sum_result > target:
                right_idx -= 1
            elif sum_result < target:
                left_idx += 1
            elif sum_result == target:
                return [num_with_idx[left_idx][1], num_with_idx[right_idx][1]]

© 2023. All rights reserved.