-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
SampleCode.cpp
34 lines (31 loc) · 927 Bytes
/
SampleCode.cpp
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
34
/*
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;
}