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}
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