Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: 21 Nov, 2020

Kth Smallest Element

Asked in companies

Problem statement

You are given an array of integers 'ARR' of size 'N' and another integer 'K'.

Your task is to find and return 'K'th smallest value present in the array.

Note: All the elements in the array are distinct.

If 'N' is 5 and 'K' is 3 and the array is 7, 2, 6, 1, 9

Sorting the array we get 1, 2, 6, 7, 9

Hence the 3rd smallest number is 6.
Input format
The first line contains two space-separated integers ‘N’ representing the size of the array and ‘K’.

The second line contains 'N' space-separated integers that represent elements of the array 'ARR'.
Output format
Print a single line that contains a single integer which is the 'Kth' smallest element of the array.
You do not need to print anything; it has already been taken care of. Just implement the given function.


01 Approach

  1. Sort the elements of ‘ARR’ using function ‘SORT’
  2. Return element at ('K' - 1)th index