Skip to content

Commit adc6e80

Browse files
committed
Kth smallest number again
1 parent 1ed871a commit adc6e80

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
- [Sort by Set Bit Count](https://practice.geeksforgeeks.org/problems/sort-by-set-bit-count1153/1# "view question") - [Cpp Solution](./solutions/Sort%20by%20Set%20Bit%20Count.cpp)
114114
- [Minimum Swaps to Sort](https://practice.geeksforgeeks.org/problems/minimum-swaps/1# "view question") - [Cpp Solution](./solutions/Minimum%20Swaps%20to%20Sort.cpp)
115115
- [Bishu and Soldiers](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/bishu-and-soldiers/ "view question") - [Cpp Solution](./solutions/Bishu%20and%20Soldiers.cpp)
116+
- [Kth smallest number again](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/kth-smallest-number-again-2/ "view question") - [Cpp Solution](./solutions/Kth%20smallest%20number%20again.cpp)
116117
- []( "view question") - [Cpp Solution](./solutions/.cpp)
117118

118119
### Linked-List
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Kth smallest number again
3+
=========================
4+
5+
Dexter was good in finding the K th smallest number from a set of numbers. He thought he could solve any problem related to K th smallest number. His friend Pipi challenged him with a problem.
6+
He gave him various ranges of number, These numbers were arranged in increasing order(only distinct numbers to be taken into account). Now he asked him to find the K th smallest number in the sequence, again and again.
7+
8+
Input Format
9+
The first line contains T, the number of test cases.
10+
For each test case, there will be two integers N and Q.
11+
Then N lines follow each line containing two integers A and B (denoting the range A-B)
12+
Then Q lines follow each line containing a non-negative integer K .
13+
14+
Output Format
15+
For each query output the K th smallest number.
16+
17+
Constraints
18+
1 <= T <= 100
19+
1 <= N <= 100
20+
1 <= Q <= 1000
21+
-10^18 <= A <= B <= 10^18
22+
K >= 1
23+
24+
N.B. If Kth smallest number is not present in the series, print -1
25+
26+
Sample Input
27+
1
28+
1 3
29+
1 5
30+
1
31+
3
32+
6
33+
Sample Output
34+
1
35+
3
36+
-1
37+
38+
Time Limit: 5
39+
Memory Limit: 256
40+
41+
Explanation
42+
The numbers are "1 2 3 4 5". The 1st smallest number is 1
43+
The 3rd smallest number is 3 The 6th smallest number is not present. Hence answer is -1
44+
*/
45+
46+
#include <bits/stdc++.h>
47+
#define ll long long int
48+
using namespace std;
49+
50+
int main()
51+
{
52+
int t;
53+
cin >> t;
54+
while (t--)
55+
{
56+
int n, q;
57+
cin >> n >> q;
58+
vector<pair<ll, ll>> v;
59+
for (int i = 0; i < n; i++)
60+
{
61+
ll a, b;
62+
cin >> a >> b;
63+
v.push_back({a, b});
64+
}
65+
66+
sort(v.begin(), v.end());
67+
ll idx = 0;
68+
69+
for (ll i = 1; i < v.size(); i++)
70+
{
71+
if (v[idx].second >= v[i].first)
72+
{
73+
v[idx].second = max(v[idx].second, v[i].second);
74+
}
75+
else
76+
{
77+
idx++;
78+
v[idx] = v[i];
79+
}
80+
}
81+
82+
while (q--)
83+
{
84+
ll k;
85+
cin >> k;
86+
ll ans = -1;
87+
88+
for (int i = 0; i <= idx; i++)
89+
{
90+
if ((v[i].second - v[i].first + 1) >= k)
91+
{
92+
ans = v[i].first + k - 1;
93+
break;
94+
}
95+
else
96+
{
97+
k = k - (v[i].second - v[i].first + 1);
98+
}
99+
}
100+
cout << ans << endl;
101+
}
102+
}
103+
104+
return 0;
105+
}

0 commit comments

Comments
 (0)