DEV Community

Santhosh V
Santhosh V

Posted on

CA 07 - Kth Smallest

Problem

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

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

Output
Example 1
Output: 5
Example 2
Output: 7
My Approach

To solve this problem, I used the sorting method.

First, I sort the array in ascending order.

After sorting, the kth smallest element will be present at index k-1.

So I return the element at position k-1.

This works because sorting arranges all elements in increasing order, making it easy to directly access the kth smallest element.

This approach is simple and easy to implement.

Code

def kth_smallest(arr, k):
    arr.sort()
    return arr[k - 1]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)