Problem Title: Kth Smallest

Problem Title: Kth Smallest

Problem Statement

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

  • Note : l and r denotes the starting and ending index of the array.

Example 1

Input:
n = 6
arr[] = 7 10 4 3 20 15
k = 3
l=0 r =5
Output : 7
Explanation : 3rd smallest element in the given array is 7.        

Example 2

Input: 5
arr[] = 7 10 4 20 15
k = 4
l=0 r =4
Output : 15
Explanation : 4th smallest element in the given array is 15.        

Your task

You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer k as input and returns the kth smallest element.

  • Expected Time Complexity: O(n)
  • Expected Auxiliary Space: O(1)

Constraints

  • 1 <= N <= 10^5
  • 1 <= arr[i] <= 10^5
  • 1 <= K <= N
  • l= 0
  • r=N-1


Java Code

class Solution{
    public static int kthSmallest(int[] arr, int l, int r, int k) 
    { 
        //Your code here
        Arrays.sort(arr);
        return arr[k-1];
    } 
}        

Explanation

The idea is to sort the array and return the kth element of the array.

  1. Sort the array.
  2. Return the kth element of the array.

  • Time Complexity: O(nlogn)
  • Space Complexity: O(1)



要查看或添加评论,请登录

Dhruva Bhat S N的更多文章

  • Problem Title : Left Rotate Matrix K times

    Problem Title : Left Rotate Matrix K times

    Problem Statement: You are given an integer k and matrix mat. Rotate the elements of the given matrix to the left k…

  • Problem Title : Summed Matrix

    Problem Title : Summed Matrix

    Problem Statement: A matrix is constructed of size and given an integer ‘’. The value at every cell of the matrix is…

  • Problem Title: K Sized Subarray Maximum

    Problem Title: K Sized Subarray Maximum

    Problem Statement: Given an array arr[] of size N and an integer K. Find the maximum for each and every contiguous…

  • Problem title: Minimum Platforms

    Problem title: Minimum Platforms

    Problem Statement: Given arrival and departure times of all trains that reach a railway station. Find the minimum…

  • Problem Title: Count Inversions

    Problem Title: Count Inversions

    Problem Description Given an array of integers. Find the Inversion Count in the array.

  • Problem Title: Minimize the Heights II

    Problem Title: Minimize the Heights II

    Problem Statement Given an array denoting heights of towers and a positive integer . For each tower, you must perform…

  • Problem Title : Sort 0s, 1s and 2s

    Problem Title : Sort 0s, 1s and 2s

    Problem Description: Given an array A of size N containing 0s, 1s, and 2s; you need to sort the array in ascending…

  • Problem Title: Minimum number of jumps

    Problem Title: Minimum number of jumps

    Problem Statement : Given an array arr[] of size n of non-negative integers. Each array element represents the maximum…

  • Problem Title : Find all pairs with a given sum

    Problem Title : Find all pairs with a given sum

    Problem Statement Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all…

  • Problem Title: Kadane's Algorithm

    Problem Title: Kadane's Algorithm

    This Problem is listed in GFG Sheet Problem Statement Given an array arr[] of n integers. Find the contiguous…

社区洞察

其他会员也浏览了