
- 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 smallest range containing elements from k lists in C++
Suppose we have k different lists. The elements are sorted. We have to search the smallest range that includes at least one number from each of the k different lists. Here the range [a,b] is smaller than range [c,d] when b-a < d-c or a < c if b-a == d-c.
So if the input is like [[4,10,15,25,26],[0,9,14,20],[5,18,24,30]], then the output will be [14, 18]
To solve this, we will follow these steps −
minRange := inf, maxRange := -inf, rangeSize := inf, tempMinRange := inf, tempMaxRange := -inf
n := size of nums
Define an array pointers of size n
make a priority queue pq
for initialize i := 0, when i < n, update (increase i by 1), do −
insert { nums[i, 0], i } into pq
tempMaxRange := maximum of tempMaxRange and nums[i, 0]
while 1 is non-zero, do −
Define one pair temp := top of pq
delete element from pq
tempMinRange := temp.first
idx := second element of temp
if tempMaxRange - tempMinRange < rangeSize, then −
rangeSize := tempMaxRange - tempMinRange
minRange := tempMinRange
maxRange := tempMaxRange
(increase pointers[idx] by 1)
if pointers[idx] is same as size of nums[idx], then −
Come out from the loop
Otherwise
tempMaxRange := maximum of tempMaxRange and nums[idx, pointers[idx]]
insert { nums[idx, pointers[idx]], idx } into pq
Define an array ans of size 2
ans[0] := minRange, ans[1] := maxRange
return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } struct Comparator{ bool operator() (pair <int, int> a, pair <int, int> b){ return !(a.first < b.first); } }; class Solution { public: vector<int> smallestRange(vector<vector<int>>& nums) { int minRange = INT_MAX; int maxRange = INT_MIN; int rangeSize = INT_MAX; int tempMinRange, tempMaxRange, tempRangeSize; tempMinRange = INT_MAX; tempMaxRange = INT_MIN; int n = nums.size(); vector <int> pointers(n); priority_queue < pair <int, int>, vector < pair <int, int> >, Comparator > pq; for(int i = 0; i < n; i++){ pq.push({nums[i][0], i}); tempMaxRange = max(tempMaxRange, nums[i][0]); } while(1){ pair <int, int> temp = pq.top(); pq.pop(); tempMinRange = temp.first; int idx = temp.second; if(tempMaxRange - tempMinRange < rangeSize){ rangeSize = tempMaxRange - tempMinRange; minRange = tempMinRange; maxRange = tempMaxRange; } pointers[idx]++; if(pointers[idx] == nums[idx].size())break; else{ tempMaxRange = max(tempMaxRange, nums[idx][pointers[idx]]); pq.push({nums[idx][pointers[idx]], idx}); } } vector <int> ans(2); ans[0] = minRange; ans[1] = maxRange; return ans; } }; main(){ Solution ob; vector<vector<int>> v = {{4,10,15,25,26},{0,9,14,20},{5,18,24,30}}; print_vector(ob.smallestRange(v)); }
Input
{{4,10,15,25,26},{0,9,14,20},{5,18,24,30}};
Output
[14, 18, ]
- Related Articles
- Smallest Range Covering Elements from K Lists in C++
- Program to find smallest difference between picked elements from different lists in C++
- Find the k smallest numbers after deleting given elements in C++
- Count number of smallest elements in given range in C++
- Find K Pairs with Smallest Sums in C++
- Find K-th Smallest Pair Distance in C++
- Smallest Range II in C++
- Count all the numbers in a range with smallest factor as K in C++
- Find K Closest Elements in C++
- Find common elements in three linked lists in C++
- Find the smallest and second smallest elements in an array in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Selecting Elements from Lists in Perl
- Find missing elements of a range in C++
- Find k-th smallest element in given n ranges in C++
