Count Inversions of size three in a given array
Last Updated :
23 Jul, 2025
Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.
Example :
Input: {8, 4, 2, 1}
Output: 4
The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1).
Input: {9, 6, 4, 5, 8}
Output: 2
The two inversions are {9, 6, 4} and {9, 6, 5}
We have already discussed inversion count of size two by merge sort, Self Balancing BST and BIT.
Simple approach: Loop for all possible value of i, j and k and check for the condition a[i] > a[j] > a[k] and i < j < k.
C++
// A Simple C++ O(n^3) program to count inversions of size 3
#include<bits/stdc++.h>
using namespace std;
// Returns counts of inversions of size three
int getInvCount(int arr[],int n)
{
int invcount = 0; // Initialize result
for (int i=0; i<n-2; i++)
{
for (int j=i+1; j<n-1; j++)
{
if (arr[i]>arr[j])
{
for (int k=j+1; k<n; k++)
{
if (arr[j]>arr[k])
invcount++;
}
}
}
}
return invcount;
}
// Driver program to test above function
int main()
{
int arr[] = {8, 4, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Inversion Count : " << getInvCount(arr, n);
return 0;
}
Java
// A simple Java implementation to count inversion of size 3
class Inversion{
// returns count of inversion of size 3
int getInvCount(int arr[], int n)
{
int invcount = 0; // initialize result
for(int i=0 ; i< n-2; i++)
{
for(int j=i+1; j<n-1; j++)
{
if(arr[i] > arr[j])
{
for(int k=j+1; k<n; k++)
{
if(arr[j] > arr[k])
invcount++;
}
}
}
}
return invcount;
}
// driver program to test above function
public static void main(String args[])
{
Inversion inversion = new Inversion();
int arr[] = new int[] {8, 4, 2, 1};
int n = arr.length;
System.out.print("Inversion count : " +
inversion.getInvCount(arr, n));
}
}
// This code is contributed by Mayank Jaiswal
Python3
# A simple python O(n^3) program
# to count inversions of size 3
# Returns counts of inversions
# of size threee
def getInvCount(arr):
n = len(arr)
invcount = 0 #Initialize result
for i in range(0,n-1):
for j in range(i+1 , n):
if arr[i] > arr[j]:
for k in range(j+1 , n):
if arr[j] > arr[k]:
invcount += 1
return invcount
# Driver program to test above function
arr = [8 , 4, 2 , 1]
print ("Inversion Count : %d" %(getInvCount(arr)))
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
C#
// A simple C# implementation to
// count inversion of size 3
using System;
class GFG {
// returns count of inversion of size 3
static int getInvCount(int []arr, int n)
{
// initialize result
int invcount = 0;
for(int i = 0 ; i < n - 2; i++)
{
for(int j = i + 1; j < n - 1; j++)
{
if(arr[i] > arr[j])
{
for(int k = j + 1; k < n; k++)
{
if(arr[j] > arr[k])
invcount++;
}
}
}
}
return invcount;
}
// Driver Code
public static void Main()
{
int []arr = new int[] {8, 4, 2, 1};
int n = arr.Length;
Console.WriteLine("Inversion count : " +
getInvCount(arr, n));
}
}
// This code is contributed by anuj_67.
PHP
<?php
// A O(n^2) PHP program to
// count inversions of size 3
// Returns count of
// inversions of size 3
function getInvCount($arr, $n)
{
// Initialize result
$invcount = 0;
for ($i = 1; $i < $n - 1; $i++)
{
// Count all smaller elements
// on right of arr[i]
$small = 0;
for($j = $i + 1; $j < $n; $j++)
if ($arr[$i] > $arr[$j])
$small++;
// Count all greater elements
// on left of arr[i]
$great = 0;
for($j = $i - 1; $j >= 0; $j--)
if ($arr[$i] < $arr[$j])
$great++;
// Update inversion count by
// adding all inversions
// that have arr[i] as
// middle of three elements
$invcount += $great * $small;
}
return $invcount;
}
// Driver Code
$arr = array(8, 4, 2, 1);
$n = sizeof($arr);
echo "Inversion Count : "
, getInvCount($arr, $n);
// This code is contributed m_kit
?>
JavaScript
<script>
// A simple Javascript implementation to count inversion of size 3
// returns count of inversion of size 3
function getInvCount(arr, n)
{
let invcount = 0; // initialize result
for(let i = 0 ; i < n - 2; i++)
{
for(let j = i + 1; j < n - 1; j++)
{
if(arr[i] > arr[j])
{
for(let k = j + 1; k < n; k++)
{
if(arr[j] > arr[k])
invcount++;
}
}
}
}
return invcount;
}
// driver program to test above function
let arr = [8, 4, 2, 1];
let n = arr.length;
document.write("Inversion count : " +
getInvCount(arr, n));
// This code is contributed by rag2127
</script>
OutputInversion Count : 4
Time complexity: O(n^3)
Auxiliary Space: O(1).
Better Approach :
We can reduce the complexity if we consider every element arr[i] as middle element of inversion, find all the numbers greater than a[i] whose index is less than i, find all the numbers which are smaller than a[i] and index is more than i. We multiply the number of elements greater than a[i] to the number of elements smaller than a[i] and add it to the result.
Below is the implementation of the idea.
C++
// A O(n^2) C++ program to count inversions of size 3
#include<bits/stdc++.h>
using namespace std;
// Returns count of inversions of size 3
int getInvCount(int arr[], int n)
{
int invcount = 0; // Initialize result
for (int i=1; i<n-1; i++)
{
// Count all smaller elements on right of arr[i]
int small = 0;
for (int j=i+1; j<n; j++)
if (arr[i] > arr[j])
small++;
// Count all greater elements on left of arr[i]
int great = 0;
for (int j=i-1; j>=0; j--)
if (arr[i] < arr[j])
great++;
// Update inversion count by adding all inversions
// that have arr[i] as middle of three elements
invcount += great*small;
}
return invcount;
}
// Driver program to test above function
int main()
{
int arr[] = {8, 4, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Inversion Count : " << getInvCount(arr, n);
return 0;
}
Java
// A O(n^2) Java program to count inversions of size 3
class Inversion {
// returns count of inversion of size 3
int getInvCount(int arr[], int n)
{
int invcount = 0; // initialize result
for (int i=0 ; i< n-1; i++)
{
// count all smaller elements on right of arr[i]
int small=0;
for (int j=i+1; j<n; j++)
if (arr[i] > arr[j])
small++;
// count all greater elements on left of arr[i]
int great = 0;
for (int j=i-1; j>=0; j--)
if (arr[i] < arr[j])
great++;
// update inversion count by adding all inversions
// that have arr[i] as middle of three elements
invcount += great*small;
}
return invcount;
}
// driver program to test above function
public static void main(String args[])
{
Inversion inversion = new Inversion();
int arr[] = new int[] {8, 4, 2, 1};
int n = arr.length;
System.out.print("Inversion count : " +
inversion.getInvCount(arr, n));
}
}
// This code has been contributed by Mayank Jaiswal
Python3
# A O(n^2) Python3 program to
# count inversions of size 3
# Returns count of inversions
# of size 3
def getInvCount(arr, n):
# Initialize result
invcount = 0
for i in range(1,n-1):
# Count all smaller elements
# on right of arr[i]
small = 0
for j in range(i+1 ,n):
if (arr[i] > arr[j]):
small+=1
# Count all greater elements
# on left of arr[i]
great = 0;
for j in range(i-1,-1,-1):
if (arr[i] < arr[j]):
great+=1
# Update inversion count by
# adding all inversions that
# have arr[i] as middle of
# three elements
invcount += great * small
return invcount
# Driver program to test above function
arr = [8, 4, 2, 1]
n = len(arr)
print("Inversion Count :",getInvCount(arr, n))
# This code is Contributed by Smitha Dinesh Semwal
C#
// A O(n^2) Java program to count inversions
// of size 3
using System;
public class Inversion {
// returns count of inversion of size 3
static int getInvCount(int []arr, int n)
{
int invcount = 0; // initialize result
for (int i = 0 ; i < n-1; i++)
{
// count all smaller elements on
// right of arr[i]
int small = 0;
for (int j = i+1; j < n; j++)
if (arr[i] > arr[j])
small++;
// count all greater elements on
// left of arr[i]
int great = 0;
for (int j = i-1; j >= 0; j--)
if (arr[i] < arr[j])
great++;
// update inversion count by
// adding all inversions that
// have arr[i] as middle of
// three elements
invcount += great * small;
}
return invcount;
}
// driver program to test above function
public static void Main()
{
int []arr = new int[] {8, 4, 2, 1};
int n = arr.Length;
Console.WriteLine("Inversion count : "
+ getInvCount(arr, n));
}
}
// This code has been contributed by anuj_67.
PHP
<?php
// A O(n^2) PHP program to count
// inversions of size 3
// Returns count of
// inversions of size 3
function getInvCount($arr, $n)
{
// Initialize result
$invcount = 0;
for ($i = 1; $i < $n - 1; $i++)
{
// Count all smaller elements
// on right of arr[i]
$small = 0;
for ($j = $i + 1; $j < $n; $j++)
if ($arr[$i] > $arr[$j])
$small++;
// Count all greater elements
// on left of arr[i]
$great = 0;
for ($j = $i - 1; $j >= 0; $j--)
if ($arr[$i] < $arr[$j])
$great++;
// Update inversion count by
// adding all inversions that
// have arr[i] as middle of
// three elements
$invcount += $great * $small;
}
return $invcount;
}
// Driver Code
$arr = array (8, 4, 2, 1);
$n = sizeof($arr);
echo "Inversion Count : " ,
getInvCount($arr, $n);
// This code is contributed by m_kit
?>
JavaScript
<script>
// A O(n^2) Javascript program to count inversions of size 3
// returns count of inversion of size 3
function getInvCount(arr, n)
{
let invcount = 0; // initialize result
for (let i = 0 ; i < n - 1; i++)
{
// count all smaller elements on right of arr[i]
let small = 0;
for (let j = i + 1; j < n; j++)
if (arr[i] > arr[j])
small++;
// count all greater elements on left of arr[i]
let great = 0;
for (let j = i - 1; j >= 0; j--)
if (arr[i] < arr[j])
great++;
// update inversion count by adding all inversions
// that have arr[i] as middle of three elements
invcount += great*small;
}
return invcount;
}
// driver program to test above function
let arr=[8, 4, 2, 1];
let n = arr.length;
document.write("Inversion count : " +getInvCount(arr, n));
// This code is contributed by avanitrachhadiya2155
</script>
OutputInversion Count : 4
Time Complexity: O(n^2)
Auxiliary Space: O(1).
Binary Indexed Tree Approach :
Like inversions of size 2, we can use Binary indexed tree to find inversions of size 3. It is strongly recommended to refer below article first.
Count inversions of size two Using BIT
The idea is similar to above method. We count the number of greater elements and smaller elements for all the elements and then multiply greater[] to smaller[] and add it to the result.
Solution :
- To find out the number of smaller elements for an index we iterate from n-1 to 0. For every element a[i] we calculate the getSum() function for (a[i]-1) which gives the number of elements till a[i]-1.
- To find out the number of greater elements for an index we iterate from 0 to n-1. For every element a[i] we calculate the sum of numbers till a[i] (sum smaller or equal to a[i]) by getSum() and subtract it from i (as i is the total number of element till that point) so that we can get number of elements greater than a[i].
Below is the code for the above approach.
C++
// C++ program to count inversions of size 3 using
// Binary Indexed Tree
#include <bits/stdc++.h>
using namespace std;
// It is beneficial to declare the 2D BIT globally
// since passing it into functions will create
// additional overhead
const int N = 100005;
int BIT[3][N] = { 0 };
// update function. "t" denotes the t'th Binary
// indexed tree
void updateBIT(int t, int i, int val, int n)
{
// Traversing the t'th BIT
while (i <= n) {
BIT[t][i] = BIT[t][i] + val;
i = i + (i & (-i));
}
}
// function to get the sum.
// "t" denotes the t'th Binary indexed tree
int getSum(int t, int i)
{
int res = 0;
// Traversing the t'th BIT
while (i > 0) {
res = res + BIT[t][i];
i = i - (i & (-i));
}
return res;
}
// Converts an array to an array with values from 1 to n
// and relative order of smaller and greater elements
// remains same.
void convert(int arr[], int n)
{
// Create a copy of arr[] in temp and sort
// the temp array in increasing order
int temp[n];
for (int i = 0; i < n; i++)
temp[i] = arr[i];
sort(temp, temp + n);
// Traverse all array elements
for (int i = 0; i < n; i++) {
// lower_bound() Returns pointer to the
// first element greater than or equal
// to arr[i]
arr[i] = lower_bound(temp, temp + n,
arr[i]) - temp + 1;
}
}
// Returns count of inversions of size three
int getInvCount(int arr[], int n)
{
// Convert arr[] to an array with values from
// 1 to n and relative order of smaller and
// greater elements remains same.
convert(arr, n);
// iterating over the converted array in
// reverse order.
for (int i = n - 1; i >= 0; i--) {
// update the BIT for l = 1
updateBIT(1, arr[i], 1, n);
// update BIT for all other BITs
for (int l = 1; l < 3; l++) {
updateBIT(l + 1, arr[i], getSum(l, arr[i] - 1), n);
}
}
// final result
return getSum(3, n);
}
// Driver program to test above function
int main()
{
int arr[] = {8, 4, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Inversion Count : " << getInvCount(arr, n);
return 0;
}
// This code is contributed by Pushpesh Raj.
Java
// Java program to count inversions of size 3 using
// Binary Indexed Tree
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.*;
import java.util.Collections;
class GFG {
// It is beneficial to declare the 2D BIT globally
// since passing it into functions will create
// additional overhead
static int N = 100005;
static int BIT[][] = new int[4][N];
// update function. "t" denotes the t'th Binary
// indexed tree
static void updateBIT(int t, int i, int val, int n)
{
// Traversing the t'th BIT
while (i <= n) {
BIT[t][i] = BIT[t][i] + val;
i = i + (i & (-i));
}
}
// function to get the sum.
// "t" denotes the t'th Binary indexed tree
static int getSum(int t, int i)
{
int res = 0;
// Traversing the t'th BIT
while (i > 0) {
res = res + BIT[t][i];
i = i - (i & (-i));
}
return res;
}
// Converts an array to an array with values from 1 to n
// and relative order of smaller and greater elements
// remains same.
static void convert(int arr[], int n)
{
// Create a copy of arr[] in temp and sort
// the temp array in increasing order
int temp[]=new int[n];
for (int i = 0; i < n; i++)
temp[i] = arr[i];
Arrays.sort(temp);
// Traverse all array elements
for (int i = 0; i < n; i++) {
// lower_bound() Returns pointer to the
// first element greater than or equal
// to arr[i]
arr[i] = Arrays.binarySearch(temp,arr[i]) + 1;
}
}
// Returns count of inversions of size three
static int getInvCount(int arr[], int n)
{
// Convert arr[] to an array with values from
// 1 to n and relative order of smaller and
// greater elements remains same.
convert(arr, n);
// iterating over the converted array in
// reverse order.
for (int i = n - 1; i >= 0; i--) {
// update the BIT for l = 1
updateBIT(1, arr[i], 1, n);
// update BIT for all other BITs
for (int l = 1; l < 3; l++) {
updateBIT(l + 1, arr[i], getSum(l, arr[i] - 1), n);
}
}
// final result
return getSum(3, n);
}
// Driver program to test above function
public static void main (String[] args)
{
int arr[] = {8, 4, 2, 1};
int n = arr.length;
System.out.print("Inversion Count : "+getInvCount(arr, n));
}
}
// This code is contributed by Utkarsh
Python3
# Python program to count inversions of size 3 using
# Binary Indexed Tree
# It is beneficial to declare the 2D BIT globally
# since passing it into functions will create
# additional overhead
N = 100005
BIT = [[0 for x in range(N)] for y in range(3)]
for i in range(0,50):
abc = [0]*50
BIT.append(abc)
# update function. "t" denotes the t'th Binary
# indexed tree
def updateBIT(t, i, val, n):
# Traversing the t'th BIT
while i <= n:
BIT[t][i] = BIT[t][i] + val
i = i + (i & (-i))
# function to get the sum.
# "t" denotes the t'th Binary indexed tree
def getSum(t, i):
res = 0
# Traversing the t'th BIT
while i > 0:
res = res + BIT[t][i]
i = i - (i & (-i))
return res
# Converts an array to an array with values from 1 to n
# and relative order of smaller and greater elements
# remains same.
def convert(arr, n):
# Create a copy of arr[] in temp and sort
# the temp array in increasing order
temp = [0 for x in range(n)]
for i in range(n):
temp[i] = arr[i]
temp.sort()
# Traverse all array elements
for i in range(n):
# lower_bound() Returns pointer to the
# first element greater than or equal
# to arr[i]
arr[i] = temp.index(arr[i]) + 1
# Returns count of inversions of size three
def getInvCount(arr, n):
# Convert arr[] to an array with values from
# 1 to n and relative order of smaller and
# greater elements remains same.
convert(arr, n)
# iterating over the converted array in
# reverse order.
for i in range(n - 1, -1, -1):
# update the BIT for l = 1
updateBIT(1, arr[i], 1, n)
# update BIT for all other BITs
for l in range(1, 3):
updateBIT(l + 1, arr[i], getSum(l, arr[i] - 1), n)
# final result
return getSum(3, n)
# Driver program to test above function
arr = [8, 4, 2, 1]
n = len(arr)
print("Inversion Count : ", getInvCount(arr, n))
# This code is contributed by ashishak__
JavaScript
// JavaScript program to count inversions of size 3 using
// Binary Indexed Tree
const N = 100005;
let BIT = [[0], [0], [0]];
for (let i = 0; i < 10; i++) {
let abc = [0];
BIT.push(abc);
}
// update function. "t" denotes the t'th Binary
// indexed tree
function updateBIT(t, i, val, n) {
// Traversing the t'th BIT
while (i <= n) {
if (!BIT[t][i]) BIT[t][i] = 0;
BIT[t][i] = BIT[t][i] + val;
i = i + (i & (-i));
}
}
// function to get the sum.
// "t" denotes the t'th Binary indexed tree
function getSum(t, i) {
let res = 0;
// Traversing the t'th BIT
while (i > 0) {
res = res + BIT[t][i];
i = i - (i & (-i));
}
return res;
}
// Converts an array to an array with values from 1 to n
// and relative order of smaller and greater elements
// remains same.
function convert(arr, n) {
// Create a copy of arr[] in temp and sort
// the temp array in increasing order
let temp = [];
for (let i = 0; i < n; i++)
temp[i] = arr[i];
temp.sort();
// Traverse all array elements
for (let i = 0; i < n; i++) {
// lower_bound() Returns pointer to the
// first element greater than or equal
// to arr[i]
arr[i] = temp.indexOf(arr[i]) + 1;
}
}
// Returns count of inversions of size three
function getInvCount(arr, n) {
// Convert arr[] to an array with values from
// 1 to n and relative order of smaller and
// greater elements remains same.
convert(arr, n);
// iterating over the converted array in
// reverse order.
for (let i = n - 1; i >= 0; i--) {
// update the BIT for l = 1
updateBIT(1, arr[i], 1, n);
// update BIT for all other BITs
for (let l = 1; l < 3; l++) {
updateBIT(l + 1, arr[i], getSum(l, arr[i] - 1), n);
}
}
// final result
return getSum(3, n);
}
// Driver program to test above function
let arr = [8, 4, 2, 1];
let n = arr.length;
console.log("Inversion Count : " + getInvCount(arr, n));
// This code is contributed by akashish__.
C#
using System;
class GFG
{
// It is beneficial to declare the 2D BIT globally
// since passing it into functions will create
// additional overhead
static int N = 100005;
static int[,] BIT = new int[4, N];
// update function. "t" denotes the t'th Binary
// indexed tree
static void updateBIT(int t, int i, int val, int n)
{
// Traversing the t'th BIT
while (i <= n)
{
BIT[t, i] = BIT[t, i] + val;
i = i + (i & (-i));
}
}
// function to get the sum.
// "t" denotes the t'th Binary indexed tree
static int getSum(int t, int i)
{
int res = 0;
// Traversing the t'th BIT
while (i > 0)
{
res = res + BIT[t, i];
i = i - (i & (-i));
}
return res;
}
// Converts an array to an array with values from 1 to n
// and relative order of smaller and greater elements
// remains same.
static void convert(int[] arr, int n)
{
// Create a copy of arr[] in temp and sort
// the temp array in increasing order
int[] temp = new int[n];
for (int i = 0; i < n; i++)
temp[i] = arr[i];
Array.Sort(temp);
// Traverse all array elements
for (int i = 0; i < n; i++)
{
// lower_bound() Returns pointer to the
// first element greater than or equal
// to arr[i]
arr[i] = Array.BinarySearch(temp, arr[i]) + 1;
}
}
// Returns count of inversions of size three
static int getInvCount(int[] arr, int n)
{
// Convert arr[] to an array with values from
// 1 to n and relative order of smaller and
// greater elements remains same.
convert(arr, n);
// iterating over the converted array in
// reverse order.
for (int i = n - 1; i >= 0; i--)
{
// update the BIT for l = 1
updateBIT(1, arr[i], 1, n);
// update BIT for all other BITs
for (int l = 1; l < 3; l++)
{
updateBIT(l + 1, arr[i], getSum(l, arr[i] - 1), n);
}
}
// final result
return getSum(3, n);
}
// Driver program to test above function
static void Main(string[] args)
{
int[] arr = { 8, 4, 2, 1 };
int n = arr.Length;
Console.Write("Inversion Count : " + getInvCount(arr, n));
}
}
OutputInversion Count : 4
Time Complexity: O(n*log(n))
Auxiliary Space: O(n).
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