two sum

Solutions on MaxInterview for two sum by the best coders in the world

showing results for - "two sum"
Filippo
25 Jan 2017
1bool twosum(int A[], int N, int X) {
2  	sort(A, A+N);
3    int i = 0, j = N-1;
4    while (i < j) {
5        if (A[i] + A[j] == X) return true;
6        else if (A[i] + A[j] > X) j--;
7        else i++;
8    }
9    return false;
10}
Sofie
02 Jan 2017
1class Solution {
2    public int[] twoSum(int[] nums, int target) {
3      int[] arr = new int[2];
4        
5        HashSet<Integer> hs = new HashSet<>();
6        
7        for(int i=0; i<nums.length; i++){
8            if(hs.contains(target-nums[i])){
9                arr[1] = i;
10                break;
11            }
12            hs.add(nums[i]);
13        }
14        
15        for(int i=0; i<nums.length; i++){
16            if(nums[i] == target - nums[arr[1]] && arr[1] != i)
17                arr[0] = i;
18        }
19        return arr;
20    }
21}
22
queries leading to this page
two sum problem solution c 2b 2bhashmap c 2b 2b 2sumtwo sum problem explanationtwo number sum hashmap c 2b 2btwo sum solution hstwo sum approach o 28n 29 c 2btwo sum in geeetwo sum in cpptwo sum in c 2b 2b1 two sum leetcode solution c 2b 2btwo sum leetcode solution c 2b 2btwo sum leetcode solution c 2b 2bc 2b 2b two sum leetcode2 sum problem leetcode2 sum c 2b 2btwo sum map solution c 2b 2btwo sums array c 2b 2btwo sum c 2b 2bsubset sum problem without dpleetcode two sum c 2b 2bleetcode two sum solution c 2b 2btwo sum iitwo sum soloution c 2b 2btwo sum in set cpphow to do two sum in c 2b 2btwo sum problem in c 2b 2b2 sum 1 two sumtwo sum 3subset sum dp problemsolution to two sum problem c 2b 2btwo sum leetcode c 2b 2btwo sum c 2b 2b geeksforgeekstwo sum problem with hashing in c 2b 2btwo sum 5how to get two numbers which their sum equal their product c 2b 2btwo sum c 2b 2b solutiontwo sum algorithmtwo sum function unordered map c 2b 2b2 sum c 2b 2b solutionfunction find two sums c 2b 2b2 number sum in cppwhat is 2 sumfunction find two sums c 2b 2b with target sum2 sum solutionways to sove two sumtwo summation cpptwo sum solution c 2b 2btwo sum problem2 sumtwo sumtwo sum 2two summation of 2 integer array c 2b 2b2 sum problem c 2b 2btwo subset sum problemtwo sum solution c 2b 2btwo sum problem using c 2b 2btwo number sum problem c 2b 2bleet code two sum problem in c 2b 2b solutionsum twofunction find two sum c 2b 2b with target sumtwo sum 4two sum two sum solutiontwo sum solutiontwo sum cpptwo number sum in cpptwosum using vectorstwo sum explaination c 2b 2b two sum problemc 2b 2b program for sum of two numbers without a functions2 sum problemtwo sum 1two number sumtwo sum