Day 2 - Arrays and Strings
Arrays
Practice Questions
Find Kth Max and Min Element

Find Kth Smallest and Largest Element

Description

  • Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the Kth smallest and largest element in the given array. It is given that all array elements are distinct.

Examples

> Case 1:
    Input: arr = [21, 43, 32, 30], K = 2
    Output: [30, 32]
 
> Case2:
    Input: arr = [10, 5, 8, 3, 2, 7, 1], K = 3
    Output: [3, 7]

Constraints

  • 1 <= N <= 10^5
  • 1 <= arr[i] <= 10^5
  • 1 <= K <= N

Code