Home
Gaurav's GitHub Page
Cancel

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 ...

Find Number of Divisors

Problem Description Given a number N, find the number of divisors of that number Solution To solve this problem optimally, we will make use of Smallest Prime Factor algorithm covered previously....

Smallest Prime Factor

Problem Description Given N, find the smallest prime factor for all numbers from [2-N] Solution The solution to this problem is similar to how we found out all prime numbers till N using Sieve o...

Find Prime Numbers

Problem Description Given a number N, return a list of all prime numbers till N. Solution This can be optimally solved using Sieve of Eratosthenes algorithm. First we creat an boolean array ...

Check Prime

Problem Description Given a number N check if it’s a prime number. Solution The main this to remember here is that we need to check only till square root of N, because if i is a factor of N, N/i...

A%M == B%M

Problem Description Given two integers A and B, find the greatest possible positive integer M, such that A % M = B % M. Solution This problem needs some derivation: A%M = B%M => A%M - B%M...

Change Array[i] to Array[Array[i]]

Problem Description Given a list of elements (an array) and a postive integer m, replace all elments such that Array[i] = Array[Array[i]]. All elements in the array are in the range [0, N-1]. Sol...

Merge Two Sorted LinkedList

PROBLEM DESCRIPTION Merge two LinkedList which are sorted such that the final LinkedList is also sorted. (Without creating a new LinkedList) SOLUTION This problem is also part of AlgoExpert: htt...

Maximum Sum Sub-Matrix

Problem Description Given a 2D matrix, find the maximum possible sum of sub-matrices in that matrix. Solution The brute force approach is to get all sub-matrices, calculate the sum and update th...