Home
Gaurav's GitHub Page
Cancel

Radix Sort

Problem Description Implement Radix Sort. Solution Find the number of digits of the max element in the given array Initialize an Array “bucket” of ArrayList with size 10. This will be used ...

Aggressive Cows

Problem Description Given an array of length N, where each element denotes the position of a stall. Now you have N stalls and an integer K which denotes the number of cows that are aggressive. To ...

Find Square Root (Binary Search Approach)

Problem Description Given a non-negative integer x, compute and return the square root of x. Leetcode: Sqrt(x) Constraints: 0 <= x <= 2^31 - 1 Solution The important part in this code...

Shifted Binary Search

Problem Description Given a sorted array of integers A of size N and an integer B. Array A is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ). Y...

Count Inversion

Problem Description Inversion Count for an array indicates – how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is s...

Two Arrays, Find Pairs A[i] > B[j]

Problem Description Given two Arrays, find the count of all pairs such that A[i] > B[j] Solution The brute force is to find all pairs such that A[i] > B[j] in the two arrays. To optimize t...

Duplicate Zeroes

Problem Description Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right. Duplicate Zeros Note that elements beyond the length of...

Merge Sort

Problem Description Implement Merge Sort. Solution We will make use of a problem we solved earlier: Sort a sub-array given s,m,e. If we carefully think about any given array, we can divide it in...

Sort Sub-Array in Array Given s,m,e

Problem Description Give an array of N elements and three indices s, m and e in which [s,m] sub-array is sorted and [m+1,e] is also sorted. You need to sort the complete sub-array from [s,e]. Sol...

Find Kth Smallest Element

Problem Description Given N array elements, find Kth smallest element. Problem Constraints 1 <= |A| <= 100000 1 <= B <= min(|A|, 500) 1 <= A[i] <= 109 Solution Since B ...