We are given an array of size n containing positive integers. The absolute difference between values at indices i and j is |a[i] - a[j]|. There are n* (n - 1) / 2 ($\frac{n(n-1)}{2}$ ) such pairs and we are asked to find and print the k-th smallest absolute difference among all these pairs, given ( $1 \leq k \leq \frac{n(n-1)}{2}$ ).
Input: arr =[1,3,6], k =2Output: 2Explanation:
1. The absolute differences are:|1-3|=2,|1-6|=5,|3-6|=32. The sorted differences are:[2,3,5]3. The 2nd smallest difference is2.
⏰ Time complexity: O(n log n + n^2 log n), where n is number of elements in array. Sorting takes O(n \log n). Counting pairs inside the binary search takes O(n^2). Thus, the overall time complexity is O(n \log n + n^2 \log n).