Modular multiplicative inverse
Last Updated :
23 Jul, 2025
Given two integers A and M, find the modular multiplicative inverse of A under modulo M.
The modular multiplicative inverse is an integer X such that:
A X ≡ 1 (mod M)
Note: The value of X should be in the range {1, 2, ... M-1}, i.e., in the range of integer modulo M. ( Note that X cannot be 0 as A*0 mod M will never be 1). The multiplicative inverse of "A modulo M" exists if and only if A and M are relatively prime (i.e. if gcd(A, M) = 1)
Examples:
Input: A = 3, M = 11
Output: 4
Explanation: Since (4*3) mod 11 = 1, 4 is modulo inverse of 3(under 11).
One might think, 15 also as a valid output as "(15*3) mod 11"
is also 1, but 15 is not in range {1, 2, ... 10}, so not valid.
Input: A = 10, M = 17
Output: 12
Explamation: Since (10*12) mod 17 = 1, 12 is modulo inverse of 10(under 17).
Naive Approach: To solve the problem, follow the below idea:
A naive method is to try all numbers from 1 to m. For every number x, check if (A * X) % M is 1
Below is the implementation of the above approach:
C++
// C++ program to find modular
// inverse of A under modulo M
#include <bits/stdc++.h>
using namespace std;
// A naive method to find modular
// multiplicative inverse of 'A'
// under modulo 'M'
int modInverse(int A, int M)
{
for (int X = 1; X < M; X++)
if (((A % M) * (X % M)) % M == 1)
return X;
}
// Driver code
int main()
{
int A = 3, M = 11;
// Function call
cout << modInverse(A, M);
return 0;
}
Java
// Java program to find modular inverse
// of A under modulo M
import java.io.*;
class GFG {
// A naive method to find modulor
// multiplicative inverse of A
// under modulo M
static int modInverse(int A, int M)
{
for (int X = 1; X < M; X++)
if (((A % M) * (X % M)) % M == 1)
return X;
return 1;
}
// Driver Code
public static void main(String args[])
{
int A = 3, M = 11;
// Function call
System.out.println(modInverse(A, M));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python
# Python3 program to find modular
# inverse of A under modulo M
# A naive method to find modulor
# multiplicative inverse of A
# under modulo M
def modInverse(A, M):
for X in range(1, M):
if (((A % M) * (X % M)) % M == 1):
return X
return -1
# Driver Code
if __name__ == "__main__":
A = 3
M = 11
# Function call
print(modInverse(A, M))
# This code is contributed by Nikita Tiwari.
C#
// C# program to find modular inverse
// of A under modulo M
using System;
class GFG {
// A naive method to find modulor
// multiplicative inverse of A
// under modulo M
static int modInverse(int A, int M)
{
for (int X = 1; X < M; X++)
if (((A % M) * (X % M)) % M == 1)
return X;
return 1;
}
// Driver Code
public static void Main()
{
int A = 3, M = 11;
// Function call
Console.WriteLine(modInverse(A, M));
}
}
// This code is contributed by anuj_67.
JavaScript
<script>
// Javascript program to find modular
// inverse of a under modulo m
// A naive method to find modulor
// multiplicative inverse of
// 'a' under modulo 'm'
function modInverse(a, m)
{
for(let x = 1; x < m; x++)
if (((a % m) * (x % m)) % m == 1)
return x;
}
// Driver Code
let a = 3;
let m = 11;
// Function call
document.write(modInverse(a, m));
// This code is contributed by _saurabh_jaiswal.
</script>
PHP
<?php
// PHP program to find modular
// inverse of A under modulo M
// A naive method to find modulor
// multiplicative inverse of
// A under modulo M
function modInverse( $A, $M)
{
for ($X = 1; $X < $M; $X++)
if ((($A%$M) * ($X%$M)) % $M == 1)
return $X;
}
// Driver Code
$A = 3;
$M = 11;
// Function call
echo modInverse($A, $M);
// This code is contributed by anuj_67.
?>
Time Complexity: O(M)
Auxiliary Space: O(1)
Modular multiplicative inverse when M and A are coprime or gcd(A, M)=1:
The idea is to use Extended Euclidean algorithms that take two integers 'a' and 'b', then find their gcd, and also find 'x' and 'y' such that
ax + by = gcd(a, b)
To find the multiplicative inverse of 'A' under 'M', we put b = M in the above formula. Since we know that A and M are relatively prime, we can put the value of gcd as 1.
Ax + My = 1
If we take modulo M on both sides, we get
Ax + My ≡ 1 (mod M)
We can remove the second term on left side as 'My (mod M)' would always be 0 for an integer y.
Ax ≡ 1 (mod M)
So the 'x' that we can find using Extended Euclid Algorithm is the multiplicative inverse of 'A'
Below is the implementation of the above approach:
C++
// C++ program to find multiplicative modulo
// inverse using Extended Euclid algorithm.
#include <bits/stdc++.h>
using namespace std;
// Function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y);
// Function to find modulo inverse of a
void modInverse(int A, int M)
{
int x, y;
int g = gcdExtended(A, M, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else {
// m is added to handle negative x
int res = (x % M + M) % M;
cout << "Modular multiplicative inverse is " << res;
}
}
// Function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y)
{
// Base Case
if (a == 0) {
*x = 0, *y = 1;
return b;
}
// To store results of recursive call
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
// Driver Code
int main()
{
int A = 3, M = 11;
// Function call
modInverse(A, M);
return 0;
}
// This code is contributed by khushboogoyal499
C
// C program to find multiplicative modulo inverse using
// Extended Euclid algorithm.
#include <stdio.h>
// C function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y);
// Function to find modulo inverse of a
void modInverse(int A, int M)
{
int x, y;
int g = gcdExtended(A, M, &x, &y);
if (g != 1)
printf("Inverse doesn't exist");
else {
// m is added to handle negative x
int res = (x % M + M) % M;
printf("Modular multiplicative inverse is %d", res);
}
}
// C function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y)
{
// Base Case
if (a == 0) {
*x = 0, *y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b % a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
// Driver Code
int main()
{
int A = 3, M = 11;
// Function call
modInverse(A, M);
return 0;
}
Java
// java program to find multiplicative modulo
// inverse using Extended Euclid algorithm.
public class GFG {
// Global Variables
public static int x;
public static int y;
// Function for extended Euclidean Algorithm
static int gcdExtended(int a, int b)
{
// Base Case
if (a == 0) {
x = 0;
y = 1;
return b;
}
// To store results of recursive call
int gcd = gcdExtended(b % a, a);
int x1 = x;
int y1 = y;
// Update x and y using results of recursive
// call
int tmp = b / a;
x = y1 - tmp * x1;
y = x1;
return gcd;
}
static void modInverse(int A, int M)
{
int g = gcdExtended(A, M);
if (g != 1) {
System.out.println("Inverse doesn't exist");
}
else {
// m is added to handle negative x
int res = (x % M + M) % M;
System.out.println(
"Modular multiplicative inverse is " + res);
}
}
// Driver code
public static void main(String[] args)
{
int A = 3, M = 11;
// Function Call
modInverse(A, M);
}
}
// The code is contributed by Gautam goel (gautamgoel962)
Python
# Python3 program to find multiplicative modulo
# inverse using Extended Euclid algorithm.
# Global Variables
x, y = 0, 1
# Function for extended Euclidean Algorithm
def gcdExtended(a, b):
global x, y
# Base Case
if (a == 0):
x = 0
y = 1
return b
# To store results of recursive call
gcd = gcdExtended(b % a, a)
x1 = x
y1 = y
# Update x and y using results of recursive
# call
x = y1 - (b // a) * x1
y = x1
return gcd
def modInverse(A, M):
g = gcdExtended(A, M)
if (g != 1):
print("Inverse doesn't exist")
else:
# m is added to handle negative x
res = (x % M + M) % M
print("Modular multiplicative inverse is ", res)
# Driver Code
if __name__ == "__main__":
A = 3
M = 11
# Function call
modInverse(A, M)
# This code is contributed by phasing17
C#
// C# program to find multiplicative modulo
// inverse using Extended Euclid algorithm.
using System;
public class GFG {
public static int x, y;
// Function for extended Euclidean Algorithm
static int gcdExtended(int a, int b)
{
// Base Case
if (a == 0) {
x = 0;
y = 1;
return b;
}
// To store results of recursive call
int gcd = gcdExtended(b % a, a);
int x1 = x;
int y1 = y;
// Update x and y using results of recursive
// call
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
// Function to find modulo inverse of a
static void modInverse(int A, int M)
{
int g = gcdExtended(A, M);
if (g != 1)
Console.Write("Inverse doesn't exist");
else {
// M is added to handle negative x
int res = (x % M + M) % M;
Console.Write(
"Modular multiplicative inverse is " + res);
}
}
// Driver Code
public static void Main(string[] args)
{
int A = 3, M = 11;
// Function call
modInverse(A, M);
}
}
// this code is contributed by phasing17
JavaScript
<script>
// JavaScript program to find multiplicative modulo
// inverse using Extended Euclid algorithm.
// Global Variables
let x, y;
// Function for extended Euclidean Algorithm
function gcdExtended(a, b){
// Base Case
if (a == 0)
{
x = 0;
y = 1;
return b;
}
// To store results of recursive call
let gcd = gcdExtended(b % a, a);
let x1 = x;
let y1 = y;
// Update x and y using results of recursive
// call
x = y1 - Math.floor(b / a) * x1;
y = x1;
return gcd;
}
function modInverse(a, m)
{
let g = gcdExtended(a, m);
if (g != 1){
document.write("Inverse doesn't exist");
}
else{
// m is added to handle negative x
let res = (x % m + m) % m;
document.write("Modular multiplicative inverse is ", res);
}
}
// Driver Code
{
let a = 3, m = 11;
// Function call
modInverse(a, m);
}
// This code is contributed by Gautam goel (gautamgoel962)
</script>
PHP
<?php
// PHP program to find multiplicative modulo
// inverse using Extended Euclid algorithm.
// Function to find modulo inverse of a
function modInverse($A, $M)
{
$x = 0;
$y = 0;
$g = gcdExtended($A, $M, $x, $y);
if ($g != 1)
echo "Inverse doesn't exist";
else
{
// m is added to handle negative x
$res = ($x % $M + $M) % $M;
echo "Modular multiplicative " .
"inverse is " . $res;
}
}
// function for extended Euclidean Algorithm
function gcdExtended($a, $b, &$x, &$y)
{
// Base Case
if ($a == 0)
{
$x = 0;
$y = 1;
return $b;
}
$x1;
$y1; // To store results of recursive call
$gcd = gcdExtended($b%$a, $a, $x1, $y1);
// Update x and y using results of
// recursive call
$x = $y1 - (int)($b/$a) * $x1;
$y = $x1;
return $gcd;
}
// Driver Code
$A = 3;
$M = 11;
// Function call
modInverse($A, $M);
// This code is contributed by chandan_jnu
?>
OutputModular multiplicative inverse is 4
Time Complexity: O(log M)
Auxiliary Space: O(log M), because of the internal recursion stack.
Iterative Implementation of the above approach:
C++
// Iterative C++ program to find modular
// inverse using extended Euclid algorithm
#include <bits/stdc++.h>
using namespace std;
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(A, M) = 1
int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
// q is quotient
int q = A / M;
int t = M;
// m is remainder now, process same as
// Euclid's algo
M = A % M, A = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
// Driver Code
int main()
{
int A = 3, M = 11;
// Function call
cout << "Modular multiplicative inverse is "
<< modInverse(A, M);
return 0;
}
// this code is contributed by shivanisinghss2110
C
// Iterative C program to find modular
// inverse using extended Euclid algorithm
#include <stdio.h>
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(A, M) = 1
int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
// q is quotient
int q = A / M;
int t = M;
// m is remainder now, process same as
// Euclid's algo
M = A % M, A = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
// Driver Code
int main()
{
int A = 3, M = 11;
// Function call
printf("Modular multiplicative inverse is %d\n",
modInverse(A, M));
return 0;
}
Java
// Iterative Java program to find modular
// inverse using extended Euclid algorithm
class GFG {
// Returns modulo inverse of a with
// respect to m using extended Euclid
// Algorithm Assumption: a and m are
// coprimes, i.e., gcd(A, M) = 1
static int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
// q is quotient
int q = A / M;
int t = M;
// m is remainder now, process
// same as Euclid's algo
M = A % M;
A = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
// Driver code
public static void main(String args[])
{
int A = 3, M = 11;
// Function call
System.out.println("Modular multiplicative "
+ "inverse is "
+ modInverse(A, M));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python
# Iterative Python 3 program to find
# modular inverse using extended
# Euclid algorithm
# Returns modulo inverse of a with
# respect to m using extended Euclid
# Algorithm Assumption: a and m are
# coprimes, i.e., gcd(A, M) = 1
def modInverse(A, M):
m0 = M
y = 0
x = 1
if (M == 1):
return 0
while (A > 1):
# q is quotient
q = A // M
t = M
# m is remainder now, process
# same as Euclid's algo
M = A % M
A = t
t = y
# Update x and y
y = x - q * y
x = t
# Make x positive
if (x < 0):
x = x + m0
return x
# Driver code
if __name__ == "__main__":
A = 3
M = 11
# Function call
print("Modular multiplicative inverse is",
modInverse(A, M))
# This code is contributed by Nikita tiwari.
C#
// Iterative C# program to find modular
// inverse using extended Euclid algorithm
using System;
class GFG {
// Returns modulo inverse of a with
// respect to m using extended Euclid
// Algorithm Assumption: a and m are
// coprimes, i.e., gcd(A, M) = 1
static int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
// q is quotient
int q = A / M;
int t = M;
// m is remainder now, process
// same as Euclid's algo
M = A % M;
A = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
// Driver Code
public static void Main()
{
int A = 3, M = 11;
// Function call
Console.WriteLine("Modular multiplicative "
+ "inverse is "
+ modInverse(A, M));
}
}
// This code is contributed by anuj_67.
JavaScript
<script>
// Iterative Javascript program to find modular
// inverse using extended Euclid algorithm
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(a, m) = 1
function modInverse(a, m)
{
let m0 = m;
let y = 0;
let x = 1;
if (m == 1)
return 0;
while (a > 1)
{
// q is quotient
let q = parseInt(a / m);
let t = m;
// m is remainder now,
// process same as
// Euclid's algo
m = a % m;
a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
// Driver Code
let a = 3;
let m = 11;
// Function call
document.write(`Modular multiplicative inverse is ${modInverse(a, m)}`);
// This code is contributed by _saurabh_jaiswal
</script>
PHP
<?php
// Iterative PHP program to find modular
// inverse using extended Euclid algorithm
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(a, m) = 1
function modInverse($A, $M)
{
$m0 = $M;
$y = 0;
$x = 1;
if ($m == 1)
return 0;
while ($A > 1)
{
// q is quotient
$q = (int) ($A / $M);
$t = $M;
// m is remainder now,
// process same as
// Euclid's algo
$M = $A % $M;
$A = $t;
$t = $y;
// Update y and x
$y = $x - $q * $y;
$x = $t;
}
// Make x positive
if ($x < 0)
$x += $m0;
return $x;
}
// Driver Code
$A = 3;
$M = 11;
// Function call
echo "Modular multiplicative inverse is: ",
modInverse($A, $M);
// This code is contributed by Anuj_67
?>
OutputModular multiplicative inverse is 4
Time Complexity: O(log m)
Auxiliary Space: O(1)
Modular multiplicative inverse when M is prime:
If we know M is prime, then we can also use Fermat's little theorem to find the inverse.
aM-1 ≡ 1 (mod M)
If we multiply both sides with a-1, we get
a-1 ≡ a M-2 (mod M)
Below is the implementation of the above approach:
C++
// C++ program to find modular inverse of A under modulo M
// This program works only if M is prime.
#include <bits/stdc++.h>
using namespace std;
// To find GCD of a and b
int gcd(int a, int b);
// To compute x raised to power y under modulo M
int power(int x, unsigned int y, unsigned int M);
// Function to find modular inverse of a under modulo M
// Assumption: M is prime
void modInverse(int A, int M)
{
int g = gcd(A, M);
if (g != 1)
cout << "Inverse doesn't exist";
else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
cout << "Modular multiplicative inverse is "
<< power(A, M - 2, M);
}
}
// To compute x^y under modulo m
int power(int x, unsigned int y, unsigned int M)
{
if (y == 0)
return 1;
int p = power(x, y / 2, M) % M;
p = (p * p) % M;
return (y % 2 == 0) ? p : (x * p) % M;
}
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver code
int main()
{
int A = 3, M = 11;
// Function call
modInverse(A, M);
return 0;
}
Java
// Java program to find modular
// inverse of A under modulo M
// This program works only if
// M is prime.
import java.io.*;
class GFG {
// Function to find modular inverse of a
// under modulo M Assumption: M is prime
static void modInverse(int A, int M)
{
int g = gcd(A, M);
if (g != 1)
System.out.println("Inverse doesn't exist");
else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
System.out.println(
"Modular multiplicative inverse is "
+ power(A, M - 2, M));
}
}
static int power(int x, int y, int M)
{
if (y == 0)
return 1;
int p = power(x, y / 2, M) % M;
p = (int)((p * (long)p) % M);
if (y % 2 == 0)
return p;
else
return (int)((x * (long)p) % M);
}
// Function to return gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver Code
public static void main(String args[])
{
int A = 3, M = 11;
// Function call
modInverse(A, M);
}
}
// This code is contributed by Nikita Tiwari.
Python
# Python3 program to find modular
# inverse of A under modulo M
# This program works only if M is prime.
# Function to find modular
# inverse of A under modulo M
# Assumption: M is prime
def modInverse(A, M):
g = gcd(A, M)
if (g != 1):
print("Inverse doesn't exist")
else:
# If A and M are relatively prime,
# then modulo inverse is A^(M-2) mod M
print("Modular multiplicative inverse is ",
power(A, M - 2, M))
# To compute x^y under modulo M
def power(x, y, M):
if (y == 0):
return 1
p = power(x, y // 2, M) % M
p = (p * p) % M
if(y % 2 == 0):
return p
else:
return ((x * p) % M)
# Function to return gcd of a and b
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
# Driver Code
if __name__ == "__main__":
A = 3
M = 11
# Function call
modInverse(A, M)
# This code is contributed by Nikita Tiwari.
C#
// C# program to find modular
// inverse of a under modulo M
// This program works only if
// M is prime.
using System;
class GFG {
// Function to find modular
// inverse of A under modulo
// M Assumption: M is prime
static void modInverse(int A, int M)
{
int g = gcd(A, M);
if (g != 1)
Console.Write("Inverse doesn't exist");
else {
// If A and M are relatively
// prime, then modulo inverse
// is A^(M-2) mod M
Console.Write(
"Modular multiplicative inverse is "
+ power(A, M - 2, M));
}
}
// To compute x^y under
// modulo M
static int power(int x, int y, int M)
{
if (y == 0)
return 1;
int p = power(x, y / 2, M) % M;
p = (p * p) % M;
if (y % 2 == 0)
return p;
else
return (x * p) % M;
}
// Function to return
// gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver Code
public static void Main()
{
int A = 3, M = 11;
// Function call
modInverse(A, M);
}
}
// This code is contributed by nitin mittal.
JavaScript
<script>
// Javascript program to find modular inverse of a under modulo m
// This program works only if m is prime.
// Function to find modular inverse of a under modulo m
// Assumption: m is prime
function modInverse(a, m)
{
let g = gcd(a, m);
if (g != 1)
document.write("Inverse doesn't exist");
else
{
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
document.write("Modular multiplicative inverse is "
+ power(a, m - 2, m));
}
}
// To compute x^y under modulo m
function power(x, y, m)
{
if (y == 0)
return 1;
let p = power(x, parseInt(y / 2), m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
// Function to return gcd of a and b
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver code
let a = 3, m = 11;
// Function call
modInverse(a, m);
// This code is contributed by subham348.
</script>
PHP
<?php
// PHP program to find modular
// inverse of A under modulo M
// This program works only if M
// is prime.
// Function to find modular
// inverse of A under modulo
// M Assumption: M is prime
function modInverse( $A, $M)
{
$g = gcd($A, $M);
if ($g != 1)
echo "Inverse doesn't exist";
else
{
// If A and M are relatively
// prime, then modulo inverse
// is A^(M-2) mod M
echo "Modular multiplicative inverse is "
, power($A, $M - 2, $M);
}
}
// To compute x^y under modulo m
function power( $x, $y, $M)
{
if ($y == 0)
return 1;
$p = power($x, $y / 2, $M) % $M;
$p = ($p * $p) % $M;
return ($y % 2 == 0)? $p : ($x * $p) % $M;
}
// Function to return gcd of a and b
function gcd($a, $b)
{
if ($a == 0)
return $b;
return gcd($b % $a, $a);
}
// Driver Code
$A = 3;
$M = 11;
// Function call
modInverse($A, $M);
// This code is contributed by anuj_67.
?>
OutputModular multiplicative inverse is 4
Time Complexity: O(log M)
Auxiliary Space: O(log M), because of the internal recursion stack.
Applications:
Computation of the modular multiplicative inverse is an essential step in RSA public-key encryption method.
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