Problem Description Given an array of +ve integers, find maximum k such that: {max subarray sum of length k <= B}, where B is a given input +ve integer. Solution We have given a number B and ...
Ath Magical Number
Problem Description Given A, B and C, find Ath magical number. Note: A number is said to be magical, if it’s divisible by B or C. Solution package com.gauk; public class MagicNumberModded { ...
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...