
- 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
Program to find out the k-th smallest difference between all element pairs in an array in C++
Suppose we are given a list containing several integer numbers. We have to find out the difference between each pair of values in the array and find out the k-th smallest difference number. The index starts at 0 and the value k is given to us as input.
So, if the input is like numbers = {2, 6, 4, 8}, k = 2, then the output will be 2.
The differences between the pairs are −
(2, 6) = 4
(2, 4) = 2
(2, 8) = 6
(6, 4) = 2
(6, 8) = 2
(4, 8) = 4
If we sort the values, it becomes 2, 2, 2, 4, 4, 6. The 2-nd smallest value is 2. (Index starts from 0).
To solve this, we will follow these steps −
- increase k by 1
- sort the array input
- le := 0
- ri := last element of input - first item of input
- while le < ri, do −
- mid := (le + ri) / 2
- tmp := 0
- lp := 0
- for initialize i := 1, when i < size of input, update (increase i by 1), do −
- while input[i] - input[lp] > mid, do −
- lp := lp + 1
- tmp := tmp + i - lp
- while input[i] - input[lp] > mid, do −
- if tmp >= k, then −
- ri := mid
- Otherwise
- le := mid + 1
- return le
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; int solve(vector<int>& input, int k) { k++; sort(input.begin(), input.end()); int le = 0; int ri = input.back() - input[0]; while (le < ri) { int mid = (le + ri) / 2; long long tmp = 0; int lp = 0; for (int i = 1; i < input.size(); i++) { while (input[i] - input[lp] > mid) lp++; tmp += i - lp; } if (tmp >= k) ri = mid; else le = mid + 1; } return le; } int main() { vector<int> numbers = {2, 6, 4, 8}; cout<< solve(numbers, 2) <<endl; return 0; }
Input
vector<int> numbers = {2, 6, 4, 8}; cout<< solve(numbers, 2) <<endl;
Output
2
- Related Articles
- Python program to find k'th smallest element in a 2D array
- Find k-th smallest element in given n ranges in C++
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- C# Program to find the smallest element from an array
- k-th missing element in an unsorted array in C++
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program
- Find the K-th minimum element from an array concatenated M times in C++
- Find K-th Smallest Pair Distance in C++
- Find all distinct pairs with difference equal to k in Python
- Find K Pairs with Smallest Sums in C++
- k-th missing element in sorted array in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Python program to find sum of absolute difference between all pairs in a list
- C# Program to find the smallest element from an array using Lambda Expressions
- C++ Program to find out the minimum difference value in n integer pairs

Advertisements