The Painter's Partition Problem
Last Updated :
29 Apr, 2025
Given an array arr[] and k, where the array represents the boards and each element of the given array represents the length of each board. k numbers of painters are available to paint these boards. Consider that each unit of a board takes 1 unit of time to paint. The task is to find the minimum time to get this job done by painting all the boards under the constraint that any painter will only paint the continuous sections of boards. say board [2, 3, 4] or only board [1] or nothing but not board [2, 4, 5].
Examples:
Input: arr[] = [5, 10, 30, 20, 15], k = 3
Output: 35
Explanation: The most optimal way will be: Painter 1 allocation : [5,10], Painter 2 allocation : [30], Painter 3 allocation : [20, 15], Job will be done when all painters finish i.e. at time = max(5 + 10, 30, 20 + 15) = 35
Input: arr[] = [10, 20, 30, 40], k = 2
Output: 60
Explanation: The most optimal way to paint: Painter 1 allocation : [10, 20, 30], Painter 2 allocation : [40], Job will be complete at time = 60
Naive Approach - Using recursion
A brute force solution is to consider all possible sets of contiguous partitions and calculate the maximum sum partition in each case and return the minimum of all these cases.
In this problem, we need to minimize the maximum time taken by any painter to complete their assigned boards. We have k painters who can work simultaneously. The recursive approach explores every possible way of dividing the boards among the painters and then selects the division that minimizes the maximum time.
The recursive function tries assigning boards from index curr to last index to each painter, then recursively computes the minimum time for the remaining boards and painters.
Recurrence Relation:
Let minTime(curr, k) represent the minimum time to paint the boards from curr to the end with k remaining painters. The recurrence relation is:
- minTime(curr, k) = min {max(sum(curr, i) + minTime(i + 1, k - 1))}
Where:
sum(curr, i) is the total time for the current painter to paint boards from index curr to i where i can range from curr to n-1.
minTime(i + 1, k - 1) is the recursive call for the remaining boards and painters.
Base Cases:
No boards left (curr >= n): Return 0, as there’s nothing to paint.
No painters left (k == 0): Return infinity as it's infeasible to paint with no painters.
C++
// C++ program to find the minimum time for
// painter's partation problem using recursion.
#include <bits/stdc++.h>
using namespace std;
int minimizeTime(int curr, int n, vector<int> &arr, int k) {
// If all boards are painted, return 0
if (curr >= n)
return 0;
// If no painters are left
if (k == 0)
return INT_MAX;
// Current workload for this painter
int currSum = 0;
// Result to store the minimum possible time
int res = INT_MAX;
// Divide the boards among painters starting from curr
for (int i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign arr[curr..i] to
// this painter
int remTime = minimizeTime(i + 1, n, arr, k - 1);
int remaining = max(currSum, remTime);
// Update the result
res = min(res, remaining);
}
return res;
}
int minTime(vector<int> &arr, int k) {
int n = arr.size();
return minimizeTime(0, n, arr, k);
}
int main() {
vector<int> arr = {10, 10, 10, 10};
int k = 2;
int res = minTime(arr, k);
cout << res << endl;
return 0;
}
Java
// Java program to find the minimum time for
// painter's partation problem using recursion.
import java.util.*;
class GfG {
static int minimizeTime(int curr, int n, int[] arr,
int k) {
// Base cases
// All boards are painted
if (curr >= n)
return 0;
// No painters left
if (k == 0)
return Integer.MAX_VALUE;
// Current workload for this painter
int currSum = 0;
int res = Integer.MAX_VALUE;
// Divide the boards among painters starting from
// curr
for (int i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign
// arr[curr..i] to this painter
int remTime
= minimizeTime(i + 1, n, arr, k - 1);
int remaining = Math.max(currSum, remTime);
// Update the result
res = Math.min(res, remaining);
}
return res;
}
static int minTime(int[] arr, int k) {
int n = arr.length;
return minimizeTime(0, n, arr, k);
}
public static void main(String[] args) {
int[] arr = { 10, 10, 10, 10 };
int k = 2;
int res = minTime(arr, k);
System.out.println(res);
}
}
Python
# Python program to find the minimum time for
# painter's partation problem using recursion.
def minimizeTime(curr, n, arr, k):
# Base cases
if curr >= n:
return 0
# No painters left, infeasible case
if k == 0:
return float('inf')
# Current workload for this painter
currSum = 0
# Result to store the minimum possible time
res = float('inf')
# Divide the boards among painters starting from curr
for i in range(curr, n):
currSum += arr[i]
# Find the maximum time if we assign
# arr[curr..i] to this painter
remTime = minimizeTime(i + 1, n, arr, k - 1)
maxTime = max(currSum, remTime)
# Update the result
res = min(res, maxTime)
return res
def minTime(arr, k):
n = len(arr)
return minimizeTime(0, n, arr, k)
if __name__ == "__main__":
arr = [10, 10, 10, 10]
k = 2
res = minTime(arr, k)
print(res)
C#
// C# program to find the minimum time for
// painter's partation problem using recursion.
using System;
class GfG {
static int minimizeTime(int curr, int n, int[] arr,
int k) {
// Base cases
// All boards are painted
if (curr >= n)
return 0;
// No painters left, infeasible case
if (k == 0)
return int.MaxValue;
// Current workload for this painter
int currSum = 0;
// Result to store the minimum possible time
int res = int.MaxValue;
// Divide the boards among painters starting from
// curr
for (int i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign
// arr[curr..i] to this painter
int remTime
= minimizeTime(i + 1, n, arr, k - 1);
int maxTime = Math.Max(currSum, remTime);
// Update the result
res = Math.Min(res, maxTime);
}
return res;
}
static int minTime(int[] arr, int k) {
int n = arr.Length;
return minimizeTime(0, n, arr, k);
}
static void Main(string[] args) {
int[] arr = { 10, 10, 10, 10 };
int k = 2;
int res = minTime(arr, k);
Console.WriteLine(res);
}
}
JavaScript
// JavaScript program to find the minimum time for
// painter's partation problem using recursion.
function minimizeTime(curr, n, arr, k) {
// Base cases
// All boards are painted
if (curr >= n)
return 0;
// No painters left
if (k === 0)
return Infinity;
// Current workload for this painter
let currSum = 0;
let res = Infinity;
// Divide the boards among painters starting from curr
for (let i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign arr[curr..i]
// to this painter
let remTime = minimizeTime(i + 1, n, arr, k - 1);
let maxTime = Math.max(currSum, remTime);
// Update the result
res = Math.min(res, maxTime);
}
return res;
}
function minTime(arr, k) {
const n = arr.length;
return minimizeTime(0, n, arr, k);
}
const arr = [ 10, 10, 10, 10 ];
const k = 2;
const res = minTime(arr, k);
console.log(res);
The above approach will have a exponential time complexity.
Using Memoization - O(n*n*k) Time and O(n*k) Space
If we closely observe the recursive function minTime(), it exhibits the property of overlapping subproblems, where the same subproblem is solved repeatedly in different recursive paths. This redundancy can be avoided by applying memoization. Since the changing parameters in the recursive calls are the current index (curr) and the number of remaining painters (k), we can utilize a 2D array of size n x (k+1) to store the results. We initialize this array with -1 to signify that a value has not been computed yet, thus preventing redundant calculations.
C++
// C++ program to find the minimum time for
// painter's partition problem using memoization.
#include <bits/stdc++.h>
using namespace std;
int minimizeTime(int curr, int n, vector<int> &arr, int k,
vector<vector<int>> &memo) {
// If all boards are painted, return 0
if (curr >= n)
return 0;
// If no painters are left
if (k == 0)
return INT_MAX;
// Check if the result is already computed and stored in memo table
// If so, return the stored result to avoid recomputation
if (memo[curr][k] != -1)
return memo[curr][k];
// Current workload for this painter
int currSum = 0;
// Result to store the minimum possible time
int res = INT_MAX;
// Divide the boards among painters starting from curr
for (int i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign arr[curr..i] to
// this painter
int remTime = minimizeTime(i + 1, n, arr, k - 1, memo);
int remaining = max(currSum, remTime);
// Update the result
res = min(res, remaining);
}
// Store the computed result in the memo table
// This helps avoid redundant calculations in future calls
return memo[curr][k] = res;
}
int minTime(vector<int> &arr, int k) {
int n = arr.size();
// Initialize memoization table with -1
// (indicating no result computed yet)
vector<vector<int>> memo(n, vector<int>(k + 1, -1));
return minimizeTime(0, n, arr, k, memo);
}
int main() {
vector<int> arr = {10, 10, 10, 10};
int k = 2;
int res = minTime(arr, k);
cout << res << endl;
return 0;
}
Java
// Java program to find the minimum time for
// painter's partition problem using memoization.
import java.util.*;
class GfG {
static int minimizeTime(int curr, int n, int[] arr,
int k, int[][] memo) {
// If all boards are painted, return 0
if (curr >= n) {
return 0;
}
// If no painters are left
if (k == 0) {
return Integer.MAX_VALUE;
}
// Check if the result is already computed and
// stored in the memo table If so, return the stored
// result to avoid recomputation
if (memo[curr][k] != -1) {
return memo[curr][k];
}
// Current workload for this painter
int currSum = 0;
// Result to store the minimum possible time
int res = Integer.MAX_VALUE;
// Divide the boards among painters starting from
// curr
for (int i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign
// arr[curr..i] to this painter
int remTime
= minimizeTime(i + 1, n, arr, k - 1, memo);
int remaining = Math.max(currSum, remTime);
// Update the result
res = Math.min(res, remaining);
}
// Store the computed result in the memo table
// This helps avoid redundant calculations in future
// calls
memo[curr][k] = res;
return res;
}
static int minTime(int[] arr, int k) {
int n = arr.length;
// Initialize memoization table with -1 (indicating
// no result computed yet)
int[][] memo = new int[n][k + 1];
for (int[] row : memo) {
Arrays.fill(row, -1);
}
return minimizeTime(0, n, arr, k, memo);
}
public static void main(String[] args) {
int[] arr = { 10, 10, 10, 10 };
int k = 2;
int res = minTime(arr, k);
System.out.println(res);
}
}
Python
# Python program to find the minimum time for
# painter's partition problem using recursion
# using memoization
def minimizeTime(curr, n, arr, k, memo):
# If all boards are painted, return 0
if curr >= n:
return 0
# If no painters are left
if k == 0:
return float('inf')
# Check if the result is already computed
# and stored in memo table
# If so, return the stored result to avoid
# recomputation
if memo[curr][k] != -1:
return memo[curr][k]
# Current workload for this painter
currSum = 0
# Result to store the minimum possible time
res = float('inf')
# Divide the boards among painters starting from curr
for i in range(curr, n):
currSum += arr[i]
# Find the maximum time if we assign
# arr[curr..i] to this painter
remTime = minimizeTime(i + 1, n, arr, k - 1, memo)
remaining = max(currSum, remTime)
# Update the result
res = min(res, remaining)
# Store the computed result in the memo table
# This helps avoid redundant calculations in
# future calls
memo[curr][k] = res
return res
def minTime(arr, k):
n = len(arr)
# Initialize memoization table with -1
# (indicating no result computed yet)
memo = [[-1] * (k + 1) for _ in range(n)]
return minimizeTime(0, n, arr, k, memo)
if __name__ == "__main__":
arr = [10, 10, 10, 10]
k = 2
res = minTime(arr, k)
print(res)
C#
// C# program to find the minimum time for
// painter's partition problem using memoization.
using System;
class GfG {
static int minimizeTime(int curr, int n, int[] arr,
int k, int[, ] memo) {
// If all boards are painted, return 0
if (curr >= n)
return 0;
// If no painters are left
if (k == 0)
return int.MaxValue;
// Check if the result is already computed and
// stored in memo table If so, return the stored
// result to avoid recomputation
if (memo[curr, k] != -1)
return memo[curr, k];
// Current workload for this painter
int currSum = 0;
// Result to store the minimum possible time
int res = int.MaxValue;
// Divide the boards among painters starting from
// curr
for (int i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign
// arr[curr..i] to this painter
int remTime
= minimizeTime(i + 1, n, arr, k - 1, memo);
int remaining = Math.Max(currSum, remTime);
// Update the result
res = Math.Min(res, remaining);
}
// Store the computed result in the memo table
// This helps avoid redundant calculations in future
// calls
memo[curr, k] = res;
return res;
}
static int minTime(int[] arr, int k) {
int n = arr.Length;
// Initialize memoization table with -1 (indicating
// no result computed yet)
int[, ] memo = new int[n, k + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
memo[i, j] = -1;
}
}
return minimizeTime(0, n, arr, k, memo);
}
static void Main(string[] args) {
int[] arr = { 10, 10, 10, 10 };
int k = 2;
int result = minTime(arr, k);
Console.WriteLine(result);
}
}
JavaScript
// JavaScript program to find the minimum time for
// painter's partition problem using memoization.
function minimizeTime(curr, n, arr, k, memo) {
// If all boards are painted, return 0
if (curr >= n)
return 0;
// If no painters are left
if (k === 0)
return Number.POSITIVE_INFINITY;
// Check if the result is already computed and stored in
// memo table If so, return the stored result to avoid
// recomputation
if (memo[curr][k] !== -1)
return memo[curr][k];
// Current workload for this painter
let currSum = 0;
// Result to store the minimum possible time
let res = Number.POSITIVE_INFINITY;
// Divide the boards among painters starting from curr
for (let i = curr; i < n; i++) {
currSum += arr[i];
// Find the maximum time if we assign arr[curr..i]
// to this painter
let remTime
= minimizeTime(i + 1, n, arr, k - 1, memo);
let remaining = Math.max(currSum, remTime);
// Update the result
res = Math.min(res, remaining);
}
// Store the computed result in the memo table
// This helps avoid redundant calculations in future
// calls
memo[curr][k] = res;
return res;
}
function minTime(arr, k) {
const n = arr.length;
// Initialize memoization table with -1 (indicating no
// result computed yet)
let memo = Array.from({length : n},
() => Array(k + 1).fill(-1));
return minimizeTime(0, n, arr, k, memo);
}
const arr = [ 10, 10, 10, 10 ];
const k = 2;
const res = minTime(arr, k);
console.log(res);
Using Tabulation - O(n*n*k) Time and O(n*k) Space
The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner.
We create a 2D array dp, where dp[i][j] represents the minimum time required to paint boards starting from index i to the last board (n-1) using j painters.
The recurrence relation for filling this DP table is as follows:
- dp[i][j] = min(dp[i][j], max(sum(i, i1), dp[i1 + 1][j - 1]))
Here:
- i1 ranges from i to n-1, representing the point where the current painter stops painting (i.e., from i to i1).
- sum(i, i1) represents the total time taken by the current painter to paint the boards from index i to i1. This is calculated using a prefix sum array for efficiency.
- dp[i1 + 1][j - 1] represents the minimum time required to paint the remaining boards from i1 + 1 to n-1 using j - 1 painters.
We take the maximum of sum(i, i1) and dp[i1 + 1][j - 1] because the time taken by the current painter is limited by the maximum time among the painters, and the goal is to minimize the maximum time.
Base Case:
dp[n][j] = 0 for all j from 0 to n-1.
This represents the scenario where there are no boards left to paint, so the time required is 0, regardless of the number of painters remaining.
C++
// C++ program to find the minimum time for
// painter's partition problem using tabulation.
#include <bits/stdc++.h>
using namespace std;
int minTime(vector<int> &arr, int k) {
int n = arr.size();
vector<int> pre(n, 0);
// Calculate the prefix sum array
pre = vector<int>(n + 1, 0);
pre[0] = arr[0];
for (int i = 1; i < n; i++) {
pre[i] = pre[i - 1] + arr[i];
}
// DP table to store the minimum time for subproblems
vector<vector<int>> dp(n + 1, vector<int>(k + 1, 1e9));
// Base case: if there are no boards to paint, the time is 0
for (int i = 0; i <= k; i++) {
dp[n][i] = 0;
}
// Fill the DP table by iterating over the boards and painters
for (int i = n - 1; i >= 0; i--) {
for (int j = 1; j <= k; j++) {
for (int i1 = i; i1 < n; i1++) {
// Calculate the sum directly using the prefix sum array
int currSum = pre[i1] - (i > 0 ? pre[i - 1] : 0);
// Transition: take the maximum of the
// current sum and the result for
// remaining painters
dp[i][j] = min(dp[i][j], max(currSum, dp[i1 + 1][j - 1]));
}
}
}
// Return the minimum time for painting all boards
// starting from index 0 with k painters
return dp[0][k];
}
int main() {
vector<int> arr = {10, 10, 10, 10};
int k = 2;
int res = minTime(arr, k);
cout << res;
return 0;
}
Java
// Java program to find the minimum time for
// painter's partition problem using tabulation.
import java.util.*;
class GfG {
static int minTime(int[] arr, int k) {
int n = arr.length;
// Prefix sum array to store the cumulative sums of
// boards
int[] pre = new int[n + 1];
// Calculate the prefix sum array
pre[0] = 0;
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + arr[i - 1];
}
// DP table to store the minimum time for
// subproblems
int[][] dp = new int[n + 1][k + 1];
// Initialize DP table with a large value
for (int i = 0; i <= n; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE);
}
// Base case: If no boards are left, the time is 0
// for all painters
for (int i = 0; i <= k; i++) {
dp[n][i] = 0;
}
// Fill the DP table by iterating over the boards
// and painters
for (int i = n - 1; i >= 0; i--) {
for (int j = 1; j <= k; j++) {
for (int i1 = i; i1 < n; i1++) {
// Calculate the sum of boards from
// index i to i1 using the prefix sum
// array
int currSum = pre[i1 + 1] - pre[i];
// Transition: take the maximum of the
// current sum and the result for
// remaining painters
dp[i][j] = Math.min(
dp[i][j],
Math.max(currSum,
dp[i1 + 1][j - 1]));
}
}
}
// Return the minimum time for painting all boards
// starting from index 0 with k painters
return dp[0][k];
}
public static void main(String[] args) {
int[] arr = { 10, 10, 10, 10 };
int k = 2;
int res = minTime(arr, k);
System.out.println(res);
}
}
Python
# Python program to find the minimum time for
# painter's partition problem using tabulation.
def minTime(arr, k):
n = len(arr)
# Prefix sum array to store the cumulative sums
# of boards
pre = [0] * (n + 1)
# Calculate the prefix sum array
for i in range(1, n + 1):
pre[i] = pre[i - 1] + arr[i - 1]
# DP table to store the minimum time for subproblems
dp = [[float('inf')] * (k + 1) for _ in range(n + 1)]
# Base case: If no boards are left, the time is
# 0 for all painters
for i in range(k + 1):
dp[n][i] = 0
# Fill the DP table by iterating over the boards
# and painters
for i in range(n - 1, -1, -1):
for j in range(1, k + 1):
for i1 in range(i, n):
# Calculate the sum of boards from index
# i to i1
# using the prefix sum array
currSum = pre[i1 + 1] - pre[i]
# Transition: take the maximum of the current sum
# and the result for remaining painters
dp[i][j] = min(dp[i][j], max(currSum, dp[i1 + 1][j - 1]))
# Return the minimum time for painting all boards
# starting from
# index 0 with k painters
return dp[0][k]
arr = [10, 10, 10, 10]
k = 2
res = minTime(arr, k)
print(res)
C#
// C# program to find the minimum time for
// painter's partition problem using tabulation.
using System;
class GfG {
static int minTime(int[] arr, int k) {
int n = arr.Length;
// Prefix sum array to store the cumulative sums of
// boards
int[] pre = new int[n + 1];
// Calculate the prefix sum array
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + arr[i - 1];
}
// DP table to store the minimum time for
// subproblems
int[, ] dp = new int[n + 1, k + 1];
// Initialize the DP table with a large value
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= k; j++) {
dp[i, j] = int.MaxValue;
}
}
// Base case: If no boards are left, the time is 0
// for all painters
for (int i = 0; i <= k; i++) {
dp[n, i] = 0;
}
// Fill the DP table by iterating over the boards
// and painters
for (int i = n - 1; i >= 0; i--) {
for (int j = 1; j <= k; j++) {
for (int i1 = i; i1 < n; i1++) {
// Calculate the sum of boards from
// index i to i1 using the prefix sum
// array
int currSum = pre[i1 + 1] - pre[i];
// Transition: take the maximum of the
// current sum and the result for
// remaining painters
dp[i, j] = Math.Min(
dp[i, j],
Math.Max(currSum,
dp[i1 + 1, j - 1]));
}
}
}
// Return the minimum time for painting all boards
// starting from index 0 with k painters
return dp[0, k];
}
static void Main() {
int[] arr = { 10, 10, 10, 10 };
int k = 2;
int res = minTime(arr, k);
Console.WriteLine(res);
}
}
JavaScript
// JavaScript program to find the minimum time for
// painter's partition problem using tabulation.
function minTime(arr, k) {
let n = arr.length;
// Prefix sum array to store the cumulative sums of
// boards
let pre = new Array(n + 1).fill(0);
// Calculate the prefix sum array
for (let i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + arr[i - 1];
}
// DP table to store the minimum time for subproblems
let dp = Array.from(
{length : n + 1},
() => new Array(k + 1).fill(Number.MAX_VALUE));
// Base case: If no boards are left, the time is 0 for
// all painters
for (let i = 0; i <= k; i++) {
dp[n][i] = 0;
}
// Fill the DP table by iterating over the boards and
// painters
for (let i = n - 1; i >= 0; i--) {
for (let j = 1; j <= k; j++) {
for (let i1 = i; i1 < n; i1++) {
// Calculate the sum of boards from index i
// to i1 using the prefix sum array
let currSum = pre[i1 + 1] - pre[i];
// Transition: take the maximum of the
// current sum and the result for remaining
// painters
dp[i][j] = Math.min(
dp[i][j],
Math.max(currSum, dp[i1 + 1][j - 1]));
}
}
}
// Return the minimum time for painting all boards
// starting from index 0 with k painters
return dp[0][k];
}
let arr = [ 10, 10, 10, 10 ];
let k = 2;
let res = minTime(arr, k);
console.log(res);
Using Binary Search - O(n*log(sum(arr) - MAX)) Time
The idea is based on the observation that if we fix a time limit (say mid), we can check whether it's possible to divide the boards among k painters such that no one paints for more than this time. We apply Binary Search on the answer space to minimize the maximum time taken by any painter.
- The highest possible value in this range is the sum of all elements in the array - this represents the case when one painter paints all the boards.
- The lowest possible value is the maximum element of the array - this ensures at least one painter paints the largest board, and others are assigned workloads not exceeding this value.
- For each mid value, we simulate the partitioning and check if it's feasible using the given number of painters.
- As the time limit increases, the number of painters required decreases, and vice versa. This monotonic property ensures the search space is ordered, making it ideal for binary search.
Please refer Painter's Problem using Binary Search for detailed explanation and implementation.
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