## Arranging Cupcakes Solution

ProblemOn the day of the party, the Chef was over-seeing all the food arrangements as well, ensuring that every item was in its designated position. The host was satisfied with everything except the cupcakes. He noticed they were arranged neatly in the shape of a rectangle. He asks the Chef to […]

## Is Fibo Solution

ProblemYou are given an integer, N. Write a program to determine if N is an element of the Fibonacci sequence.The first few elements of the Fibonacci sequence are 0, 1, 1, 2, 3, 5, 8, 13…. Hackerrank Problem Link Input The first line of input contains a single integer T, number of test cases. T lines […]

## Rectangular Game Solution

Problem You are given an infinite 2d grid with the bottom left cell referenced as (1,1). All the cells contain a value of zero initially. Let’s play a game? The game consists of N steps wherein each step you are given two integers a and b. The value of each of the cells in the coordinate […]

## Fibonacci Numbers in O(logn) [Matrix Exponentiation]

Matrix multiplication can be performed in O(logn) by using matrix exponentiation. It is one of the most used techniques

## Fibonacci Numbers Solution (O(1) auxiliary space)

In mathematics, the Fibonacci series (Fn) is a sequence, such that each number in the series is the sum of the two preceding ones, starting from 0 and 1. That is, and for n > 1, Problem Statement Input a number n, print n-th Fibonacci Number.  Examples:  Input : n = […]

## Pascal’s Triangle

In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients. It can also be viewed as: each number in Pascal’s triangle is the sum of the two numbers directly above it as shown: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 […]

## Minimum number of moves to equal array elements – LeetCode Solution [Medium]

Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal. In one move, you can increment n – 1 elements of the array by 1. Problem Statement https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ Example 1: Input: nums = [1,2,3] Output: 3 Explanation: [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] Minimum number of […]

## Implementing basic Mathematical operations (Euclidean algorithm for GCD)

Problem Statement – Given two non-negative integers a and b, you’ve to find the Greatest Common Divisor (GCD) or Highest Common Factor (HCF) of the two numbers. In other words, find the largest number that divides them both. Euclidean Algorithm The Euclidean algorithm is one of the oldest numerical algorithms to compute […]

## Implementing basic Mathematical operations (Exponential)

You’re given two integers a and n. You’ve to compute a^n. Using inbuilt-function pow Output 7 ^ 3 = 343 Drawback of this method is that it will cause int overflow, if we need to compute: A^n mod p, where A can be as large as 10^18 and p is a […]

## Matrix-chain multiplication

We are given a sequence (chain) (M1, M2, M3…Mn) of n matrices to be multiplied and we need to find the most efficient way to multiply these matrices together. Now, if the chain of matrices is M1*M2*M3*M4, then it could be evaluated in various distinct ways without affecting the final product: (M1(M2(M3*M4))) […]