House Robber - Maximum possible stolen value
Last Updated :
01 Oct, 2024
There are n houses built in a line, each of which contains some money in it. A robber wants to steal money from these houses, but he can’t steal from two adjacent houses. The task is to find the maximum amount of money which can be stolen.
Examples:
Input: hval[] = {6, 7, 1, 3, 8, 2, 4}
Output: 19
Explanation: The thief will steal from house 1, 3, 5 and 7, total money = 6 + 1 + 8 + 4 = 19.
Input: hval[] = {5, 3, 4, 11, 2}
Output: 16
Explanation: Thief will steal from house 1 and 4, total money = 5 + 11 = 16.
Given an array, the solution is to find the maximum sum subsequence where no two selected elements are adjacent.
[Naive Approach] Using Recursion- O(2n) Time and O(n) Space
The idea is to explore all the possibilities for each house using Recursion. We can start from the last house and for each house, we have two choices:
- Rob the current house and skip the house just before it.
- Skip the current house and move to the next house.
So, the recurrence relation will be:
maxLootRec(n) = max(hval[n - 1] + maxLootRec(n - 2), maxLootRec(n - 1)),
where maxLootRec(n) returns the maximum amount of money which can be stolen if n houses are left.
C++
// C++ Program to solve House Robber Problem using Recursion
#include <iostream>
#include <vector>
using namespace std;
// Calculate the maximum stolen value recursively
int maxLootRec(vector<int> &hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLootRec(hval, n - 2);
int notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return max(pick, notPick);
}
// Function to calculate the maximum stolen value
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval);
return 0;
}
C
// C Program to solve House Robber Problem using Recursion
#include <stdio.h>
// Function to calculate the maximum stolen value
int maxLoot(int *hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLoot(hval, n - 2);
int notPick = maxLoot(hval, n - 1);
// Return the max of two choices
return (pick > notPick) ? pick : notPick;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Recursion
class GfG {
// Calculate the maximum stolen value recursively
static int maxLootRec(int[] hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLootRec(hval, n - 2);
int notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return Math.max(pick, notPick);
}
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Recursion
# Calculate the maximum stolen value recursively
def maxLootRec(hval, n):
# If no houses are left, return 0.
if n <= 0:
return 0
# If only 1 house is left, rob it.
if n == 1:
return hval[0]
# Two Choices: Rob the nth house and do not rob the nth house
pick = hval[n - 1] + maxLootRec(hval, n - 2)
notPick = maxLootRec(hval, n - 1)
# Return the max of two choices
return max(pick, notPick)
# Function to calculate the maximum stolen value
def maxLoot(hval):
n = len(hval)
# Call the recursive function for n houses
return maxLootRec(hval, n)
if __name__ == "__main__":
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Recursion
using System;
class GfG {
// Calculate the maximum stolen value recursively
static int MaxLootRec(int[] hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + MaxLootRec(hval, n - 2);
int notPick = MaxLootRec(hval, n - 1);
// Return the max of two choices
return Math.Max(pick, notPick);
}
// Function to calculate the maximum stolen value
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Call the recursive function for n houses
return MaxLootRec(hval, n);
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Recursion
// Calculate the maximum stolen value recursively
function maxLootRec(hval, n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n === 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
let pick = hval[n - 1] + maxLootRec(hval, n - 2);
let notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return Math.max(pick, notPick);
}
// Function to calculate the maximum stolen value
function maxLoot(hval) {
let n = hval.length;
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
let hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(2n). Every house has 2 choices to pick and not pick.
Auxiliary Space: O(n). For recursion stack space
[Better Approach] Using Memoization - O(n) Time and O(n) Space
The above solution has optimal substructure and overlapping subproblems. See the below recursion tree, maxLootRec(2) is being evaluated twice.
Recursion Tree for House RobberWe can optimize this solution using a memo array of size (n + 1), such that memo[i] represents the maximum value that can be collected from first i houses. Please note that there is only one parameter that changes in recursion and the range of this parameter is from 0 to n.
C++
// C++ Program to solve House Robber Problem using Memoization
#include <iostream>
#include <vector>
using namespace std;
int maxLootRec(const vector<int>& hval, int n, vector<int>& memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = max(pick, notPick);
return memo[n];
}
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Initialize memo array with -1
vector<int> memo(n + 1, -1);
return maxLootRec(hval, n, memo);
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval);
return 0;
}
C
// C Program to solve House Robber Problem using Memoization
#include <stdio.h>
#include <stdlib.h>
int maxLootRec(const int* hval, int n, int* memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = (pick > notPick) ? pick : notPick;
return memo[n];
}
int maxLoot(int* hval, int n) {
// Initialize memo array with -1
int memo[n + 1];
for (int i = 0; i <= n; ++i) {
memo[i] = -1;
}
int result = maxLootRec(hval, n, memo);
return result;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Memoization
import java.util.Arrays;
class GfG {
static int maxLootRec(int[] hval, int n, int[] memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
// Initialize memo array with -1
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
return maxLootRec(hval, n, memo);
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Memoization
def maxLootRec(hval, n, memo):
if n <= 0:
return 0
if n == 1:
return hval[0]
# Check if the result is already computed
if memo[n] != -1:
return memo[n]
pick = hval[n - 1] + maxLootRec(hval, n - 2, memo)
notPick = maxLootRec(hval, n - 1, memo)
# Store the max of two choices in the memo array and return it
memo[n] = max(pick, notPick)
return memo[n]
def maxLoot(hval):
n = len(hval)
# Initialize memo array with -1
memo = [-1] * (n + 1)
return maxLootRec(hval, n, memo)
if __name__ == "__main__":
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Memoization
using System;
class GfG {
static int MaxLootRec(int[] hval, int n, int[] memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + MaxLootRec(hval, n - 2, memo);
int notPick = MaxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.Max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Initialize memo array with -1
int[] memo = new int[n + 1];
for (int i = 0; i <= n; i++) {
memo[i] = -1;
}
return MaxLootRec(hval, n, memo);
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Memoization
function maxLootRec(hval, n, memo) {
if (n <= 0) return 0;
if (n === 1) return hval[0];
// Check if the result is already computed
if (memo[n] !== -1) return memo[n];
const pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
const notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
function maxLoot(hval) {
const n = hval.length;
// Initialize memo array with -1
const memo = new Array(n + 1).fill(-1);
return maxLootRec(hval, n, memo);
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n). Every house is computed only once.
Auxiliary Space: O(n). For recursion stack space and memo array.
[Expected Approach 1] Using Tabulation - O(n) Time and O(n) Space
The idea is to build the solution in bottom-up manner. We create a dp[] array of size n+1 where dp[i] represents the maximum value that can be collected with first i houses. We first fill the known values, dp[0] and dp[1] and then fill the remaining values using the formula: dp[i] = max(hval[i] + dp[i - 2], dp[i - 1]). The final result will be stored at dp[n].
C++
// C++ Program to solve House Robber Problem using Tabulation
#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the maximum stolen value using bottom-up DP
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Create a dp array to store the maximum loot at each house
vector<int> dp(n+1, 0);
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++)
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval) << endl;
return 0;
}
C
// C Program to solve House Robber Problem using Tabulation
#include <stdio.h>
int max(int a, int b) {return (a > b) ? a : b;}
int maxLoot(int* hval, int n) {
// Create a dp array to store the
// maximum loot at each house
int dp[n+1];
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the
// bottom-up approach
for (int i = 2; i <= n; i++)
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Tabulation
class GfG {
// Function to calculate the maximum stolen value using bottom-up DP
static int maxLoot(int[] hval) {
int n = hval.length;
// Create a dp array to store the maximum loot at each house
int[] dp = new int[n + 1];
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++) {
dp[i] = Math.max(hval[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[n];
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Tabulation
def maxLoot(hval):
n = len(hval)
# Create a dp array to store the maximum loot at each house
dp = [0] * (n + 1)
# Base cases
dp[0] = 0
dp[1] = hval[0]
# Fill the dp array using the bottom-up approach
for i in range(2, n + 1):
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1])
return dp[n]
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Tabulation
using System;
class GfG {
// Function to calculate the maximum stolen value using bottom-up DP
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Create a dp array to store the maximum loot at each house
int[] dp = new int[n + 1];
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++) {
dp[i] = Math.Max(hval[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[n];
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Tabulation
function maxLoot(hval) {
const n = hval.length;
// Create a dp array to store the maximum loot at each house
const dp = new Array(n + 1).fill(0);
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (let i = 2; i <= n; i++)
dp[i] = Math.max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n), Every house is computed only once.
Auxiliary Space O(n), We are using a dp array of size n.
[Expected Approach 2] Space-Optimized DP - O(n) Time and O(1) Space
On observing the dp[] array in the previous approach, it can be seen that the answer at the current index depends only on the last two values. In other words, dp[i] depends only on dp[i - 1] and dp[i - 2]. So, instead of storing the result in an array, we can simply use two variables to store the last and second last result.
C++
// C++ Program to solve House Robber Problem using
// Space Optimized Tabulation
#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the maximum stolen value
int maxLoot(vector<int> &hval) {
int n = hval.size();
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
int res;
for (int i = 1; i < n; i++) {
res = max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval) << endl;
return 0;
}
C
// C Program to solve House Robber Problem using
// Space Optimized Tabulation
#include <stdio.h>
int max(int a, int b) { return (a > b) ? a : b; }
// Function to calculate the maximum stolen value
int maxLoot(int hval[], int n) {
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous
// two values. The final current value
// would be our result
int res;
for (int i = 1; i < n; i++) {
res = max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using
// Space Optimized Tabulation
import java.util.Arrays;
class GfG {
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous
// two values. The final current value
// would be our result
int res = 0;
for (int i = 1; i < n; i++) {
res = Math.max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using
# Space Optimized Tabulation
# Function to calculate the maximum stolen value
def maxLoot(hval):
n = len(hval)
if n == 0:
return 0
if n == 1:
return hval[0]
# Set previous 2 values
secondLast = 0
last = hval[0]
# Compute current value using previous two values
# The final current value would be our result
res = 0
for i in range(1, n):
res = max(hval[i] + secondLast, last)
secondLast = last
last = res
return res
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using
// Space Optimized Tabulation
using System;
class GfG {
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.Length;
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
int res = 0;
for (int i = 1; i < n; i++) {
res = Math.Max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(maxLoot(hval));
}
}
JavaScript
// Function to calculate the maximum stolen value
function maxLoot(hval) {
const n = hval.length;
if (n === 0)
return 0;
if (n === 1)
return hval[0];
// Set previous 2 values
let secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
let res;
for (let i = 1; i < n; i++) {
res = Math.max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n), Every value is computed only once.
Auxiliary Space: O(1), as we are using only two variables.
Find maximum possible stolen value - House Robber Problem
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