Lexicographic rank of a String
Last Updated :
23 Jul, 2025
Given a string str, find its rank among all its permutations when sorted lexicographically.
Note: The characters in string are all unique.
Examples:
Input: str = "acb"
Output: 2
Explanation: If all the permutations of the string are arranged lexicographically they will be "abc", "acb", "bac", "bca", "cab", "cba". From here it can be clearly that the rank of str is 2.
Input: str = "string"
Output: 598
Input: str[] = "cba"
Output: Rank = 6
[Naive Approach] Generating all permutations
One simple solution is to generate all the permutations in lexicographic order and store the rank of the current string. To generate all permutations, we first sort the string and then one by one generate lexicographically next permutation. After generating a permutation, check if the generated permutation is the same as the given string and return the rank of str.
C++
// C++ code for finding rank of string
#include <bits/stdc++.h>
using namespace std;
// A function to find rank of a string in all permutations
// of characters
int findRank(string &str) {
// Create a copy of the original
// string to keep it unchanged
string original = str;
// Sort the string to get the first
// permutation in lexicographic order
sort(str.begin(), str.end());
int rank = 1;
// Keep generating next permutation
// until we find the original string
while (str != original) {
next_permutation(str.begin(), str.end());
rank++;
}
return rank;
}
int main() {
string str = "string";
cout << findRank(str);
return 0;
}
Java
// Java code for finding rank of string
import java.util.Arrays;
class GfG {
// A function to find rank of a string in all permutations
// of characters
static int findRank(String str) {
// Create a copy of the original
// string to keep it unchanged
String original = str;
// Convert to character array for sorting and permutation
char[] arr = str.toCharArray();
// Sort the string to get the first
// permutation in lexicographic order
Arrays.sort(arr);
int rank = 1;
// Keep generating next permutation
// until we find the original string
while (!String.valueOf(arr).equals(original)) {
nextPermutation(arr);
rank++;
}
return rank;
}
static void nextPermutation(char[] arr) {
int i = arr.length - 2;
while (i >= 0 && arr[i] >= arr[i + 1]) i--;
if (i >= 0) {
int j = arr.length - 1;
while (arr[j] <= arr[i]) j--;
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
reverse(arr, i + 1, arr.length - 1);
}
static void reverse(char[] arr, int start, int end) {
while (start < end) {
char temp = arr[start];
arr[start++] = arr[end];
arr[end--] = temp;
}
}
public static void main(String[] args) {
String str = "string";
System.out.println(findRank(str));
}
}
Python
# Python code for finding rank of string
import itertools
# A function to find rank of a string in all permutations
# of characters
def findRank(str):
# Create a copy of the original
# string to keep it unchanged
original = str
# Sort the string to get the first
# permutation in lexicographic order
arr = sorted(str)
rank = 1
# Keep generating next permutation
# until we find the original string
while ''.join(arr) != original:
arr = next(itertools.islice(itertools.permutations(arr), 1, None))
arr = list(arr)
rank += 1
return rank
if __name__ == "__main__":
str = "string"
print(findRank(str))
C#
// C# code for finding rank of string
using System;
class GfG {
// A function to find rank of a string in all permutations
// of characters
static int findRank(string str) {
// Create a copy of the original
// string to keep it unchanged
string original = str;
// Sort the string to get the first
// permutation in lexicographic order
char[] arr = str.ToCharArray();
Array.Sort(arr);
int rank = 1;
// Keep generating next permutation
// until we find the original string
while (new string(arr) != original) {
nextPermutation(arr);
rank++;
}
return rank;
}
static void nextPermutation(char[] arr) {
int i = arr.Length - 2;
while (i >= 0 && arr[i] >= arr[i + 1]) i--;
if (i >= 0) {
int j = arr.Length - 1;
while (arr[j] <= arr[i]) j--;
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
reverse(arr, i + 1, arr.Length - 1);
}
static void reverse(char[] arr, int start, int end) {
while (start < end) {
char temp = arr[start];
arr[start++] = arr[end];
arr[end--] = temp;
}
}
static void Main(string[] args) {
string str = "string";
Console.WriteLine(findRank(str));
}
}
JavaScript
// JavaScript code for finding rank of string
// A function to find rank of a string in all permutations
// of characters
function findRank(str) {
// Create a copy of the original
// string to keep it unchanged
let original = str;
// Sort the string to get the first
// permutation in lexicographic order
let arr = str.split('').sort();
let rank = 1;
// Keep generating next permutation
// until we find the original string
while (arr.join('') !== original) {
nextPermutation(arr);
rank++;
}
return rank;
}
function nextPermutation(arr) {
let i = arr.length - 2;
while (i >= 0 && arr[i] >= arr[i + 1]) i--;
if (i >= 0) {
let j = arr.length - 1;
while (arr[j] <= arr[i]) j--;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
reverse(arr, i + 1, arr.length - 1);
}
function reverse(arr, start, end) {
while (start < end) {
[arr[start], arr[end]] = [arr[end], arr[start]];
start++;
end--;
}
}
let str = "string";
console.log(findRank(str));
Time Complexity: O(n!), for generating all permutations
Auxiliary Space: O(n)
Count Smaller Strings - O(n^3) Time
We count smaller strings than the given string, and at the end return rank as one plus the count value.
Rank = Count of Smaller + 1
For example, for "cba", the 5 smaller strings are ""abc", "acb", "bac", "bca" and "cab" and our result is 5 + 1.
How do we count smaller?
We first find count of smaller strings when the first character is replaced. For example, for "cba", if we fix the first character other than "c", we get 4 smaller strings. Now we do the same thing for second character which means, we count smaller strings when first is "c" and second is smaller than "b". We have 1 such string.
For a better understanding follow the below illustration.
Let the given string be "STRING". In the input string, 'S' is the first character. There are total 6 characters and 4 of them are smaller than 'S'. So there can be 4 * 5! smaller strings where first character is smaller than 'S', like following
- G X X X X X
- R X X X X X
- I X X X X X
- N X X X X X
Similarly we can use the same process for the other letters. Fix 'S' and find the smaller strings starting with 'S'.
- Repeat the same process for T, rank is 4*5! + 4*4! +. . .
- Now fix T and repeat the same process for R, rank is 4*5! + 4*4! + 3*3! + . . .
- Now fix R and repeat the same process for I, rank is 4*5! + 4*4! + 3*3! + 1*2! + . . .
- Now fix I and repeat the same process for N, rank is 4*5! + 4*4! + 3*3! + 1*2! + 1*1! + . . .
- Now fic N and repeat the same process for G, rank is 4*5! + 4*4! + 3*3! + 1*2! + 1*1! + 0*0!
If this process is continued the rank = 4*5! + 4*4! + 3*3! + 1*2! + 1*1! + 0*0! = 597. The above computations find count of smaller strings. Therefore rank of given string is count of smaller strings plus 1. The final rank = 1 + 597 = 598
Follow the steps mentioned below to implement the idea:
- Iterate the string from i = 0 to length of string:
- Find the number of characters smaller than the current character.
- Calculate the number of lexicographically smaller that can be formed using them as shown above.
- Add that value to the rank.
- At the end, add 1 with rank and return it as the required answer. [the reason is mentioned above]
Below is the implementation of the above approach.
C++
// C++ program to find lexicographic rank
// of a string
#include <bits/stdc++.h>
using namespace std;
// Factorial
int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
int findSmallerInRight(string str, int low) {
int countRight = 0;
for (int i = low + 1; i < str.size(); ++i)
if (str[i] < str[low])
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
int findRank(string str) {
int n = str.size();
// 1 to be added to smaller count
int rank = 1;
int countRight;
for (int i = 0; i < n; ++i) {
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * fact(n - i - 1);
}
return rank;
}
int main() {
string str = "string";
cout << findRank(str);
return 0;
}
Java
// Java program to find lexicographic rank
// of a string
class GfG {
// Factorial
static int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
static int findSmallerInRight(String str, int low) {
int countRight = 0;
for (int i = low + 1; i < str.length(); ++i)
if (str.charAt(i) < str.charAt(low))
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
static int findRank(String str) {
int n = str.length();
// 1 to be added to smaller count
int rank = 1;
int countRight;
for (int i = 0; i < n; ++i) {
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * fact(n - i - 1);
}
return rank;
}
public static void main(String[] args) {
String str = "string";
System.out.println(findRank(str));
}
}
Python
# Python program to find lexicographic rank
# of a string
# Factorial
def fact(n):
res = 1
for i in range(2, n + 1):
res *= i
return res
# A utility function to count
# smaller characters on right of arr[low]
def findSmallerInRight(str, low):
countRight = 0
for i in range(low + 1, len(str)):
if str[i] < str[low]:
countRight += 1
return countRight
# A function to find rank of a string
# in all permutations of characters
def findRank(str):
n = len(str)
# 1 to be added to smaller count
rank = 1
for i in range(n):
# Count number of chars smaller than str[i]
# from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i)
rank += countRight * fact(n - i - 1)
return rank
if __name__ == "__main__":
str = "string"
print(findRank(str))
C#
// C# program to find lexicographic rank
// of a string
using System;
class GfG {
// Factorial
static int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
static int findSmallerInRight(string str, int low) {
int countRight = 0;
for (int i = low + 1; i < str.Length; ++i)
if (str[i] < str[low])
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
static int findRank(string str) {
int n = str.Length;
// 1 to be added to smaller count
int rank = 1;
int countRight;
for (int i = 0; i < n; ++i) {
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * fact(n - i - 1);
}
return rank;
}
static void Main(string[] args) {
string str = "string";
Console.WriteLine(findRank(str));
}
}
JavaScript
// JavaScript program to find lexicographic rank
// of a string
// Factorial
function fact(n) {
let res = 1;
for (let i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
function findSmallerInRight(str, low) {
let countRight = 0;
for (let i = low + 1; i < str.length; ++i)
if (str[i] < str[low])
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
function findRank(str) {
let n = str.length;
// 1 to be added to smaller count
let rank = 1;
let countRight;
for (let i = 0; i < n; ++i) {
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * fact(n - i - 1);
}
return rank;
}
let str = "string";
console.log(findRank(str));
Time Complexity : O(n^3)
Auxiliary Space : O(1)
Optimized Approach - Reducing Repeated Computations - O(n^2) Time
We can avoid repeated computations of factorial. We first compute factorial of n, and then divide n, n-1, etc to compute upcoming factorials.
C++
// C++ program to find lexicographic rank
// of a string
#include <bits/stdc++.h>
using namespace std;
// Factorial
int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
int findSmallerInRight(string str, int low) {
int countRight = 0;
for (int i = low + 1; i < str.size(); ++i)
if (str[i] < str[low])
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
int findRank(string str) {
int n = str.size();
int mul = fact(n);
int rank = 1;
int countRight;
for (int i = 0; i < n; ++i) {
mul /= (n - i);
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * mul;
}
return rank;
}
int main() {
string str = "string";
cout << findRank(str);
return 0;
}
Java
// Java program to find lexicographic rank
// of a string
class GfG {
// Factorial
static int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
static int findSmallerInRight(String str, int low) {
int countRight = 0;
for (int i = low + 1; i < str.length(); ++i)
if (str.charAt(i) < str.charAt(low))
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
static int findRank(String str) {
int n = str.length();
int mul = fact(n);
int rank = 1;
int countRight;
for (int i = 0; i < n; ++i) {
mul /= (n - i);
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * mul;
}
return rank;
}
public static void main(String[] args) {
String str = "string";
System.out.println(findRank(str));
}
}
Python
# Python program to find lexicographic rank
# of a string
# Factorial
def fact(n):
res = 1
for i in range(2, n + 1):
res *= i
return res
# A utility function to count
# smaller characters on right of arr[low]
def findSmallerInRight(str, low):
countRight = 0
for i in range(low + 1, len(str)):
if str[i] < str[low]:
countRight += 1
return countRight
# A function to find rank of a string
# in all permutations of characters
def findRank(str):
n = len(str)
mul = fact(n)
rank = 1
for i in range(n):
mul //= (n - i)
# Count number of chars smaller than str[i]
# from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i)
rank += countRight * mul
return rank
if __name__ == "__main__":
str = "string"
print(findRank(str))
C#
// C# program to find lexicographic rank
// of a string
using System;
class GfG {
// Factorial
static int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
static int findSmallerInRight(string str, int low) {
int countRight = 0;
for (int i = low + 1; i < str.Length; ++i)
if (str[i] < str[low])
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
static int findRank(string str) {
int n = str.Length;
int mul = fact(n);
int rank = 1;
int countRight;
for (int i = 0; i < n; ++i) {
mul /= (n - i);
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * mul;
}
return rank;
}
static void Main(string[] args) {
string str = "string";
Console.WriteLine(findRank(str));
}
}
JavaScript
// JavaScript program to find lexicographic rank
// of a string
// Factorial
function fact(n) {
let res = 1;
for (let i = 2; i <= n; i++) { res *= i; }
return res;
}
// A utility function to count
// smaller characters on right of arr[low]
function findSmallerInRight(str, low) {
let countRight = 0;
for (let i = low + 1; i < str.length; ++i)
if (str[i] < str[low])
++countRight;
return countRight;
}
// A function to find rank of a string
// in all permutations of characters
function findRank(str) {
let n = str.length;
let mul = fact(n);
let rank = 1;
let countRight;
for (let i = 0; i < n; ++i) {
mul = Math.floor(mul / (n - i));
// Count number of chars smaller than str[i]
// from str[i+1] to str[len-1]
countRight = findSmallerInRight(str, i);
rank += countRight * mul;
}
return rank;
}
let str = "string";
console.log(findRank(str));
Time Complexity: O(n2)
Auxiliary Space: O(1)
Further Optimized - Using Frequency Array - O(n) Time
Create an array to store the number of characters smaller than the ith character in the whole string and update it after each index of the given string during the iteration of the string.
Below is the implementation of the above approach.
C++
// C++ code for finding rank of string
#include <bits/stdc++.h>
using namespace std;
// Factorial
int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A function to find rank of a string in all permutations
// of characters
int findRank(string &str)
{
int n = str.size();
int mul = fact(n);
int rank = 1;
// Using a vector of size 26 for lowercase letters
vector<int> count(26, 0);
// Populate the count array for each character in string
for (int i = 0; i < n; ++i) {
++count[str[i] - 'a'];
}
// Convert count to cumulative sum
for (int i = 1; i < 26; ++i) {
count[i] += count[i - 1];
}
for (int i = 0; i < n; ++i) {
mul /= (n - i);
// Get index of current character in count array
int charIndex = str[i] - 'a';
// Add count of characters smaller than current character
if (charIndex > 0) {
rank += count[charIndex - 1] * mul;
}
// Update count array
for (int j = charIndex; j < 26; ++j) {
--count[j];
}
}
return rank;
}
int main() {
string str = "string";
cout << findRank(str);
return 0;
}
Java
// Java code for finding rank of string
class GfG {
// Factorial
static int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A function to find rank of a string in all permutations
// of characters
static int findRank(String str) {
int n = str.length();
int mul = fact(n);
int rank = 1;
// Using a vector of size 26 for lowercase letters
int[] count = new int[26];
// Populate the count array for each character in string
for (int i = 0; i < n; ++i) {
++count[str.charAt(i) - 'a'];
}
// Convert count to cumulative sum
for (int i = 1; i < 26; ++i) {
count[i] += count[i - 1];
}
for (int i = 0; i < n; ++i) {
mul /= (n - i);
// Get index of current character in count array
int charIndex = str.charAt(i) - 'a';
// Add count of characters smaller than current character
if (charIndex > 0) {
rank += count[charIndex - 1] * mul;
}
// Update count array
for (int j = charIndex; j < 26; ++j) {
--count[j];
}
}
return rank;
}
public static void main(String[] args) {
String str = "string";
System.out.println(findRank(str));
}
}
Python
# Python code for finding rank of string
# Factorial
def fact(n):
res = 1
for i in range(2, n + 1):
res *= i
return res
# A function to find rank of a string in all permutations
# of characters
def findRank(str):
n = len(str)
mul = fact(n)
rank = 1
# Using a vector of size 26 for lowercase letters
count = [0] * 26
# Populate the count array for each character in string
for i in range(n):
count[ord(str[i]) - ord('a')] += 1
# Convert count to cumulative sum
for i in range(1, 26):
count[i] += count[i - 1]
for i in range(n):
mul //= (n - i)
# Get index of current character in count array
charIndex = ord(str[i]) - ord('a')
# Add count of characters smaller than current character
if charIndex > 0:
rank += count[charIndex - 1] * mul
# Update count array
for j in range(charIndex, 26):
count[j] -= 1
return rank
if __name__ == "__main__":
str = "string"
print(findRank(str))
C#
// C# code for finding rank of string
using System;
class GfG {
// Factorial
static int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) { res *= i; }
return res;
}
// A function to find rank of a string in all permutations
// of characters
static int findRank(string str) {
int n = str.Length;
int mul = fact(n);
int rank = 1;
// Using a vector of size 26 for lowercase letters
int[] count = new int[26];
// Populate the count array for each character in string
for (int i = 0; i < n; ++i) {
++count[str[i] - 'a'];
}
// Convert count to cumulative sum
for (int i = 1; i < 26; ++i) {
count[i] += count[i - 1];
}
for (int i = 0; i < n; ++i) {
mul /= (n - i);
// Get index of current character in count array
int charIndex = str[i] - 'a';
// Add count of characters smaller than current character
if (charIndex > 0) {
rank += count[charIndex - 1] * mul;
}
// Update count array
for (int j = charIndex; j < 26; ++j) {
--count[j];
}
}
return rank;
}
static void Main(string[] args) {
string str = "string";
Console.WriteLine(findRank(str));
}
}
JavaScript
// JavaScript code for finding rank of string
// Factorial
function fact(n) {
let res = 1;
for (let i = 2; i <= n; i++) { res *= i; }
return res;
}
// A function to find rank of a string in all permutations
// of characters
function findRank(str) {
let n = str.length;
let mul = fact(n);
let rank = 1;
// Using a vector of size 26 for lowercase letters
let count = new Array(26).fill(0);
// Populate the count array for each character in string
for (let i = 0; i < n; ++i) {
++count[str.charCodeAt(i) - 'a'.charCodeAt(0)];
}
// Convert count to cumulative sum
for (let i = 1; i < 26; ++i) {
count[i] += count[i - 1];
}
for (let i = 0; i < n; ++i) {
mul = Math.floor(mul / (n - i));
// Get index of current character in count array
let charIndex = str.charCodeAt(i) - 'a'.charCodeAt(0);
// Add count of characters smaller than current character
if (charIndex > 0) {
rank += count[charIndex - 1] * mul;
}
// Update count array
for (let j = charIndex; j < 26; ++j) {
--count[j];
}
}
return rank;
}
let str = "string";
console.log(findRank(str));
Time Complexity: O(n)
Auxiliary Space: O(1) as we are using an array of size 256
Note: The above programs don't work for duplicate characters. To make them work for duplicate characters, find all the characters that are smaller (include equal this time also), do the same as above but, this time divide the rank so formed by p! where p is the count of occurrences of the repeating character.
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