Count digit groupings of a number with given constraints
Last Updated :
23 Jul, 2025
We are given a string consisting of digits, we may group these digits into sub-groups (but maintaining their original order). The task is to count the number of groupings such that for every sub-group except the last one, the sum of digits in a sub-group is less than or equal to the sum of the digits in the sub-group immediately on its right.
For example, a valid grouping of digits of number 1119 is (1-11-9). Sum of digits in first subgroup is 1, next subgroup is 2, and last subgroup is 9. Sum of every subgroup is less than or equal to its immediate right.
Examples :
Input : "1119"
Output: 7
Sub-groups: [1-119], [1-1-19], [1-11-9], [1-1-1-9],
[11-19] and [111-9].
Note : Here we have included [1119] in the group and
the sum of digits is 12 and this group has no
immediate right.
Input : "1234"
Output: 6
Sub-groups : [1234], [1-234], [12-34], [1-2-3-4],
[12-3-4] and [1-2-34]
Let "length" be the length of the input number. A recursive solution is to consider every position from 0 length-1. For every position, recursively count all possible subgroups after it.
Below is C++ implementation of the naive recursive solution.
C++
// C++ program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal to
// its immediate right subgroup.
#include<bits/stdc++.h>
using namespace std;
// Function to find the subgroups
int countGroups(int position,
int previous_sum,
int length, char *num)
{
// Terminating Condition
if (position == length)
return 1;
int res = 0;
// sum of digits
int sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (int i = position; i < length; i++)
{
sum += (num[i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
// Total number of subgroups
// till current position
return res;
}
// Driver Code
int main()
{
char num[] = "1119";
int len = strlen(num);
cout << countGroups(0, 0, len, num);
return 0;
}
Java
// Java program to count number
// of ways to group digits of
// a number such that sum of
// digits in every subgroup is
// less than or equal to its
// immediate right subgroup.
import java.io.*;
class GFG
{
// Function to find
// the subgroups
static int countGroups(int position,
int previous_sum,
int length,
String num)
{
// Terminating Condition
if (position == length)
return 1;
int res = 0;
// sum of digits
int sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (int i = position; i < length; i++)
{
sum += (num.charAt(i) - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
// Total number of subgroups
// till current position
return res;
}
// Driver Code
public static void main (String[] args)
{
String num = "1119";
int len =num .length();
System.out.println(countGroups(0, 0,
len, num));
}
}
// This code is contributed by anuj_67.
Python3
# Python3 program to count
# number of ways to group digits
# of a number such that sum of
# digits in every subgroup
# is less than or equal to its immediate
# right subgroup.
# Function to find the subgroups
def countGroups(position, previous_sum,
length, num):
# Terminating Condition
if(position == length):
return 1
res = 0
# sum of digits
sum = 0
# Traverse all digits from
# current position to rest
# of the length of string
for i in range(position, length):
sum = sum + int(num[i])
# If forward_sum is greater
# than the previous sum,
# then call the method again
if (sum >= previous_sum):
# Note : We pass current
# sum as previous sum
res = res + countGroups(i + 1, sum, length, num)
# Total number of subgroups
# till the current position
return res
# Driver Code
if __name__=='__main__':
num = "1119"
len = len(num)
print(countGroups(0, 0, len, num))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to count number
// of ways to group digits of
// a number such that sum of
// digits in every subgroup is
// less than or equal to its
// immediate right subgroup.
using System;
class GFG
{
// Function to find
// the subgroups
static int countGroups(int position,
int previous_sum,
int length,
String num)
{
// Terminating Condition
if (position == length)
return 1;
int res = 0;
// sum of digits
int sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (int i = position; i < length; i++)
{
sum += (num[i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
// Total number of subgroups
// till current position
return res;
}
// Driver Code
public static void Main ()
{
String num = "1119";
int len = num.Length;
Console.Write(countGroups(0, 0, len, num));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to count number
// of ways to group digits of
// a number such that sum of
// digits in every subgroup is
// less than or equal to its
// immediate right subgroup.
// Function to find
// the subgroups
function countGroups(position,
previous_sum,
length, num)
{
// Terminating Condition
if (position == length)
return 1;
let res = 0;
// sum of digits
let sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (let i = position; i < length; i++)
{
sum += (num[i].charCodeAt() - '0'.charCodeAt());
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum, length, num);
}
// Total number of subgroups
// till current position
return res;
}
let num = "1119";
let len = num.length;
document.write(countGroups(0, 0, len, num));
</script>
PHP
<?php
// PHP program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal
// to its immediate right subgroup.
// Function to find the subgroups
function countGroups($position,
$previous_sum,
$length,$num)
{
// Terminating Condition
if ($position == $length)
return 1;
$res = 0;
// sum of digits
$sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for ($i = $position; $i < $length; $i++)
{
$sum += ($num[$i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if ($sum >= $previous_sum)
// Note : We pass current
// sum as previous sum
$res += countGroups($i + 1, $sum,
$length, $num);
}
// Total number of subgroups
// till current position
return $res;
}
// Driver Code
$num = "1119";
$len = strlen($num);
echo countGroups(0, 0, $len, $num);
// This code is contributed by ajit
?>
If we take a closer look at the above recursive solution, we notice that there may be overlapping subproblems. For example, if the input number is 12345, then for position = 3 and previous_sum = 3, we recur two times. Similarly, for position 4 and previous_sum = 7, we recur two times. Therefore the above solution can be optimized using Dynamic Programming. Below is a Dynamic Programming based solution for this problem.
- The maximum sum of digits can be 9*length where 'length' is length of input num.
- Create a 2D array int dp[MAX][9*MAX] where MAX is maximum possible length of input number. A value dp[position][previous] is going to store result for 'position' and 'previous_sum'.
- If current subproblem has been evaluated i.e; dp[position][previous_sum] != -1, then use this result, else recursively compute its value.
- If by including the current position digit in sum i.e; sum = sum + num[position]-'0', sum becomes greater than equal to previous sum, then increment the result and call the problem for next position in the num.
- If position == length, then we have been traversed current subgroup successfully and we return 1;
Below is the implementation of the above algorithm.
C++
// C++ program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal
// to its immediate right subgroup.
#include<bits/stdc++.h>
using namespace std;
// Maximum length of
// input number string
const int MAX = 40;
// A memoization table to store
// results of subproblems length
// of string is 40 and maximum
// sum will be 9 * 40 = 360.
int dp[MAX][9*MAX + 1];
// Function to find the count
// of splits with given condition
int countGroups(int position,
int previous_sum,
int length, char *num)
{
// Terminating Condition
if (position == length)
return 1;
// If already evaluated for
// a given sub problem then
// return the value
if (dp[position][previous_sum] != -1)
return dp[position][previous_sum];
// countGroups for current
// sub-group is 0
dp[position][previous_sum] = 0;
int res = 0;
// sum of digits
int sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (int i = position; i < length; i++)
{
sum += (num[i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
dp[position][previous_sum] = res;
// total number of subgroups
// till current position
return res;
}
// Driver Code
int main()
{
char num[] = "1119";
int len = strlen(num);
// Initialize dp table
memset(dp, -1, sizeof(dp));
cout << countGroups(0, 0, len, num);
return 0;
}
Java
// Java program to count the number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal
// to its immediate right subgroup.
class GFG
{
// Maximum length of
// input number string
static int MAX = 40;
// A memoization table to store
// results of subproblems length
// of string is 40 and maximum
// sum will be 9 * 40 = 360.
static int dp[][] = new int[MAX][9 * MAX + 1];
// Function to find the count
// of splits with given condition
static int countGroups(int position,
int previous_sum,
int length, char []num)
{
// Terminating Condition
if (position == length)
return 1;
// If already evaluated for
// a given sub problem then
// return the value
if (dp[position][previous_sum] != -1)
return dp[position][previous_sum];
// countGroups for current
// sub-group is 0
dp[position][previous_sum] = 0;
int res = 0;
// sum of digits
int sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (int i = position; i < length; i++)
{
sum += (num[i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
dp[position][previous_sum] = res;
// total number of subgroups
// till current position
return res;
}
// Driver Code
public static void main(String[] args)
{
char num[] = "1119".toCharArray();
int len = num.length;
// Initialize dp table
for(int i = 0; i < dp.length; i++)
{
for(int j = 0;j < 9 * MAX + 1; j++){
dp[i][j] = -1;
}
}
System.out.println(countGroups(0, 0, len, num));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to count the number of
# ways to group digits of a number
# such that sum of digits in every
# subgroup is less than or equal
# to its immediate right subgroup.
# Maximum length of
# input number string
MAX = 40
# A memoization table to store
# results of subproblems length
# of string is 40 and maximum
# sum will be 9 * 40 = 360.
dp = [[ -1 for i in range(9 * MAX + 1)]
for i in range(MAX)]
# Function to find the count
# of splits with given condition
def countGroups(position, previous_sum,
length, num):
# Terminating Condition
if (position == length):
return 1
# If already evaluated for
# a given sub problem then
# return the value
if (dp[position][previous_sum] != -1):
return dp[position][previous_sum]
# countGroups for current
# sub-group is 0
dp[position][previous_sum] = 0
res = 0
# sum of digits
sum = 0
# Traverse all digits from
# current position to rest
# of the length of string
for i in range(position,length):
sum += (ord(num[i]) - ord('0'))
# If forward_sum is greater
# than the previous sum,
# then call the method again
if (sum >= previous_sum):
# Note : We pass current
# sum as previous sum
res += countGroups(i + 1, sum,
length, num)
dp[position][previous_sum] = res
# total number of subgroups
# till the current position
return res
# Driver Code
num = "1119"
len = len(num)
print(countGroups(0, 0, len, num))
# This code is contributed by Mohit Kumar
C#
// C# program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal
// to its immediate right subgroup.
using System;
class GFG
{
// Maximum length of
// input number string
static int MAX = 40;
// A memoization table to store
// results of subproblems length
// of string is 40 and maximum
// sum will be 9 * 40 = 360.
static int[,] dp = new int[MAX, 9 * MAX + 1];
// Function to find the count
// of splits with given condition
static int countGroups(int position,
int previous_sum,
int length, char[] num)
{
// Terminating Condition
if (position == length)
return 1;
// If already evaluated for
// a given sub problem then
// return the value
if (dp[position,previous_sum] != -1)
return dp[position,previous_sum];
// countGroups for current
// sub-group is 0
dp[position,previous_sum] = 0;
int res = 0;
// sum of digits
int sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (int i = position; i < length; i++)
{
sum += (num[i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
dp[position,previous_sum] = res;
// total number of subgroups
// till current position
return res;
}
// Driver Code
static void Main()
{
char[] num = {'1', '1', '1', '9'};
int len = num.Length;
// Initialize dp table
for(int i = 0; i < MAX; i++)
for(int j = 0; j < 9 * MAX + 1; j++)
dp[i, j] = -1;
Console.Write(countGroups(0, 0, len, num));
}
}
// This code is contributed by DrRoot_
JavaScript
<script>
// Javascript program to count the number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal
// to its immediate right subgroup.
// Maximum length of
// input number string
let MAX = 40;
// A memoization table to store
// results of subproblems length
// of string is 40 and maximum
// sum will be 9 * 40 = 360.
let dp=new Array(MAX);
// Function to find the count
// of splits with given condition
function countGroups( position,previous_sum,length,num)
{
// Terminating Condition
if (position == length)
return 1;
// If already evaluated for
// a given sub problem then
// return the value
if (dp[position][previous_sum] != -1)
return dp[position][previous_sum];
// countGroups for current
// sub-group is 0
dp[position][previous_sum] = 0;
let res = 0;
// sum of digits
let sum = 0;
// Traverse all digits from
// current position to rest
// of the length of string
for (let i = position; i < length; i++)
{
sum += (num[i] - '0');
// If forward_sum is greater
// than the previous sum,
// then call the method again
if (sum >= previous_sum)
// Note : We pass current
// sum as previous sum
res += countGroups(i + 1, sum,
length, num);
}
dp[position][previous_sum] = res;
// total number of subgroups
// till current position
return res;
}
// Driver Code
let num = "1119".split("");
let len = num.length;
// Initialize dp table
for(let i = 0; i < dp.length; i++)
{
dp[i]=new Array(9 * MAX + 1)
for(let j = 0;j < 9 * MAX + 1; j++){
dp[i][j] = -1;
}
}
document.write(countGroups(0, 0, len, num));
// This code is contributed by unknown2108
</script>
Using DP Tabulation
- Compute the sum of digits in the input string.
- Initialize a 2D table of size (length+1)x(x+1) to store the results of the sub-problems:
- Set all values in the last row of the table to 1, since there is only one way to obtain any target value from the digits in the last position of the string.
- Fill the table in a bottom-up manner, starting from the second-last position of the string and working backwards.
- For each position in the string and each possible target value, compute the number of sub-strings that have a sum of digits equal to the target value.
- Finally, return the value in the top-left corner of the table, which represents the number of sub-strings of the entire string with a sum of digits equal to 0.
Below is the implementation of the above algorithm.
C++
// C++ program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal to
// its immediate right subgroup.
#include <bits/stdc++.h>
using namespace std;
int countGroups(string num)
{
// Compute the sum of digits in the input string
int length = num.length();
int x = 0;
for (int i = 0; i < length; i++) {
x += num[i] - '0';
}
// Initialize a 2D table of size (length+1)x(x+1)
vector<vector<int> > dp(length + 1,
vector<int>(x + 1, 0));
// Base case: set all values in the last row to 1
for (int s = 0; s <= x; s++) {
dp[length][s] = 1;
}
// Fill the table in bottom-up manner
for (int position = length - 1; position >= 0;
position--) {
for (int previous_sum = 0; previous_sum <= x;
previous_sum++) {
int sum = 0;
int res = 0;
// Compute the number of sub-strings with sum >=
// previous_sum
for (int i = position; i < length; i++) {
sum += (num[i] - '0');
if (sum >= previous_sum) {
res += dp[i + 1][sum];
}
}
// Store the result in the current position of
// the table
dp[position][previous_sum] = res;
}
}
// Return the value in the top-left corner of the table
return dp[0][0];
}
// Driver Code
int main()
{
string str = "1119";
cout << "number of groupings : " << countGroups(str);
return 0;
}
//this is contributed by RAVI SINGH
Java
// Java program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal to
// its immediate right subgroup.
import java.util.*;
public class Main {
public static int countGroups(String num) {
// Compute the sum of digits in the input string
int length = num.length();
int x = 0;
for (int i = 0; i < length; i++) {
x += num.charAt(i) - '0';
}
// Initialize a 2D table of size (length+1)x(x+1)
int[][] dp = new int[length + 1][x + 1];
// Base case: set all values in the last row to 1
for (int s = 0; s <= x; s++) {
dp[length][s] = 1;
}
// Fill the table in bottom-up manner
for (int position = length - 1; position >= 0; position--) {
for (int previous_sum = 0; previous_sum <= x; previous_sum++) {
int sum = 0;
int res = 0;
// Compute the number of sub-strings with sum >=
// previous_sum
for (int i = position; i < length; i++) {
sum += (num.charAt(i) - '0');
if (sum >= previous_sum) {
res += dp[i + 1][sum];
}
}
// Store the result in the current position of
// the table
dp[position][previous_sum] = res;
}
}
// Return the value in the top-left corner of the table
return dp[0][0];
}
// Driver Code
public static void main(String[] args) {
String str = "1119";
System.out.println("number of groupings : " + countGroups(str));
}
}
//this is contributed by RAVI SINGH
Python3
def countGroups(num):
# Compute the sum of digits in the input string
x = sum(int(c) for c in num)
# Initialize a 2D table of size (length+1)x(x+1)
dp = [[0] * (x + 1) for _ in range(len(num) + 1)]
# Base case: set all values in the last row to 1
for s in range(x + 1):
dp[-1][s] = 1
# Fill the table in bottom-up manner
for position in range(len(num) - 1, -1, -1):
for previous_sum in range(x + 1):
current_sum = 0
res = 0
# Compute the number of sub-strings with sum >= previous_sum
for i in range(position, len(num)):
current_sum += int(num[i])
if current_sum >= previous_sum:
res += dp[i + 1][current_sum]
# Store the result in the current position of the table
dp[position][previous_sum] = res
# Return the value in the top-left corner of the table
return dp[0][0]
# Driver Code
if __name__ == "__main__":
num = "1119"
print("number of groupings:", countGroups(num))
C#
using System;
public class GFG {
public static int CountGroups(string num) {
// Compute the sum of digits in the input string
int length = num.Length;
int x = 0;
for (int i = 0; i < length; i++) {
x += num[i] - '0';
}
// Initialize a 2D table of size (length+1)x(x+1)
int[,] dp = new int[length + 1, x + 1];
// Base case: set all values in the last row to 1
for (int s = 0; s <= x; s++) {
dp[length, s] = 1;
}
// Fill the table in bottom-up manner
for (int position = length - 1; position >= 0; position--) {
for (int previous_sum = 0; previous_sum <= x; previous_sum++) {
int sum = 0;
int res = 0;
// Compute the number of sub-strings with sum >= previous_sum
for (int i = position; i < length; i++) {
sum += (num[i] - '0');
if (sum >= previous_sum) {
res += dp[i + 1, sum];
}
}
// Store the result in the current position of the table
dp[position, previous_sum] = res;
}
}
// Return the value in the top-left corner of the table
return dp[0, 0];
}
// Driver Code
public static void Main(string[] args) {
string str = "1119";
Console.WriteLine("number of groupings: " + CountGroups(str));
}
}
JavaScript
// Javascript program to count number of
// ways to group digits of a number
// such that sum of digits in every
// subgroup is less than or equal to
// its immediate right subgroup.
function countGroups(num) {
// Compute the sum of digits in the input string
let x = 0;
for (let i = 0; i < num.length; i++) {
x += parseInt(num[i]);
}
// Initialize a 2D array of size (length+1)x(x+1)
let dp = new Array(num.length + 1).fill(0).map(() => new Array(x + 1).fill(0));
// Base case: set all values in the last row to 1
for (let s = 0; s <= x; s++) {
dp[num.length][s] = 1;
}
// Fill the table in bottom-up manner
for (let position = num.length - 1; position >= 0; position--) {
for (let previous_sum = 0; previous_sum <= x; previous_sum++) {
let sum = 0;
let res = 0;
// Compute the number of sub-strings with
// sum >= previous_sum
for (let i = position; i < num.length; i++) {
sum += parseInt(num[i]);
if (sum >= previous_sum) {
res += dp[i + 1][sum];
}
}
// Store the result in the
// current position of the table
dp[position][previous_sum] = res;
}
}
// Return the value in the
// top-left corner of the table
return dp[0][0];
}
// Driver Code
let str = "1119";
console.log("Number of groupings:", countGroups(str));
Outputnumber of groupings : 7
Time Complexity: O(length*x^2) where x is the sum of its digits.
Space Complexity: O(length*x)
This article is reviewed by team GeeksForGeeks.
Count digit groupings of a number | DSA 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