Skip to content

Commit 6f49cbe

Browse files
committed
leetcode 16. 3sum closest
1 parent 479a753 commit 6f49cbe

10 files changed

+159
-0
lines changed

Hashing/TwoSum.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Given an array of integers, return indices of the two numbers such that they
3+
* add up to a specific target.
4+
*
5+
* You may assume that each input would have exactly one solution, and you may
6+
* not use the same element twice.
7+
*
8+
* Example:
9+
*
10+
* Given nums = [2, 7, 11, 15], target = 9,
11+
*
12+
* Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
13+
*/
14+
15+
class TwoSum {
16+
public int[] twoSum(int[] nums, int target) {
17+
HashMap<Integer, Integer> diffMap = new HashMap<>();
18+
19+
for (int i = 0; i < nums.length; i++)
20+
{
21+
if(diffMap.containsKey(nums[i]))
22+
{
23+
return new int[]{diffMap.get(nums[i]), i};
24+
}
25+
else {
26+
diffMap.put((target - nums[i]), i);
27+
}
28+
}
29+
30+
throw new IllegalArgumentException("No two sum solution.");
31+
}
32+
33+
public static void main(String[] args) {
34+
int[] arr = {2, 7, 11, 15};
35+
int target = 9;
36+
}
37+
}

LeetCode/1. Two Sum/Solution.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Given an array of integers, return indices of the two numbers such that they
3+
* add up to a specific target.
4+
*
5+
* You may assume that each input would have exactly one solution, and you may
6+
* not use the same element twice.
7+
*
8+
* Example:
9+
*
10+
* Given nums = [2, 7, 11, 15], target = 9,
11+
*
12+
* Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
13+
*/
14+
15+
class Solution {
16+
public int[] twoSum(int[] nums, int target) {
17+
HashMap<Integer, Integer> diffMap = new HashMap<>();
18+
19+
for (int i = 0; i < nums.length; i++)
20+
{
21+
if(diffMap.containsKey(nums[i]))
22+
{
23+
return new int[]{diffMap.get(nums[i]), i};
24+
}
25+
else {
26+
diffMap.put((target - nums[i]), i);
27+
}
28+
}
29+
30+
throw new IllegalArgumentException("No two sum solution.");
31+
}
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target.
3+
* Return the sum of the three integers. You may assume that each input would have exactly one solution.
4+
*
5+
* Example:
6+
*
7+
* Given array nums = [-1, 2, 1, -4], and target = 1.
8+
* The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
9+
*/
10+
11+
class Solution {
12+
public int threeSumClosest(int[] nums, int target) {
13+
Arrays.sort(nums);
14+
int closest = nums[0] + nums[1] + nums[2];
15+
16+
for(int i = 0; i < nums.length; i++)
17+
{
18+
int left = i + 1;
19+
int right = nums.length - 1;
20+
21+
while(left < right)
22+
{
23+
int sum = nums[left] + nums[right] + nums[i];
24+
25+
if(Math.abs(sum - target) < Math.abs(closest - target)) //check for closeness
26+
closest = sum;
27+
28+
if(sum > target)
29+
right --;
30+
31+
if(sum < target)
32+
left ++;
33+
34+
if(sum == target)
35+
return closest;
36+
}
37+
}
38+
return closest;
39+
}
40+
}

LeetCode/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Leetcode Problems
22

3+
## Arrays
4+
- [x] [Two Sum](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/1.%20Two%20Sum)
5+
- [x] [3Sum Closest]()
36
## Linked List
47

58
- [x] [Add Two Numbers](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/2.%20Add%20Two%20Numbers)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* An Array of integers is given, both +ve and -ve.
3+
* You need to find the two elements such that their sum is closest to zero.
4+
*/
5+
6+
import java.util.*;
7+
import java.lang.*;
8+
public class SumClosestToZero
9+
{
10+
public void twoSumCloseToZero(int[] arr)
11+
{
12+
int i = 0;
13+
int n = arr.length;
14+
int j = n - 1;
15+
int sum = Integer.MAX_VALUE;
16+
int minSum = Integer.MAX_VALUE;
17+
18+
int min_l = i;
19+
int min_r = n - 1;
20+
21+
Arrays.sort(arr);
22+
23+
while(i < j)
24+
{
25+
sum = arr[i] + arr[j];
26+
if(Math.abs(sum) < Math.abs(minSum))
27+
{
28+
minSum = sum;
29+
min_l = i;
30+
min_r = j;
31+
}
32+
if (sum < 0)
33+
i++;
34+
else
35+
j--;
36+
}
37+
38+
System.out.println("The two elements whose sum is minimum are : " + arr[min_l] + " " + arr[min_r]);
39+
}
40+
41+
public static void main(String[] args) {
42+
SumClosestToZero obj = new SumClosestToZero();
43+
int arr[] = {1, 60, -10, 70, -80, 85};
44+
45+
obj.twoSumCloseToZero(arr);
46+
}
47+
}

0 commit comments

Comments
 (0)