-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
52 lines (44 loc) · 1.85 KB
/
Solution.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
47
48
49
50
51
52
//Problem: https://www.hackerrank.com/challenges/closest-numbers
//Java 8
/*
Initial Thoughts: We can sort the numbers then just look
at adjacent absolute mins since they will
always be smaller than non adjacent sums.
In the case of multiple pairs, or for just
tracking the min in general, we can use a
stringbuilder kind of like a linked list of
sorts.
Time Complexity: O(n log(n)) //We need to sort the array list
Space Complexity: O(n) //We use a dynamically sized array
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++)
array[i] = input.nextInt();
Arrays.sort(array);
int minAbs = Integer.MAX_VALUE; //Minimum absolute difference
StringBuilder pairs = new StringBuilder("");
for(int i = 0; i < n-1; i++)
{
int absDiff;
if((array[i]<0 && array[i+1]<0)||(array[i]>0 && array[i+1]>0))//both numbers have matching signs
absDiff = Math.abs(array[i] - array[i+1]); //Absolute difference
else
absDiff = Math.abs(array[i]) + Math.abs(array[i+1]); //Absolute difference
if(absDiff < minAbs)//New minAbs
{
minAbs = absDiff;
pairs = new StringBuilder("");//Empty pairs
pairs.append(array[i]+ " " +array[i+1]+" ");//Add pair
}
else if(absDiff == minAbs)//Multiple minAbs
pairs.append(array[i]+ " " +array[i+1]+" ");//Add pair
}
System.out.println(pairs);
}
}