AfterAcademy Tech
•
13 Jan 2020

Problem Description: Given an array of n distinct integers A[], write a program to find all pairs of elements with the minimum absolute difference of any two elements. Return a list of pairs in ascending order (with respect to pairs), each pair [i, j] as follows :
Example :
Input: A[] = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
Before moving forward, try to solve this problem here.
The brute force solution would be to create a nested loop that compares every possible pair of values in the array.
Solution Steps
Pseudo Code
int[][] minimumAbsDifference(int input[], int size)
{
int min_abs_diff = INT_MAX
int result[][]
for(int i = 0 to size)
{
for(int j = i+1 to size)
min_abs_diff = min(min_abs_diff, abs(input[i] - input[j]))
}
int k = 0
for(int i = 0 to size)
{
for(int j = i+1 to size)
{
if(abs(input[i] - input[j]) == min_abs_diff)
{
if(input[i] < input[j])
{
result[k][0] = input[i]
result[k][1] = input[j]
}
else
{
result[k][0] = input[j]
result[k][1] = input[i]
}
k = k + 1
}
}
}
return result
}
Complexity Analysis
We are running two nested loops to calculate the minimum absolute difference. Time Complexity = O(n²)
Space Complexity = O(1)
Crital Ideas to Think
The better idea is to first sort the array, which simplifies everything. Notably, the minimal difference between 2 values in a sorted array must be between 2 adjacent values, hence once the array is sorted, we only need to go over the array once to find the minimal value.
This could be possible in two ways:
The first solution would likely be more efficient in the average case, although there is no difference in n-notation running time.
Solution steps
Pseudo Code
int[][] minimumAbsDifference(int input[], int size)
{
sort(input)
int min_abs_diff = INT_MAX
int result[][]
for(int i = 0 to size-1)
min_abs_diff = min(min_abs_diff, abs(input[i] - input[i+1]))
int k = 0
for(int i = 0 to size-1)
{
if(abs(input[i] - input[i+1]) == min_abs_diff)
{
result[k][0] = input[i]
result[k][1] = input[i+1]
k = k + 1
}
}
return result
}
Complexity Analysis
Time Complexity = Time Complexity of sorting + Time complexity of the loop to find the minimum absolute difference = O(n log n) + O(n) = O(n logn)
Space Complexity: O(1)
Critical Ideas to Think

Please comment down below if you have alternative approaches or find an error/bug in the above approaches.
Happy Coding, Enjoy Algorithms!
AfterAcademy Tech
This is an interview problem asked in Google technical interview. Given a BST(Binary Search Tree) with non-negative values, write a program to find the minimum absolute difference between values of any two nodes.

AfterAcademy Tech
You have a sorted and rotated array where elements are sorted and rotated circularly. Write a program to find the minimum element in the array.

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.

AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)
