Find Pairs with least absolute difference in an given unsorted array,
Interview Answers
Anonymous
Jan 22, 2016
1. Sort the array - O(nlogn)
2. Run through the loop to get the minimum difference
6
Anonymous
Mar 2, 2016
there is better approach: build a tree, and each time you insert a value to the tree, compute difference with all elements you compare with this element. The whole complexity will be O(NlogN)
Anonymous
Mar 14, 2016
Time O(n), Space O(n), n is length of integer.
List> findPairNubmer(int[] nums, int target) {
List> result = new ArrayList();
if(nums.length == 0 )
return result;
HashSet set = new HashSet();
for(int i : nums) {
set.add(i);
}
for(int i = 0; i pair = new ArrayList();
pair.add(nums[i]);
pair.add(nums[i] + target);
result.add(pair);
}
}
return result;
}