DEV Community

Tharunya K R
Tharunya K R

Posted on

Find the Kth Smallest Element in an Array

Problem

Given an integer array arr[] and an integer k, find the kth smallest element.

The kth smallest element is determined based on the sorted order.

Example 1
Input: arr = [10, 5, 4, 3, 48, 6, 2, 33, 53, 10], k = 4
Output: 5
Example 2
Input: arr = [7, 10, 4, 3, 20, 15], k = 3
Output: 7

Approach

Sort the array in ascending order.
Return the element at index k-1.

Python Code

class Solution:
def kthSmallest(self, arr, k):
arr.sort()
return arr[k - 1]

Output:

Kth smallest element: 5

Top comments (0)