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

Kth Smallest Element

Easy
Asked in companies
SnapdealMicrosoftMedia.net

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.


Example
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.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.

Approaches

01 Approach

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