C++ | Find smallest interval














































C++ | Find smallest interval



Problem Statement : Given a number 'n', and set of 'n' closed intervals. Find the smallest interval that covers all the intervals. If there are many, return any of them.
For example : n = 5
                      intervals = [0,3],[2,6],[3,4],[6,9], 
in this case the resultant interval would be = [3,6], as is the smallest interval that covers all the interval

Following is the C++ implementation of above problem : 
#include<bits/stdc++.h> using namespace std; pair<int,int> findSmallestSet(vector<pair<int,int> > v) { pair<int,int> ret = {INT_MAX, INT_MIN}; for(int i = 0; i < (int)v.size(); i++) { if(v[i].first > ret.second) { ret.second = v[i].first; } if(v[i].second < ret.first) { ret.first = v[i].second; } } return ret; } int main() { cout << "Enter the number of intervals : "; int n; cin >> n; cout << "Enter the start and end of each interval : "; vector<pair<int,int> > v(n); for(int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } auto smallestSet = findSmallestSet(v); cout << "The smallest interval is : "; cout << smallestSet.first << " " << smallestSet.second << '\n'; return 0; }

Input and Output : 



More Articles of Anuj Maurya:

Name Views Likes
C++ | Find smallest interval 1109 0
C++ | Lexicographic rank of string 538 0
C++ | Counting Rooms (CSES) 383 0
C++ | Building Roads (CSES) 865 0
C++ | High Score (CSES) 2066 0
C++ | Collect maximum coin in grid 541 0
C++ | Is this k-palindrome ? 310 0
C++ | Shuffle the array elements 2337 0
C++ | Fizz Buzz 502 0
C++ | Check if given points form square 265 0
C++ | Klee Algorithm to find union of line segment 327 0
C++ | Check if points are collinear 480 0
C++ | Check if given point lies inside triangle 707 0
C++ | Check if two line segment intersect 1474 0
C++ | Number of ways to decode message 278 0
C++ || Minimise the subset difference 224 0
C++ | Count of subsets with given sum 338 24
C++ | Currency Arbitrage 725 22
C++ | Estimating the value of π (pi) using Monte-Carlo Simulation 225 18
C++ | Find the first missing number 225 16
C++ | Finding word in grid using DFS 261 11
C++ | Implementing pow(x, y) in logarithmic time 288 9
C++ | Sum of all factors of number 236 10
C++ | Building Teams (CSES) 1046 21
C++ | Positions of anagram 224 12
C++ | Integer factorization 606 13
C++ | Primality test 240 14
C++ | Subset Sum Problem 1267 12
C++ | Civil Time in Abseil 542 19
C++ | Abseil Time Duration 669 15
C++ | absl::Time 926 12
C++ | Introduction to Abseil Time library 237 9
C++ | absl::StripPrefix() and absl::StripSuffix() 804 8
C++ | absl::ConsumePrefix() and absl::ConsumeSuffix() 744 19
C++ | absl::string_view 1220 13
C++ | absl::Substitute() | Formatting string in abseil 763 15
C++ | absl::StrJoin() | Yet another way of joining strings 935 7
C++ | String concatenation in Abseil - 2 | absl::StrCat() 395 10
C++ | absl::StrAppend() | String concatenation in abseil - 1 422 16
C++ | absl::StrSplit() 649 15
C++ | Appending to string and Writing to stream in Abseil 235 11
C++ | absl::PrintF(), absl::FPrintF() and absl::SNPrintF() 327 0
C++ | Abseil StrFormat() 271 27
C++ | Abseil flags Library 430 348
C++ | Abseil Numeric Library 257 65
C++ | Abseil Hash library 251 0
C++ | Abseil Containers 422 46
C++ Introduction to Abseil 277 13
CSES | Shortest Route II 1863 47
CSES | Shortest Routes I 2055 1

Comments