
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find K-th Smallest Pair Distance in C++
Suppose we have an integer array; we have to find the kth smallest distance among all the pairs. The distance of a pair (A, B) is actually the absolute difference between A and B. So if the input is like [1,3,8], then all possible pairs are [1,3], [3, 8], [1, 8], then when k = 2, the second smallest distance is 5 (8 - 3).
To solve this, we will follow these steps −
- n := size of nums, x := 0
- for initialize i := 0, when i < n, update (increase i by 1), do −
- x := maximum of x and nums[i]
- Define an array cnt of size x + 1
- for initialize i := 0, when i < n, update (increase i by 1), do −
- for initialize j := i + 1, when j < n, update (increase j by 1), do −
- increase cnt[|nums[j] - nums[i]|] by 1
- for initialize j := i + 1, when j < n, update (increase j by 1), do −
- for initialize i := 0, when i <= x, update (increase i by 1), do −
- if cnt[i] >= k, then −
- return i
- k := k - cnt[i]
- if cnt[i] >= k, then −
- return x
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int smallestDistancePair(vector<int>& nums, int k) { int n = nums.size(); int x = 0; for(int i = 0; i < n; i++)x = max(x, nums[i]); vector <int> cnt(x + 1); for(int i = 0 ; i < n; i++){ for(int j = i + 1; j < n; j++){ cnt[abs(nums[j] - nums[i])]++; } } for(int i = 0; i <= x; i++){ if(cnt[i] >= k)return i; k -= cnt[i]; } return x; } }; main(){ Solution ob; vector<int> v = {1,3,8}; cout << (ob.smallestDistancePair(v, 2)); }
Input
{1,3,8}
Output
5
- Related Articles
- K-th Smallest Prime Fraction in C++
- Find k-th smallest element in given n ranges in C++
- Find m-th smallest value in k sorted arrays in C++
- K-th Smallest in Lexicographical Order in C++
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- Find the k-th smallest divisor of a natural number N in C++
- Python program to find k'th smallest element in a 2D array
- K-th smallest element after removing some integers from natural numbers in C++
- Program to find out the k-th smallest difference between all element pairs in an array in C++
- Program to find smallest pair sum where distance is not consecutive in Python
- Find K Pairs with Smallest Sums in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Find value of k-th bit in binary representation in C++
- Find smallest range containing elements from k lists in C++
- K-th Symbol in Grammar in C++

Advertisements