forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoElementsSumClosestToZero.java
46 lines (38 loc) · 1.26 KB
/
TwoElementsSumClosestToZero.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.rampatra.arrays;
import com.rampatra.sorting.QuickSort;
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 7/30/15
* @time: 5:44 PM
*/
public class TwoElementsSumClosestToZero {
/**
* An array of integers is given containing both +ve and -ve numbers. You
* need to find two elements such that their sum is closest to zero.
*
* @param a
* @return
*/
public static int[] getTwoElementsWhoseSumIsClosestToZero(int[] a) {
QuickSort.quickSort(a);
int minDiff = Math.abs(0 - (a[0] + a[a.length - 1])), n1 = a[0], n2 = a[a.length - 1];
for (int i = 1, j = a.length - 2; i < j; ) {
if (Math.abs(0 - (a[i] + a[j])) < minDiff) {
minDiff = Math.abs(0 - (a[i] + a[j]));
n1 = a[i];
n2 = a[j];
i++;
} else {
j--;
}
}
return new int[]{n1, n2};
}
public static void main(String[] args) {
System.out.println(Arrays.toString(getTwoElementsWhoseSumIsClosestToZero(new int[]{1, 60, -10, -80, 85, 70})));
System.out.println(Arrays.toString(getTwoElementsWhoseSumIsClosestToZero(new int[]{-3, -100, -10, -80, 85, 70})));
}
}