Compute n! under modulo p
Last Updated :
23 Jul, 2025
Given a large number n and a prime p, how to efficiently compute n! % p?
Examples :
Input: n = 5, p = 13
Output: 3
5! = 120 and 120 % 13 = 3
Input: n = 6, p = 11
Output: 5
6! = 720 and 720 % 11 = 5
A Naive Solution is to first compute n!, then compute n! % p. This solution works fine when the value of n! is small. The value of n! % p is generally needed for large values of n when n! cannot fit in a variable, and causes overflow. So computing n! and then using modular operator is not a good idea as there will be overflow even for slightly larger values of n and r.
Following are different methods.
Method 1 (Simple)
A Simple Solution is to one by one multiply result with i under modulo p. So the value of result doesn't go beyond p before next iteration.
C++
// Simple method to compute n! % p
#include <bits/stdc++.h>
using namespace std;
// Returns value of n! % p
int modFact(int n, int p)
{
if (n >= p)
return 0;
int result = 1;
for (int i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
// Driver program
int main()
{
int n = 25, p = 29;
cout << modFact(n, p);
return 0;
}
Java
// Simple method to compute n! % p
import java.io.*;
class GFG
{
// Returns value of n! % p
static int modFact(int n,
int p)
{
if (n >= p)
return 0;
int result = 1;
for (int i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
// Driver Code
public static void main (String[] args)
{
int n = 25, p = 29;
System.out.print(modFact(n, p));
}
}
// This code is contributed
// by aj_36
Python3
# Simple method to compute n ! % p
# Returns value of n ! % p
def modFact(n, p):
if n >= p:
return 0
result = 1
for i in range(1, n + 1):
result = (result * i) % p
return result
# Driver Code
n = 25; p = 29
print (modFact(n, p))
# This code is contributed by Shreyanshi Arun.
C#
// Simple method to compute n! % p
using System;
class GFG {
// Returns value of n! % p
static int modFact(int n, int p)
{
if (n >= p)
return 0;
int result = 1;
for (int i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
// Driver program
static void Main()
{
int n = 25, p = 29;
Console.Write(modFact(n, p));
}
}
// This code is contributed by Anuj_67
PHP
<?php
// PHP Simple method to compute n! % p
// Returns value of n! % p
function modFact($n, $p)
{
if ($n >= $p)
return 0;
$result = 1;
for ($i = 1; $i <= $n; $i++)
$result = ($result * $i) % $p;
return $result;
}
// Driver Code
$n = 25; $p = 29;
echo modFact($n, $p);
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Simple method to compute n! % p
// Returns value of n! % p
function modFact(n,p)
{
if (n >= p)
return 0;
let result = 1;
for (let i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
// Driver Code
let n = 25, p = 29;
document.write(modFact(n, p));
// This code is contributed by Rajput-Ji
</script>
Output :
5
Time Complexity : O(n).
Auxiliary Space : O(1) since using only constant space
Method 2 (Using Sieve)
The idea is based on below formula discussed here.
The largest possible power of a prime pi that divides n is,
?n/pi? + ?n/(pi2)? + ?n/(pi3)? + .....+ 0
Every integer can be written as multiplication of powers of primes. So,
n! = p1k1 * p2k2 * p3k3 * ....
Where p1, p2, p3, .. are primes and
k1, k2, k3, .. are integers greater than or equal to 1.
The idea is to find all primes smaller than n using Sieve of Eratosthenes. For every prime 'pi', find the largest power of it that divides n!. Let the largest power be ki. Compute piki % p using modular exponentiation. Multiply this with final result under modulo p.
Below is implementation of above idea.
C++
// C++ program to Returns n % p
// using Sieve of Eratosthenes
#include <bits/stdc++.h>
using namespace std;
// Returns largest power of p that divides n!
int largestPower(int n, int p)
{
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n) {
n /= p;
x += n;
}
return x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n! % p
int modFact(int n, int p)
{
if (n >= p)
return 0;
int res = 1;
// Use Sieve of Eratosthenes to find all primes
// smaller than n
bool isPrime[n + 1];
memset(isPrime, 1, sizeof(isPrime));
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n; j += i)
isPrime[j] = 0;
}
}
// Consider all primes found by Sieve
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of prime 'i' that divides n
int k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
// Driver method
int main()
{
int n = 25, p = 29;
cout << modFact(n, p);
return 0;
}
Java
import java.util.Arrays;
// Java program Returns n % p using Sieve of Eratosthenes
public class GFG {
// Returns largest power of p that divides n!
static int largestPower(int n, int p) {
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
x += n;
}
return x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
static int power(int x, int y, int p) {
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1) {
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n! % p
static int modFact(int n, int p) {
if (n >= p) {
return 0;
}
int res = 1;
// Use Sieve of Eratosthenes to find all primes
// smaller than n
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
// Consider all primes found by Sieve
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of prime 'i' that divides n
int k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
// Driver method
static public void main(String[] args) {
int n = 25, p = 29;
System.out.println(modFact(n, p));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to Returns n % p
# using Sieve of Eratosthenes
# Returns largest power of p that divides n!
def largestPower(n, p):
# Initialize result
x = 0
# Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n):
n //= p
x += n
return x
# Utility function to do modular exponentiation.
# It returns (x^y) % p
def power( x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more than
# or equal to p
while (y > 0) :
# If y is odd, multiply x with result
if (y & 1) :
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Returns n! % p
def modFact( n, p) :
if (n >= p) :
return 0
res = 1
# Use Sieve of Eratosthenes to find
# all primes smaller than n
isPrime = [1] * (n + 1)
i = 2
while(i * i <= n):
if (isPrime[i]):
for j in range(2 * i, n, i) :
isPrime[j] = 0
i += 1
# Consider all primes found by Sieve
for i in range(2, n):
if (isPrime[i]) :
# Find the largest power of prime 'i'
# that divides n
k = largestPower(n, i)
# Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p
return res
# Driver code
if __name__ =="__main__":
n = 25
p = 29
print(modFact(n, p))
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program Returns n % p using Sieve of Eratosthenes
using System;
public class GFG {
// Returns largest power of p that divides n!
static int largestPower(int n, int p) {
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
x += n;
}
return x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
static int power(int x, int y, int p) {
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1) {
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n! % p
static int modFact(int n, int p) {
if (n >= p) {
return 0;
}
int res = 1;
// Use Sieve of Eratosthenes to find all primes
// smaller than n
bool []isPrime = new bool[n + 1];
for(int i = 0;i<n+1;i++)
isPrime[i] = true;
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
// Consider all primes found by Sieve
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of prime 'i' that divides n
int k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
// Driver method
static public void Main() {
int n = 25, p = 29;
Console.WriteLine(modFact(n, p));
}
}
// This code is contributed by PrinciRaj19992
PHP
<?php
// PHP program to Returns n % p
// using Sieve of Eratosthenes
// Returns largest power of p that
// divides n!
function largestPower($n, $p)
{
// Initialize result
$x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while ($n)
{
$n = (int)($n / $p);
$x += $n;
}
return $x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
function power($x, $y, $p)
{
$res = 1; // Initialize result
$x = $x % $p; // Update x if it is more
// than or equal to p
while ($y > 0)
{
// If y is odd, multiply x with result
if ($y & 1)
$res = ($res * $x) % $p;
// y must be even now
$y = $y >> 1; // y = y/2
$x = ($x * $x) % $p;
}
return $res;
}
// Returns n! % p
function modFact($n, $p)
{
if ($n >= $p)
return 0;
$res = 1;
// Use Sieve of Eratosthenes to find
// all primes smaller than n
$isPrime = array_fill(0, $n + 1, true);
for ($i = 2; $i * $i <= $n; $i++)
{
if ($isPrime[$i])
{
for ($j = 2 * $i; $j <= $n; $j += $i)
$isPrime[$j] = 0;
}
}
// Consider all primes found by Sieve
for ($i = 2; $i <= $n; $i++)
{
if ($isPrime[$i])
{
// Find the largest power of
// prime 'i' that divides n
$k = largestPower($n, $i);
// Multiply result with (i^k) % p
$res = ($res * power($i, $k, $p)) % $p;
}
}
return $res;
}
// Driver Code
$n = 25;
$p = 29;
echo modFact($n, $p);
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program Returns n % p
// using Sieve of Eratosthenes
// Returns largest power of p
// that divides n!
function largestPower(n, p)
{
// Initialize result
let x = 0;
// Calculate x = n/p + n/(p^2) +
// n/(p^3) + ....
while (n > 0) {
n = parseInt(n / p, 10);
x += n;
}
return x;
}
// Utility function to do
// modular exponentiation.
// It returns (x^y) % p
function power(x, y, p)
{
// Initialize result
let res = 1;
// Update x if it is more than or
x = x % p;
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1) {
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n! % p
function modFact(n, p) {
if (n >= p) {
return 0;
}
let res = 1;
// Use Sieve of Eratosthenes
// to find all primes
// smaller than n
let isPrime = new Array(n + 1);
for(let i = 0;i<n+1;i++)
isPrime[i] = true;
for (let i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (let j = 2 * i; j <= n; j += i)
{
isPrime[j] = false;
}
}
}
// Consider all primes found by Sieve
for (let i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of
// prime 'i' that divides n
let k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
let n = 25, p = 29;
document.write(modFact(n, p));
</script>
Output:
5
Time Complexity: O(nlog(logn))
Auxiliary Space: O(n)
This is an interesting method, but time complexity of this is more than Simple Method as time complexity of Sieve itself is O(n log log n). This method can be useful if we have list of prime numbers smaller than or equal to n available to us.
Method 3 (Using Wilson's Theorem)
Wilson’s theorem states that a natural number p > 1 is a prime number if and only if
(p - 1) ! ? -1 mod p
OR (p - 1) ! ? (p-1) mod p
Note that n! % p is 0 if n >= p. This method is mainly useful when p is close to input number n. For example (25! % 29). From Wilson's theorem, we know that 28! is -1. So we basically need to find [ (-1) * inverse(28, 29) * inverse(27, 29) * inverse(26) ] % 29. The inverse function inverse(x, p) returns inverse of x under modulo p (See this for details).
C++
// C++ program to compute n! % p using Wilson's Theorem
#include <bits/stdc++.h>
using namespace std;
// Utility function to do modular exponentiation.
// It returns (x^y) % p
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to find modular inverse of a under modulo p
// using Fermat's method. Assumption: p is prime
int modInverse(int a, int p)
{
return power(a, p - 2, p);
}
// Returns n! % p using Wilson's Theorem
int modFact(int n, int p)
{
// n! % p is 0 if n >= p
if (p <= n)
return 0;
// Initialize result as (p-1)! which is -1 or (p-1)
int res = (p - 1);
// Multiply modulo inverse of all numbers from (n+1)
// to p
for (int i = n + 1; i < p; i++)
res = (res * modInverse(i, p)) % p;
return res;
}
// Driver method
int main()
{
int n = 25, p = 29;
cout << modFact(n, p);
return 0;
}
Java
// Java program to compute n! % p
// using Wilson's Theorem
class GFG
{
// Utility function to do
// modular exponentiation.
// It returns (x^y) % p
static int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more
// than or equal to p
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to find modular
// inverse of a under modulo p
// using Fermat's method.
// Assumption: p is prime
static int modInverse(int a, int p)
{
return power(a, p - 2, p);
}
// Returns n! % p using
// Wilson's Theorem
static int modFact(int n, int p)
{
// n! % p is 0 if n >= p
if (p <= n)
return 0;
// Initialize result as (p-1)!
// which is -1 or (p-1)
int res = (p - 1);
// Multiply modulo inverse of
// all numbers from (n+1) to p
for (int i = n + 1; i < p; i++)
res = (res * modInverse(i, p)) % p;
return res;
}
// Driver Code
public static void main(String[] args)
{
int n = 25, p = 29;
System.out.println(modFact(n, p));
}
}
// This code is contributed by mits
Python3
def gcdExtended(a,b):
if(b==0):
return a,1,0
g,x1,y1=gcdExtended(b,a%b)
x1,y1 = y1,x1-(a//b)*y1
return g,x1,y1
# Driver code
for _ in range(int(input())):
n,m=map(int,input().split())
# if n>m, then there is a m in (1*2*3..n) % m such that
# m % m=0 then whole ans is 0
if(n>=m):
print(0)
else:
s=1
# Using Wilson's Theorem, (m-1)! % m = m - 1
# Since we need (1*2*3..n) % m
# it can be divided into two parts
# ( ((1*2*3...n) % m) * ((n+1)*(n+2)*...*(m-1)) )%m=m-1
# We only need to calculate 2nd part i.e. let s=((n+1)*(n+2)*...*(m-1)) ) % m
for i in range(n+1,m):
s=(s*i)%m
# Then we divide (m-1) by s under modulo m means modulo inverse of s under m
# It can be done by taking gcd extended
g,a,b=gcdExtended(s,m)
a=a%m
# ans is (m-1)*(modulo inverse of s under m)
print(((m-1)*a)%m)
# This code is contributed by Himanshu Kaithal
C#
// C# program to compute n! % p
// using Wilson's Theorem
using System;
// Utility function to do modular
// exponentiation. It returns (x^y) % p
class GFG
{
public int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more
// than or equal to p
while (y > 0)
{
// If y is odd, multiply
// x with result
if((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to find modular inverse
// of a under modulo p using Fermat's
// method. Assumption: p is prime
public int modInverse(int a, int p)
{
return power(a, p - 2, p);
}
// Returns n! % p using
// Wilson's Theorem
public int modFact(int n, int p)
{
// n! % p is 0 if n >= p
if (p <= n)
return 0;
// Initialize result as (p-1)!
// which is -1 or (p-1)
int res = (p - 1);
// Multiply modulo inverse of all
// numbers from (n+1) to p
for (int i = n + 1; i < p; i++)
res = (res * modInverse(i, p)) % p;
return res;
}
// Driver method
public static void Main()
{
GFG g = new GFG();
int n = 25, p = 29;
Console.WriteLine(g.modFact(n, p));
}
}
// This code is contributed by Soumik
PHP
<?php
// PHP program to compute
// n!% p using Wilson's Theorem
// Utility function to do
// modular exponentiation.
// It returns (x^y) % p
function power($x, $y, $p)
{
$res = 1; // Initialize result
$x = $x % $p; // Update x if it is
// more than or equal to p
while ($y > 0)
{
// If y is odd, multiply
// x with result
if ($y & 1)
$res = ($res * $x) % $p;
// y must be even now
$y = $y >> 1; // y = y/2
$x = ($x * $x) % $p;
}
return $res;
}
// Function to find modular
// inverse of a under modulo
// p using Fermat's method.
// Assumption: p is prime
function modInverse($a, $p)
{
return power($a, $p - 2, $p);
}
// Returns n! % p
// using Wilson's Theorem
function modFact( $n, $p)
{
// n! % p is 0
// if n >= p
if ($p <= $n)
return 0;
// Initialize result as
// (p-1)! which is -1 or (p-1)
$res = ($p - 1);
// Multiply modulo inverse
// of all numbers from (n+1)
// to p
for ($i = $n + 1; $i < $p; $i++)
$res = ($res *
modInverse($i, $p)) % $p;
return $res;
}
// Driver Code
$n = 25; $p = 29;
echo modFact($n, $p);
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to compute n! % p
// using Wilson's Theorem
// Utility function to do modular
// exponentiation. It returns (x^y) % p
function power(x, y, p)
{
let res = 1; // Initialize result
x = x % p; // Update x if it is more
// than or equal to p
while (y > 0)
{
// If y is odd, multiply
// x with result
if((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to find modular inverse
// of a under modulo p using Fermat's
// method. Assumption: p is prime
function modInverse(a, p)
{
return power(a, p - 2, p);
}
// Returns n! % p using
// Wilson's Theorem
function modFact(n, p)
{
// n! % p is 0 if n >= p
if (p <= n)
return 0;
// Initialize result as (p-1)!
// which is -1 or (p-1)
let res = (p - 1);
// Multiply modulo inverse of all
// numbers from (n+1) to p
for (let i = n + 1; i < p; i++)
res = (res * modInverse(i, p)) % p;
return res;
}
let n = 25, p = 29;
document.write(modFact(n, p));
// This code is contributed by divyeshrabadiya07.
</script>
Output:
5
Time complexity of this method is O((p-n)*Logn)
Auxiliary Space : O(1) because using only constant variables
Method 4 (Using Primality Test Algorithm)
1) Initialize: result = 1
2) While n is not prime
result = (result * n) % p
3) result = (result * (n-1)) % p // Using Wilson's Theorem
4) Return result.
Note that time complexity step 2 of above algorithm depends on the primality test algorithm being used and value of the largest prime smaller than n. The AKS algorithm for example takes O(Log 10.5 n) time.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem