Given an array of integers, print the minimum of the absolute difference of all possible pairs of elements.
Example :N = 5
ARR = [ 3, -6, 7, -7, 0 ]
Out of all pairs, (-7,-6) have a difference of ‘1’, and no other pair has less difference. So ‘ANS’ is ‘1’.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains integer ‘N’ denoting the size of the array ‘ARR’.
The second line contains ‘N’ integers denoting the integers of array ‘ARR’.
Output format :
For each test case, print the minimum difference of all possible pairs in ‘ARR’.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 10^5
-10^8 <= ARR[i] <= 10^8
Sum of N <= 10^5
Time Limit: 1 sec
2
4
1 8 3 10
2
5 5
2
0
For test case 1,
Out of all pairs (1,3) and (8,10) have the minimum difference ‘2’ so the answer is ‘2’.
For test case 2,
There is only one possible pair (5,5) so the answer is ‘0’.
2
3
8 1 8
2
-3 3
0
6
Can you go through all pairs and find the best answer?
APPROACH :
ALGORITHM :
O( N^2 ), where ‘N’ is the size of array ‘ARR’.
There is a total of ‘(N * (N - 1))/2’ pairs, so the overall time complexity is O(N^2).
O( 1 ), where ‘1’ is constant space.
No extra space is used in the following approach so the overall time complexity is O(1).