All combinations of size r from an array
Last Updated :
20 May, 2025
You are given an array arr[] consisting of n elements. Your task is to generate and print all possible combinations of exactly r elements from this array.
Note: A combination is a selection of items where the order does not matter. Ensure that each unique group of r elements is printed only once, regardless of order.
Example:
Input: arr = [1, 2, 3, 4], r = 2
Output: 1 2
1 3
1 4
2 3
2 4
3 4
Explanation: We need to generate all possible combinations of size 2 from an array of size 4. The total number of such combinations is given by the formula:
4C2 = 4! / [(4 - 2)! × 2!] = 6 combinations.
Input: arr = [1, 2, 3, 4], r = 3
Output: 1 2 3
1 2 4
1 3 4
2 3 4
Explanation: We need to generate all possible combinations of size 3 from an array of size 4. The total number of such combinations is given by the formula:
4C3 = 4! / [(4 - 3)! × 3!] = 4 combinations.
Using Recursion and Fixing The Elements - O(nCr * r) Time and O(r) Space
The idea is to fix one element at a time at the current index and recursively fill the remaining positions. Starting from the first index, we try all possible elements that can be placed at that position such that the remaining elements can still fill the combination of size r. Once the size of the current combination becomes equal to r, we store or print that combination. To avoid duplicates, we only consider elements for the remaining positions that are on the right side of the current element.
Follow the below given step-by-step approach:
- Start with the first index (initially 0) and fix one element at this index.
- Recursively try to fix the next element at the next index.
- Continue this process until the size of the current combination becomes equal to
r
. - At each recursive call, iterate from the current
start
index to the end
of the array while ensuring there are enough remaining elements to fill the combination. - When the current combination size becomes
r
, add it to the result list. - Use a temporary array to build each combination step by step.
- Store the final combinations in a result list and return it.
Following diagram shows recursion tree:

C++
#include <bits/stdc++.h>
using namespace std;
// Helper function to find all combinations
// of size r in an array of size n
void combinationUtil(int ind, int r, vector<int> &data,
vector<vector<int>> &result, vector<int> &arr) {
int n = arr.size();
// If size of current combination is r
if (data.size() == r) {
result.push_back(data);
return;
}
// Replace index with all possible elements
for(int i = ind; i < n; i++) {
// Current element is included
data.push_back(arr[i]);
// Recur for next elements
combinationUtil(i + 1, r, data, result, arr);
// Backtrack to find other combinations
data.pop_back();
}
}
// Function to find all combinations of size r
// in an array of size n
vector<vector<int>> findCombination(vector<int> &arr, int r) {
int n = arr.size();
// to store the result
vector<vector<int>> result;
// Temporary array to store current combination
vector<int> data;
combinationUtil(0, r, data, result, arr);
return result;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
int r = 2;
vector<vector<int>> res = findCombination(arr, r);
for (const auto &comb : res) {
for (int num : comb) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
Java
import java.util.*;
class GfG {
// Helper function to find all combinations
// of size r in an array of size n
static void combinationUtil(int ind, int r, List<Integer> data,
List<List<Integer>> result, int[] arr) {
int n = arr.length;
// If size of current combination is r
if (data.size() == r) {
result.add(new ArrayList<>(data));
return;
}
// Replace index with all possible elements
for (int i = ind; i < n; i++) {
// Current element is included
data.add(arr[i]);
// Recur for next elements
combinationUtil(i + 1, r, data, result, arr);
// Backtrack to find other combinations
data.remove(data.size() - 1);
}
}
// Function to find all combinations of size r
// in an array of size n
static List<List<Integer>> findCombination(int[] arr, int r) {
int n = arr.length;
// to store the result
List<List<Integer>> result = new ArrayList<>();
// Temporary array to store current combination
List<Integer> data = new ArrayList<>();
combinationUtil(0, r, data, result, arr);
return result;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int r = 2;
List<List<Integer>> res = findCombination(arr, r);
for (List<Integer> comb : res) {
for (int num : comb) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
Python
# Helper function to find all combinations
# of size r in an array of size n
def combinationUtil(ind, r, data, result, arr):
n = len(arr)
# If size of current combination is r
if len(data) == r:
result.append(data.copy())
return
# Replace index with all possible elements
for i in range(ind, n):
# Current element is included
data.append(arr[i])
# Recur for next elements
combinationUtil(i + 1, r, data, result, arr)
# Backtrack to find other combinations
data.pop()
# Function to find all combinations of size r
# in an array of size n
def findCombination(arr, r):
n = len(arr)
# to store the result
result = []
# Temporary array to store current combination
data = []
combinationUtil(0, r, data, result, arr)
return result
arr = [1, 2, 3, 4]
r = 2
res = findCombination(arr, r)
for comb in res:
for num in comb:
print(num, end=" ")
print()
C#
using System;
using System.Collections.Generic;
class GfG {
// Helper function to find all combinations
// of size r in an array of size n
static void combinationUtil(int ind, int r, List<int> data,
List<List<int>> result, int[] arr) {
int n = arr.Length;
// If size of current combination is r
if (data.Count == r) {
result.Add(new List<int>(data));
return;
}
// Replace index with all possible elements
for (int i = ind; i < n; i++) {
// Current element is included
data.Add(arr[i]);
// Recur for next elements
combinationUtil(i + 1, r, data, result, arr);
// Backtrack to find other combinations
data.RemoveAt(data.Count - 1);
}
}
// Function to find all combinations of size r
// in an array of size n
static List<List<int>> findCombination(int[] arr, int r) {
int n = arr.Length;
// to store the result
List<List<int>> result = new List<List<int>>();
// Temporary array to store current combination
List<int> data = new List<int>();
combinationUtil(0, r, data, result, arr);
return result;
}
static void Main() {
int[] arr = {1, 2, 3, 4};
int r = 2;
List<List<int>> res = findCombination(arr, r);
foreach (var comb in res) {
foreach (int num in comb) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
}
JavaScript
// Helper function to find all combinations
// of size r in an array of size n
function combinationUtil(ind, r, data, result, arr) {
const n = arr.length;
// If size of current combination is r
if (data.length === r) {
result.push([...data]);
return;
}
// Replace index with all possible elements
for (let i = ind; i < n; i++) {
// Current element is included
data.push(arr[i]);
// Recur for next elements
combinationUtil(i + 1, r, data, result, arr);
// Backtrack to find other combinations
data.pop();
}
}
// Function to find all combinations of size r
// in an array of size n
function findCombination(arr, r) {
const n = arr.length;
// to store the result
const result = [];
// Temporary array to store current combination
const data = [];
combinationUtil(0, r, data, result, arr);
return result;
}
const arr = [1, 2, 3, 4];
const r = 2;
const res = findCombination(arr, r);
for (const comb of res) {
for (const num of comb) {
process.stdout.write(num + " ");
}
console.log();
}
Output1 2
1 3
1 4
2 3
2 4
3 4
Time Complexity: O(r × C(n, r)), generates all combinations of size r
from n
elements, which is C(n, r) in total.
Each combination takes O(r) time to construct and store, leading to an overall complexity of O(r × C(n, r)).
Auxiliary Space: O(r), recursion depth reaches up to r
, so the maximum stack space used is O(r). We are not including the space used for storing the final combinations
Using Recursion and Including - Excluding Each Element - O(2 ^ n) Time and O(r) Space
The idea is to explore each element in the array by making a binary choice—either include it in the current combination or skip it—while keeping track of the elements chosen so far. We recursively advance through the array, adding elements until the temporary combination reaches size r (at which point it’s recorded), then backtrack to explore all other possibilities. This guarantees that every valid combination of size r is generated.
Follow the below given step-by-step approach:
- Initialize an empty list
data
to hold the current combination. - Define a recursive function with parameters
(ind, r, data, result, arr)
. - If
data.size() == r
, add a copy of data
to result
and return. - If
ind >= arr.size()
, return (no more elements to process). - Add
arr[ind]
to data
. - Recurse with
ind + 1
to include the current element. - Remove the last element from
data
(backtrack). - Recurse with
ind + 1
to explore combinations that exclude the current element.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// Helper function to find all combinations
// of size r in an array of size n
void combinationUtil(int ind, int r, vector<int> &data,
vector<vector<int>> &result, vector<int> &arr) {
int n = arr.size();
// If size of current combination is r
if (data.size() == r) {
result.push_back(data);
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.push_back(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.pop_back();
// exclude the current element and move to the next
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
vector<vector<int>> findCombination(vector<int> &arr, int r) {
int n = arr.size();
// to store the result
vector<vector<int>> result;
// Temporary array to store current combination
vector<int> data;
combinationUtil(0, r, data, result, arr);
return result;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
int r = 2;
vector<vector<int>> res = findCombination(arr, r);
for (const auto &comb : res) {
for (int num : comb) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
Java
import java.util.*;
class GfG {
// Helper function to find all combinations
// of size r in an array of size n
static void combinationUtil(int ind, int r, List<Integer> data,
List<List<Integer>> result, int[] arr) {
int n = arr.length;
// If size of current combination is r
if (data.size() == r) {
result.add(new ArrayList<>(data));
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.add(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.remove(data.size() - 1);
// exclude the current element and move to the next
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
static List<List<Integer>> findCombination(int[] arr, int r) {
int n = arr.length;
// to store the result
List<List<Integer>> result = new ArrayList<>();
// Temporary array to store current combination
List<Integer> data = new ArrayList<>();
combinationUtil(0, r, data, result, arr);
return result;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int r = 2;
List<List<Integer>> res = findCombination(arr, r);
for (List<Integer> comb : res) {
for (int num : comb) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
Python
# Helper function to find all combinations
# of size r in an array of size n
def combinationUtil(ind, r, data,
result, arr):
n = len(arr)
# If size of current combination is r
if len(data) == r:
result.append(data.copy())
return
# If no more elements are left to put in data
if ind >= n:
return
# include the current element
data.append(arr[ind])
# Recur for next elements
combinationUtil(ind + 1, r, data, result, arr)
# Backtrack to find other combinations
data.pop()
# exclude the current element and move to the next
combinationUtil(ind + 1, r, data, result, arr)
# Function to find all combinations of size r
# in an array of size n
def findCombination(arr, r):
n = len(arr)
# to store the result
result = []
# Temporary array to store current combination
data = []
combinationUtil(0, r, data, result, arr)
return result
arr = [1, 2, 3, 4]
r = 2
res = findCombination(arr, r)
for comb in res:
for num in comb:
print(num, end=" ")
print()
C#
using System;
using System.Collections.Generic;
class GfG {
// Helper function to find all combinations
// of size r in an array of size n
static void combinationUtil(int ind, int r, List<int> data,
List<List<int>> result, int[] arr) {
int n = arr.Length;
// If size of current combination is r
if (data.Count == r) {
result.Add(new List<int>(data));
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.Add(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.RemoveAt(data.Count - 1);
// exclude the current element and move to the next
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
static List<List<int>> findCombination(int[] arr, int r) {
int n = arr.Length;
// to store the result
List<List<int>> result = new List<List<int>>();
// Temporary array to store current combination
List<int> data = new List<int>();
combinationUtil(0, r, data, result, arr);
return result;
}
static void Main() {
int[] arr = {1, 2, 3, 4};
int r = 2;
List<List<int>> res = findCombination(arr, r);
foreach (var comb in res) {
foreach (int num in comb) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
}
JavaScript
// Helper function to find all combinations
// of size r in an array of size n
function combinationUtil(ind, r, data, result, arr) {
const n = arr.length;
// If size of current combination is r
if (data.length === r) {
result.push([...data]);
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.push(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.pop();
// exclude the current element and move to the next
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
function findCombination(arr, r) {
const n = arr.length;
// to store the result
const result = [];
// Temporary array to store current combination
const data = [];
combinationUtil(0, r, data, result, arr);
return result;
}
const arr = [1, 2, 3, 4];
const r = 2;
const res = findCombination(arr, r);
for (const comb of res) {
for (const num of comb) {
process.stdout.write(num + " ");
}
console.log();
}
Output1 2
1 3
1 4
2 3
2 4
3 4
Using Sort to Handle The Duplicate Elements in Input - O(2 ^ n) Time and O(n) Space
The idea is to eliminate duplicate combinations by first sorting the input array so that identical elements are adjacent, and then, during the recursive generation, skipping any element that is the same as its immediate predecessor at the same recursion depth. This ensures that each unique value is only considered once per position in the combination, preventing repeated outputs.
Follow the below given step-by-step approach:
- Sort the input array before invoking the recursive routine.
- In the recursive helper, iterate
i
from ind
to n–1
. - If
i > ind
and arr[i] == arr[i–1]
, skip this iteration to avoid duplicates. - Include
arr[i]
in the current combination list and recurse with ind = i + 1
. - Backtrack by removing the last element after the recursive call.
- Continue recursion to consider combinations that exclude
arr[i]
. - Whenever the current combination’s size reaches
r
, add it to the result.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// Helper function to find all combinations
// of size r in an array of size n
void combinationUtil(int ind, int r, vector<int> &data,
vector<vector<int>> &result, vector<int> &arr) {
int n = arr.size();
// If size of current combination is r
if (data.size() == r) {
result.push_back(data);
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.push_back(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.pop_back();
// exclude the current element and
// move to the next unique element
while(ind + 1 < n && arr[ind] == arr[ind + 1]) {
ind++;
}
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
vector<vector<int>> findCombination(vector<int> &arr, int r) {
int n = arr.size();
// to store the result
vector<vector<int>> result;
// sort the array
sort(arr.begin(), arr.end());
// Temporary array to store current combination
vector<int> data;
combinationUtil(0, r, data, result, arr);
return result;
}
int main() {
vector<int> arr = {1, 1, 2, 3, 4};
int r = 2;
vector<vector<int>> res = findCombination(arr, r);
for (const auto &comb : res) {
for (int num : comb) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
Java
import java.util.*;
class GfG {
// Helper function to find all combinations
// of size r in an array of size n
static void combinationUtil(int ind, int r, List<Integer> data,
List<List<Integer>> result, int[] arr) {
int n = arr.length;
// If size of current combination is r
if (data.size() == r) {
result.add(new ArrayList<>(data));
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.add(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.remove(data.size() - 1);
// exclude the current element and
// move to the next unique element
while (ind + 1 < n && arr[ind] == arr[ind + 1]) {
ind++;
}
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
static List<List<Integer>> findCombination(int[] arr, int r) {
int n = arr.length;
// to store the result
List<List<Integer>> result = new ArrayList<>();
// sort the array
Arrays.sort(arr);
// Temporary array to store current combination
List<Integer> data = new ArrayList<>();
combinationUtil(0, r, data, result, arr);
return result;
}
public static void main(String[] args) {
int[] arr = {1, 1, 2, 3, 4};
int r = 2;
List<List<Integer>> res = findCombination(arr, r);
for (List<Integer> comb : res) {
for (int num : comb) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
Python
# Helper function to find all combinations
# of size r in an array of size n
def combinationUtil(ind, r, data,
result, arr):
n = len(arr)
# If size of current combination is r
if len(data) == r:
result.append(data.copy())
return
# If no more elements are left to put in data
if ind >= n:
return
# include the current element
data.append(arr[ind])
# Recur for next elements
combinationUtil(ind + 1, r, data, result, arr)
# Backtrack to find other combinations
data.pop()
# exclude the current element and
# move to the next unique element
while ind + 1 < n and arr[ind] == arr[ind + 1]:
ind += 1
combinationUtil(ind + 1, r, data, result, arr)
# Function to find all combinations of size r
# in an array of size n
def findCombination(arr, r):
n = len(arr)
# to store the result
result = []
# sort the array
arr.sort()
# Temporary array to store current combination
data = []
combinationUtil(0, r, data, result, arr)
return result
arr = [1, 1, 2, 3, 4]
r = 2
res = findCombination(arr, r)
for comb in res:
for num in comb:
print(num, end=" ")
print()
C#
using System;
using System.Collections.Generic;
class GfG {
// Helper function to find all combinations
// of size r in an array of size n
static void combinationUtil(int ind, int r, List<int> data,
List<List<int>> result, int[] arr) {
int n = arr.Length;
// If size of current combination is r
if (data.Count == r) {
result.Add(new List<int>(data));
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.Add(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.RemoveAt(data.Count - 1);
// exclude the current element and
// move to the next unique element
while (ind + 1 < n && arr[ind] == arr[ind + 1]) {
ind++;
}
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
static List<List<int>> findCombination(int[] arr, int r) {
int n = arr.Length;
// to store the result
List<List<int>> result = new List<List<int>>();
// sort the array
Array.Sort(arr);
// Temporary array to store current combination
List<int> data = new List<int>();
combinationUtil(0, r, data, result, arr);
return result;
}
static void Main() {
int[] arr = {1, 1, 2, 3, 4};
int r = 2;
List<List<int>> res = findCombination(arr, r);
foreach (var comb in res) {
foreach (int num in comb) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
}
JavaScript
// Helper function to find all combinations
// of size r in an array of size n
function combinationUtil(ind, r, data, result, arr) {
const n = arr.length;
// If size of current combination is r
if (data.length === r) {
result.push([...data]);
return;
}
// If no more elements are left to put in data
if (ind >= n) {
return;
}
// include the current element
data.push(arr[ind]);
// Recur for next elements
combinationUtil(ind + 1, r, data, result, arr);
// Backtrack to find other combinations
data.pop();
// exclude the current element and
// move to the next unique element
while (ind + 1 < n && arr[ind] === arr[ind + 1]) {
ind++;
}
combinationUtil(ind + 1, r, data, result, arr);
}
// Function to find all combinations of size r
// in an array of size n
function findCombination(arr, r) {
const n = arr.length;
// to store the result
const result = [];
// sort the array
arr.sort((a, b) => a - b);
// Temporary array to store current combination
const data = [];
combinationUtil(0, r, data, result, arr);
return result;
}
const arr = [1, 1, 2, 3, 4];
const r = 2;
const res = findCombination(arr, r);
for (const comb of res) {
for (const num of comb) {
process.stdout.write(num + " ");
}
console.log();
}
Output1 1
1 2
1 3
1 4
2 3
2 4
3 4
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