Skip to content

Commit df6c83b

Browse files
committed
find kth min and max
1 parent c50857d commit df6c83b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Array/kth_min_and_max.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# both the function will take O(n*log(n)) time
2+
3+
def kthMin(arr, k):
4+
5+
# sort the array
6+
arr.sort()
7+
8+
# return kth element in the sorted array
9+
return arr[k-1]
10+
11+
12+
arr1 = [2, 4, 7, 1, 23, 13]
13+
14+
print(kthMin(arr1, k=1))
15+
16+
17+
def kthMax(arr, k):
18+
19+
# sort the given array in reverse
20+
arr.sort(reverse=True)
21+
22+
# return the kth element from the array
23+
return arr[k-1]
24+
25+
26+
arr2 = [1, 4, 2, 6, 9]
27+
print(kthMax(arr2, k=2))

0 commit comments

Comments
 (0)