Approach:
Step 1 Take the array
Step 2 Sort the array
Step 3 Return element at index k minus 1
Why k minus 1 means???
Array index starts from 0
so 1st element index 0
2nd element index 1
so kth element index k minus 1
Code
def kthSmallest(arr,k):
arr.sort()
return arr[k-1]
Limitation:
This is not the best method for large data
because sorting whole array is unnecessary
but this is simplest way to do
Top comments (0)