
- 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
Smallest Range Covering Elements from K Lists in C++
Suppose we have k lists of sorted integers. We have to search the smallest range that includes at least one number from each of the k 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
Let us see the following implementation to get better understanding −
Example
#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
- Find smallest range containing elements from k lists in C++
- Program to find smallest difference between picked elements from different lists in C++
- Count number of smallest elements in given range in C++
- Selecting Elements from Lists in Perl
- Count all the numbers in a range with smallest factor as K in C++
- Find the k smallest numbers after deleting given elements in C++
- Smallest Range II in C++
- Smallest Range I in Python
- Merge k Sorted Lists in Python
- Get positive elements from given list of lists in Python
- Program to interleave list elements from two linked lists in Python
- Merge K sorted linked lists in Java
- Python – Next N elements from K value
- Remove a range of elements from a LinkedList in Java
- Remove a range of elements from the ArrayList in C#

Advertisements