forked from anujji1999/Computer-Society
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleCode.cpp
More file actions
33 lines (29 loc) · 938 Bytes
/
SampleCode.cpp
File metadata and controls
33 lines (29 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
Program Description - This program return Kth Smallest Element From An Array.
Time Complexity: O(n * log n)
Space Complexity: O(1)
Solution Description: Sorting the Array with STL ( Standard Template Library ) and then returning Kth Element.
Assumption(s)[Optional]: K is less than the size of Array.
*/
#include <iostream>
using namespace std;
int kthSmallestElement(int Array[], int sizeOfArray, int k) {
sort(Array , Array + sizeOfArray);
return Array[ k - 1 ];
}
int main() {
int sizeOfArray;
cout << "\nEnter Size\t:\t";
cin >> sizeOfArray;
int *Array = new int[sizeOfArray];
cout << "\nEnter Array Elements\n";
for ( int itr = 0; itr < sizeOfArray; itr++ ) {
cin >> Array[itr];
}
int k;
cout << "Enter K\t:\t";
cin >> k;
cout << "\nKth Smallest Element\t:\t" << kthSmallestElement( Array , sizeOfArray , k ) << endl;
delete[] Array;
return 0;
}