Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5dc28c9

Browse files
Merge pull request from CoderRounak/main
Added K-smallest program to 450-question-series
2 parents 95e43c3 + e207480 commit 5dc28c9

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

‎450-Question-Series/k_smallest.cpp‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Kth smallest element
2+
3+
// Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
4+
5+
// Example 1:-
6+
7+
// Input:
8+
// N = 6
9+
// arr[] = 7 10 4 3 20 15
10+
// K = 3
11+
12+
// Output : 7
13+
14+
// Explanation :
15+
// 3rd smallest element in the given
16+
// array is 7.
17+
18+
// Example 2:
19+
20+
// Input:
21+
// N = 5
22+
// arr[] = 7 10 4 20 15
23+
// K = 4
24+
25+
// Output : 15
26+
27+
// Explanation :
28+
// 4th smallest element in the given
29+
// array is 15.
30+
31+
// Your Task:
32+
// You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the Kth smallest element.
33+
34+
35+
// Expected Time Complexity: O(n)
36+
// Expected Auxiliary Space: O(log(n))
37+
38+
// Constraints:
39+
// 1 <= N <= 105
40+
// 1 <= arr[i] <= 105
41+
// 1 <= K <= N
42+
43+
44+
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
45+
46+
//{ Driver Code Starts
47+
//Initial function template for C++
48+
49+
#include<bits/stdc++.h>
50+
using namespace std;
51+
52+
// } Driver Code Ends
53+
//User function template for C++
54+
55+
class Solution{
56+
public:
57+
// arr : given array
58+
// l : starting index of the array i.e 0
59+
// r : ending index of the array i.e size-1
60+
// k : find kth smallest element and return using this function
61+
int kthSmallest(int arr[], int l, int r, int k) {
62+
//code here
63+
// vector<int>v;
64+
// for(int i=0;i<=r;i++)
65+
// v.push_back(arr[i]);
66+
// sort(v.begin(),v.end());
67+
// return v[k-1];
68+
69+
priority_queue<int>p;
70+
for(int i=0;i<=r;i++)
71+
p.push(arr[i]);
72+
73+
for(int i=1;i<=r+1-k;i++)
74+
p.pop();
75+
76+
return p.top();
77+
78+
}
79+
};
80+
81+
//{ Driver Code Starts.
82+
83+
int main()
84+
{
85+
int test_case;
86+
cin>>test_case;
87+
while(test_case--)
88+
{
89+
int number_of_elements;
90+
cin>>number_of_elements;
91+
int a[number_of_elements];
92+
93+
for(int i=0;i<number_of_elements;i++)
94+
cin>>a[i];
95+
96+
int k;
97+
cin>>k;
98+
Solution ob;
99+
cout<<ob.kthSmallest(a, 0, number_of_elements-1, k)<<endl;
100+
}
101+
return 0;
102+
}
103+
// } Driver Code Ends

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /