
- 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 smallest difference between picked elements from different lists in C++
Suppose we have a list of lists, we have to find the smallest difference that can be formed by picking one value from each of the lists and taking the difference between the maximum and the minimum number of the picked element.
So, if the input is like lists = [ [30, 50, 90], [85], [35, 70]], then the output will be 20, as we can take 90, 85, 70 and 90 - 70 = 20
To solve this, we will follow these steps −
maxVal := -inf
ret := inf
define a priority queue pq
n := size of lists
for initialize i := 0, when i < n, update (increase i by 1), do −
sort the array lists[i]
insert {lists[i, 0], i, 0} into pq
maxVal := maximum of lists[i, 0] and maxVal
while size of pq is same as n, do −
Define an array temp = top of pq
delete top element from pq
ret := minimum of ret and (maxVal - temp[0])
increase last element of temp
if last element of temp < size of lists[temp[1]], then <
maxVal := maximum of maxVal and lists[temp[1], last element of temp]
temp[0] := lists[temp[1], last element of temp]
insert temp into pq
return ret
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; struct Cmp { bool operator()(vector<int>& a, vector<int>& b) { return !(a[0] < b[0]); } }; class Solution { public: int solve(vector<vector<int>>& lists) { int maxVal = INT_MIN; int ret = INT_MAX; priority_queue<vector<int>, vector<vector<int>>, Cmp> pq; int n = lists.size(); for (int i = 0; i < n; i++) { sort(lists[i].begin(), lists[i].end()); pq.push({lists[i][0], i, 0}); maxVal = max(lists[i][0], maxVal); } while (pq.size() == n) { vector<int> temp = pq.top(); pq.pop(); ret = min(ret, maxVal - temp[0]); temp.back()++; if (temp.back() < lists[temp[1]].size()) { maxVal = max(maxVal, lists[temp[1]][temp.back()]); temp[0] = lists[temp[1]][temp.back()]; pq.push(temp); } } return ret; } }; int solve(vector<vector<int>>& lists) { return (new Solution())->solve(lists); } int main(){ vector<vector<int>> v = {{30, 50, 90},{85},{35, 70}}; cout << solve(v); }
Input
{{30, 50, 90},{85},{35, 70}}
Output
20
- Related Articles
- Program to find minimum difference between two elements from two lists in Python
- Find smallest range containing elements from k lists in C++
- Smallest Range Covering Elements from K Lists in C++
- C# program to list the difference between two lists
- Python program to list the difference between two lists.
- Program to interleave list elements from two linked lists in Python
- Python program to split the even and odd elements into two different lists.
- Java program to split the Even and Odd elements into two different lists
- Python program to find common elements in three lists using sets
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- Program to find minimum difference between largest and smallest value in three moves using Python
- Simple way to find if two different lists contain exactly the same elements in Java
- C++ program to find the smallest element among three elements
- Selecting Elements from Lists in Perl
- 8085 program to find smallest number between two numbers
