AfterAcademy Tech
•
12 Oct 2019

Difficulty : Medium
Asked in : Amazon, Google
Problem Description: Given an array A[] of n elements and a positive integer K, find the Kth smallest element in the array. It is given that all array elements are distinct.
For Example :
Input : A[] = {10, 3, 6, 9, 2, 4, 15, 23}, K = 4
Output: 6
Input : A[] = {5, -8, 10, 37, 101, 2, 9}, K = 6
Output: 37
Possible follow-up questions to ask the interviewer:-
We will be discussing four possible solutions for this problem:-
The idea is to sort the array to arrange the numbers in increasing order and then returning the Kth number from the start.
Pseudo-Code
int kthSmallest(int A[], int n, int K)
{
sort(A,n)
return A[K-1]
}
Complexity Analysis
Time Complexity: Sorting the array + Picking Kth element from the start = O(nlogn) + O(1) = O(nlogn)
Space Complexity: If you use merge sort, then O(n), else if you use heap sort, its O(1).
Critical ideas to think!
This is a good example of problem-solving via a heap data structure. The basic idea here is to create a min-heap of all n elements and then extract the minimum element K times. The last element to be extracted will be the Kth smallest element.
Solution Steps
Pseudo-Code
int kthSmallest(int A[], int n, int K)
{
build_minHeap(A, n)
for( i = 0 to K-1 )
extractMin(A)
return A[0]
}
Complexity Analysis
Time Complexity: Building the min heap of n elements + Extracting min element K-1 times = O(n) + (K-1) * log(n) = O( n + Klogn)
Space Complexity: O(1) (Why?)
Critical ideas to think!
The idea is to construct a max-heap of the smaller elements. Since the root of the max-heap is the largest element of the heap, you should use this to your advantage to track the top K smallest elements in heap and then returning the root.
Solution Steps
3. Return the root of the heap.
Pseudo-Code
int kthSmallest(int A[], int n, int K)
{
build_maxHeap(A, K)
for ( i = K to n-1 )
{
if ( A[i] < A[0] )
{
A[0] = A[i] // Now A[i] the new root
heapify(A, 0)
}
}
return A[0]
}
Complexity Analysis
Time Complexity: Building max-heap of k elements + Inserting n-K elements to the heap = O(K) + O((n-K)logK) = O(K + (n-K)logK)
Space Complexity: O(1)
Critical ideas to think!
This approach is similar to the quick sort algorithm where we use the partition on the input array recursively. But unlike quicksort, which processes both sides of the array recursively, this algorithm works on only one side of the partition. We recur for either the left or right side according to the position of pivot.
Solution Steps
Pseudo-Code
// Original value for left = 0 and right = n-1
int kthSmallest(int A[], int left, int right, int K)
{
if (left == right)
return A[left]
int pos = partition(A, left, right)
count = pos - left + 1
if ( count == K )
return A[pos]
else if ( count > K )
return kthSmallest(A, left, pos-1, K)
else
return kthSmallest(A, pos+1, right, K-i)
}
int partition(int A[], int l, int r)
{
int x = A[r]
int i = l-1
for ( j = l to r-1 )
{
if (A[j] <= x)
{
i = i + 1
swap(A[i], A[j])
}
}
swap(A[i+1], A[r])
return i+1
}
Complexity Analysis
Time Complexity: The worst-case time complexity for this algorithm is O(n²), but it can be improved if we choose the pivot element randomly. If we randomly select the pivot, the expected time complexity would be linear, O(n).
Space Complexity: O(logn) average for recursion call stack (Think!)
Critical ideas to think!

Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array A[] of size n, you need to find the next greater element for each element in the array. Return an array that consists of the next greater element of A[i] at index i.

AfterAcademy Tech
Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times.

AfterAcademy Tech
You are given an array A[] consisting of n elements. You need to find and return the number which appears more than n/2 times.

AfterAcademy Tech
Given an array arr[] of size n , write a program to find the largest element in it. To find the largest element we can just traverse the array in one pass and find the largest element by maintaining a max variable.
