Given an n x n square matrix, find sum of all sub-squares of size k x k
Last Updated :
12 Dec, 2022
Given an n x n square matrix, find sum of all sub-squares of size k x k where k is smaller than or equal to n.
Examples :
Input:
n = 5, k = 3
arr[][] = { {1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{3, 3, 3, 3, 3},
{4, 4, 4, 4, 4},
{5, 5, 5, 5, 5},
};
Output:
18 18 18
27 27 27
36 36 36
Input:
n = 3, k = 2
arr[][] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
Output:
12 16
24 28
A Simple Solution is to one by one pick starting point (leftmost-topmost corner) of all possible sub-squares. Once the starting point is picked, calculate sum of sub-square starting with the picked starting point.
Following is the implementation of this idea.
C++
// A simple C++ program to find sum of all subsquares of
// size k x k
#include <iostream>
using namespace std;
// Size of given matrix
#define n 5
// A simple function to find sum of all sub-squares of size
// k x k in a given square matrix of size n x n
void printSumSimple(int mat[][n], int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// row number of first cell in current sub-square of
// size k x k
for (int i = 0; i < n - k + 1; i++) {
// column of first cell in current sub-square of
// size k x k
for (int j = 0; j < n - k + 1; j++) {
// Calculate and print sum of current sub-square
int sum = 0;
for (int p = i; p < k + i; p++)
for (int q = j; q < k + j; q++)
sum += mat[p][q];
cout << sum << " ";
}
// Line separator for sub-squares starting with next
// row
cout << endl;
}
}
// Driver program to test above function
int main()
{
int mat[n][n] = {
{ 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5 },
};
int k = 3;
printSumSimple(mat, k);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
C
// A simple C program to find sum of all subsquares of
// size k x k
#include <stdio.h>
// Size of given matrix
#define n 5
// A simple function to find sum of all sub-squares of size
// k x k in a given square matrix of size n x n
void printSumSimple(int mat[][n], int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// row number of first cell in current sub-square of
// size k x k
for (int i = 0; i < n - k + 1; i++) {
// column of first cell in current sub-square of
// size k x k
for (int j = 0; j < n - k + 1; j++) {
// Calculate and print sum of current sub-square
int sum = 0;
for (int p = i; p < k + i; p++)
for (int q = j; q < k + j; q++)
sum += mat[p][q];
printf("%d ", sum);
}
// Line separator for sub-squares starting with next
// row
printf("\n");
}
}
// Driver program to test above function
int main()
{
int mat[n][n] = {
{ 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5 },
};
int k = 3;
printSumSimple(mat, k);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
Java
// A simple Java program to find sum of all
// subsquares of size k x k
import java.io.*;
class GFG {
// Size of given matrix
static final int n = 5;
// A simple function to find sum of all
// sub-squares of size k x k in a given
// square matrix of size n x n
static void printSumSimple(int mat[][], int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// row number of first cell in current sub-square of
// size k x k
for (int i = 0; i < n - k + 1; i++) {
// column of first cell in current sub-square of
// size k x k
for (int j = 0; j < n - k + 1; j++) {
// Calculate and print sum of current
// sub-square
int sum = 0;
for (int p = i; p < k + i; p++)
for (int q = j; q < k + j; q++)
sum += mat[p][q];
System.out.print(sum + " ");
}
// Line separator for sub-squares starting with
// next row
System.out.println();
}
}
// Driver Program to test above function
public static void main(String arg[])
{
int mat[][] = { { 1, 1, 1, 1, 1 },
{ 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3 },
{ 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5 } };
int k = 3;
printSumSimple(mat, k);
}
}
// This code is contributed by Aditya Kumar (adityakumar129)
Python3
# A simple Python 3 program to find sum
# of all subsquares of size k x k
# Size of given matrix
n = 5
# A simple function to find sum of all
# sub-squares of size k x k in a given
# square matrix of size n x n
def printSumSimple(mat, k):
# k must be smaller than or equal to n
if (k > n):
return
# row number of first cell in current
# sub-square of size k x k
for i in range(n - k + 1):
# column of first cell in current
# sub-square of size k x k
for j in range(n - k + 1):
# Calculate and print sum of
# current sub-square
sum = 0
for p in range(i, k + i):
for q in range(j, k + j):
sum += mat[p][q]
print(sum, end = " ")
# Line separator for sub-squares
# starting with next row
print()
# Driver Code
if __name__ == "__main__":
mat = [[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5]]
k = 3
printSumSimple(mat, k)
# This code is contributed by ita_c
C#
// A simple C# program to find sum of all
// subsquares of size k x k
using System;
class GFG
{
// Size of given matrix
static int n = 5;
// A simple function to find sum of all
//sub-squares of size k x k in a given
// square matrix of size n x n
static void printSumSimple(int [,]mat, int k)
{
// k must be smaller than or
// equal to n
if (k > n) return;
// row number of first cell in
// current sub-square of size k x k
for (int i = 0; i < n-k+1; i++)
{
// column of first cell in current
// sub-square of size k x k
for (int j = 0; j < n-k+1; j++)
{
// Calculate and print sum of
// current sub-square
int sum = 0;
for (int p = i; p < k+i; p++)
for (int q = j; q < k+j; q++)
sum += mat[p,q];
Console.Write(sum+ " ");
}
// Line separator for sub-squares
// starting with next row
Console.WriteLine();
}
}
// Driver Program to test above function
public static void Main()
{
int [,]mat = {{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{3, 3, 3, 3, 3},
{4, 4, 4, 4, 4},
{5, 5, 5, 5, 5}};
int k = 3;
printSumSimple(mat, k);
}
}
// This code is contributed by Sam007
PHP
<?php
// A simple PHP program to find
// sum of all subsquares of size
// k x k
// Size of given matrix
$n = 5;
// function to find sum of all sub -
// squares of size k x k in a given
// square matrix of size n x n
function printSumSimple( $mat, $k)
{
global $n;
// k must be smaller than
// or equal to n
if ($k > $n) return;
// row number of first cell in
// current sub-square of size
// k x k
for($i = 0; $i < $n - $k + 1; $i++)
{
// column of first cell in
// current sub-square of size
// k x k
for($j = 0; $j < $n - $k + 1; $j++)
{
// Calculate and print sum of
// current sub-square
$sum = 0;
for ($p = $i; $p < $k + $i; $p++)
for ($q = $j; $q < $k + $j; $q++)
$sum += $mat[$p][$q];
echo $sum , " ";
}
// Line separator for sub-squares
// starting with next row
echo "\n";
}
}
// Driver Code
$mat = array(array(1, 1, 1, 1, 1),
array(2, 2, 2, 2, 2,),
array(3, 3, 3, 3, 3,),
array(4, 4, 4, 4, 4,),
array(5, 5, 5, 5, 5));
$k = 3;
printSumSimple($mat, $k);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// A simple Javascript program to find sum of all
// subsquares of size k x k
// Size of given matrix
let n = 5;
// A simple function to find sum of all
//sub-squares of size k x k in a given
// square matrix of size n x n
function printSumSimple(mat,k)
{
// k must be smaller than or
// equal to n
if (k > n) return;
// row number of first cell in
// current sub-square of size k x k
for (let i = 0; i < n-k+1; i++)
{
// column of first cell in current
// sub-square of size k x k
for (let j = 0; j < n-k+1; j++)
{
// Calculate and print sum of
// current sub-square
let sum = 0;
for (let p = i; p < k+i; p++)
for (let q = j; q < k+j; q++)
sum += mat[p][q];
document.write(sum+ " ");
}
// Line separator for sub-squares
// starting with next row
document.write("<br>");
}
}
// Driver Program to test above function
let mat=[[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5]]
let k = 3;
printSumSimple(mat, k);
// This code is contributed by avanitrachhadiya2155
</script>
Output18 18 18
27 27 27
36 36 36
Time complexity: O(k2n2).
Auxiliary Space: O(1)
We can solve this problem in O(n2) time using a Tricky Solution.
The idea is to preprocess the given square matrix. In the preprocessing step, calculate sum of all vertical strips of size k x 1 in a temporary square matrix stripSum[][]. Once we have sum of all vertical strips, we can calculate sum of first sub-square in a row as sum of first k strips in that row, and for remaining sub-squares, we can calculate sum in O(1) time by removing the leftmost strip of previous subsquare and adding the rightmost strip of new square.
Following is the implementation of this idea.
C++
// An efficient C++ program to find sum of all subsquares of
// size k x k
#include <iostream>
using namespace std;
// Size of given matrix
#define n 5
// A O(n^2) function to find sum of all sub-squares of size
// k x k in a given square matrix of size n x n
void printSumTricky(int mat[][n], int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// 1: PREPROCESSING
// To store sums of all strips of size k x 1
int stripSum[n][n];
// Go column by column
for (int j = 0; j < n; j++) {
// Calculate sum of first k x 1 rectangle in this
// column
int sum = 0;
for (int i = 0; i < k; i++)
sum += mat[i][j];
stripSum[0][j] = sum;
// Calculate sum of remaining rectangles
for (int i = 1; i < n - k + 1; i++) {
sum += (mat[i + k - 1][j] - mat[i - 1][j]);
stripSum[i][j] = sum;
}
}
// 2: CALCULATE SUM of Sub-Squares using stripSum[][]
for (int i = 0; i < n - k + 1; i++) {
// Calculate and print sum of first subsquare in
// this row
int sum = 0;
for (int j = 0; j < k; j++)
sum += stripSum[i][j];
cout << sum << " ";
// Calculate sum of remaining squares in current row
// by removing the leftmost strip of previous
// sub-square and adding a new strip
for (int j = 1; j < n - k + 1; j++) {
sum += (stripSum[i][j + k - 1]
- stripSum[i][j - 1]);
cout << sum << " ";
}
cout << endl;
}
}
// Driver program to test above function
int main()
{
int mat[n][n] = {
{ 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5 },
};
int k = 3;
printSumTricky(mat, k);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
C
// An efficient C program to find sum of all subsquares of
// size k x k
#include <stdio.h>
// Size of given matrix
#define n 5
// A O(n^2) function to find sum of all sub-squares of size
// k x k in a given square matrix of size n x n
void printSumTricky(int mat[][n], int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// 1: PREPROCESSING
// To store sums of all strips of size k x 1
int stripSum[n][n];
// Go column by column
for (int j = 0; j < n; j++) {
// Calculate sum of first k x 1 rectangle in this
// column
int sum = 0;
for (int i = 0; i < k; i++)
sum += mat[i][j];
stripSum[0][j] = sum;
// Calculate sum of remaining rectangles
for (int i = 1; i < n - k + 1; i++) {
sum += (mat[i + k - 1][j] - mat[i - 1][j]);
stripSum[i][j] = sum;
}
}
// 2: CALCULATE SUM of Sub-Squares using stripSum[][]
for (int i = 0; i < n - k + 1; i++) {
// Calculate and print sum of first subsquare in
// this row
int sum = 0;
for (int j = 0; j < k; j++)
sum += stripSum[i][j];
printf("%d ", sum);
// Calculate sum of remaining squares in current row
// by removing the leftmost strip of previous
// sub-square and adding a new strip
for (int j = 1; j < n - k + 1; j++) {
sum += (stripSum[i][j + k - 1]
- stripSum[i][j - 1]);
printf("%d ", sum);
}
printf("\n");
}
}
// Driver program to test above function
int main()
{
int mat[n][n] = {
{ 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5 },
};
int k = 3;
printSumTricky(mat, k);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
Java
// An efficient Java program to find sum of all subsquares
// of size k x k
import java.io.*;
class GFG {
// Size of given matrix
static int n = 5;
// A O(n^2) function to find sum of all sub-squares of
// size k x k in a given square matrix of size n x n
static void printSumTricky(int mat[][], int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// 1: PREPROCESSING
// To store sums of all strips of size k x 1
int stripSum[][] = new int[n][n];
// Go column by column
for (int j = 0; j < n; j++) {
// Calculate sum of first k x 1 rectangle in
// this column
int sum = 0;
for (int i = 0; i < k; i++)
sum += mat[i][j];
stripSum[0][j] = sum;
// Calculate sum of remaining rectangles
for (int i = 1; i < n - k + 1; i++) {
sum += (mat[i + k - 1][j] - mat[i - 1][j]);
stripSum[i][j] = sum;
}
}
// 2: CALCULATE SUM of Sub-Squares
// using stripSum[][]
for (int i = 0; i < n - k + 1; i++) {
// Calculate and print sum of first
// subsquare in this row
int sum = 0;
for (int j = 0; j < k; j++)
sum += stripSum[i][j];
System.out.print(sum + " ");
// Calculate sum of remaining squares in current
// row by removing the leftmost strip of
// previous sub-square and adding a new strip
for (int j = 1; j < n - k + 1; j++) {
sum += (stripSum[i][j + k - 1]
- stripSum[i][j - 1]);
System.out.print(sum + " ");
}
System.out.println();
}
}
// Driver program to test above function
public static void main(String[] args)
{
int mat[][] = {
{ 1, 1, 1, 1, 1 }, { 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3 }, { 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5 },
};
int k = 3;
printSumTricky(mat, k);
}
}
// This code is contributed by Aditya Kumar (adityakumar129)
Python3
# An efficient Python3 program to find sum
# of all subsquares of size k x k
# A O(n^2) function to find sum of all
# sub-squares of size k x k in a given
# square matrix of size n x n
def printSumTricky(mat, k):
global n
# k must be smaller than or
# equal to n
if k > n:
return
# 1: PREPROCESSING
# To store sums of all strips of size k x 1
stripSum = [[None] * n for i in range(n)]
# Go column by column
for j in range(n):
# Calculate sum of first k x 1
# rectangle in this column
Sum = 0
for i in range(k):
Sum += mat[i][j]
stripSum[0][j] = Sum
# Calculate sum of remaining rectangles
for i in range(1, n - k + 1):
Sum += (mat[i + k - 1][j] -
mat[i - 1][j])
stripSum[i][j] = Sum
# 2: CALCULATE SUM of Sub-Squares
# using stripSum[][]
for i in range(n - k + 1):
# Calculate and print sum of first
# subsquare in this row
Sum = 0
for j in range(k):
Sum += stripSum[i][j]
print(Sum, end = " ")
# Calculate sum of remaining squares
# in current row by removing the leftmost
# strip of previous sub-square and adding
# a new strip
for j in range(1, n - k + 1):
Sum += (stripSum[i][j + k - 1] -
stripSum[i][j - 1])
print(Sum, end = " ")
print()
# Driver Code
n = 5
mat = [[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5]]
k = 3
printSumTricky(mat, k)
# This code is contributed by PranchalK
C#
// An efficient C# program to find
// sum of all subsquares of size k x k
using System;
class GFG {
// Size of given matrix
static int n = 5;
// A O(n^2) function to find sum of all
// sub-squares of size k x k in a given
// square matrix of size n x n
static void printSumTricky(int [,]mat, int k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// 1: PREPROCESSING
// To store sums of all strips of
// size k x 1
int [,]stripSum = new int[n,n];
// Go column by column
for (int j = 0; j < n; j++)
{
// Calculate sum of first k x 1
// rectangle in this column
int sum = 0;
for (int i = 0; i < k; i++)
sum += mat[i,j];
stripSum[0,j] = sum;
// Calculate sum of remaining
// rectangles
for (int i = 1; i < n - k + 1; i++)
{
sum += (mat[i + k - 1,j]
- mat[i - 1,j]);
stripSum[i,j] = sum;
}
}
// 2: CALCULATE SUM of Sub-Squares
// using stripSum[][]
for (int i = 0; i < n - k + 1; i++)
{
// Calculate and print sum of first
// subsquare in this row
int sum = 0;
for (int j = 0; j < k; j++)
sum += stripSum[i,j];
Console.Write(sum + " ");
// Calculate sum of remaining
// squares in current row by
// removing the leftmost strip
// of previous sub-square
// and adding a new strip
for (int j = 1; j < n - k + 1; j++)
{
sum += (stripSum[i,j + k - 1]
- stripSum[i,j - 1]);
Console.Write(sum + " ");
}
Console.WriteLine();
}
}
// Driver program to test above function
public static void Main()
{
int [,]mat = {{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{3, 3, 3, 3, 3},
{4, 4, 4, 4, 4},
{5, 5, 5, 5, 5},
};
int k = 3;
printSumTricky(mat, k);
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// An efficient PHP program to
// find sum of all subsquares
// of size k x k
// Size of given matrix
$n = 5;
// A O(n^2) function to find
// sum of all sub-squares of
// size k x k in a given
// square matrix of size n x n
function printSumTricky($mat, $k)
{
global $n;
// k must be smaller
// than or equal to n
if ($k > $n) return;
// 1: PREPROCESSING
// To store sums of all
// strips of size k x 1
$stripSum = array(array());
// Go column by column
for ($j = 0; $j < $n; $j++)
{
// Calculate sum of first
// k x 1 rectangle in this column
$sum = 0;
for ($i = 0; $i < $k; $i++)
$sum += $mat[$i][$j];
$stripSum[0][$j] = $sum;
// Calculate sum of
// remaining rectangles
for ($i = 1; $i < $n - $k + 1; $i++)
{
$sum += ($mat[$i + $k - 1][$j] -
$mat[$i - 1][$j]);
$stripSum[$i][$j] = $sum;
}
}
// 2: CALCULATE SUM of
// Sub-Squares using stripSum[][]
for ($i = 0; $i < $n - $k + 1; $i++)
{
// Calculate and print sum of
// first subsquare in this row
$sum = 0;
for ($j = 0; $j < $k; $j++)
$sum += $stripSum[$i][$j];
echo $sum , " ";
// Calculate sum of remaining
// squares in current row by
// removing the leftmost strip
// of previous sub-square and
// adding a new strip
for ($j = 1; $j < $n - $k + 1; $j++)
{
$sum += ($stripSum[$i][$j + $k - 1] -
$stripSum[$i][$j - 1]);
echo $sum , " ";
}
echo "\n";
}
}
// Driver Code
$mat = array(array(1, 1, 1, 1, 1),
array(2, 2, 2, 2, 2),
array(3, 3, 3, 3, 3),
array(4, 4, 4, 4, 4),
array(5, 5, 5, 5, 5));
$k = 3;
printSumTricky($mat, $k);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// An efficient Javascript program to find
// sum of all subsquares of size k x k
// Size of given matrix
let n = 5;
// A O(n^2) function to find sum of all
// sub-squares of size k x k in a given
// square matrix of size n x n
function printSumTricky(mat, k)
{
// k must be smaller than or equal to n
if (k > n)
return;
// 1: PREPROCESSING
// To store sums of all strips of size k x 1
let stripSum = new Array(n);
for(let i = 0; i < n; i++)
{
stripSum[i] = new Array(n);
}
for(let i = 0; i < n; i++)
{
for(let j = 0; j < n; j++)
{
stripSum[i][j] = 0;
}
}
// Go column by column
for(let j = 0; j < n; j++)
{
// Calculate sum of first k x 1
// rectangle in this column
let sum = 0;
for(let i = 0; i < k; i++)
sum += mat[i][j];
stripSum[0][j] = sum;
// Calculate sum of remaining rectangles
for(let i = 1; i < n - k + 1; i++)
{
sum += (mat[i + k - 1][j] - mat[i - 1][j]);
stripSum[i][j] = sum;
}
}
// 2: CALCULATE SUM of Sub-Squares
// using stripSum[][]
for(let i = 0; i < n - k + 1; i++)
{
// Calculate and print sum of first
// subsquare in this row
let sum = 0;
for (let j = 0; j < k; j++)
sum += stripSum[i][j];
document.write(sum + " ");
// Calculate sum of remaining squares
// in current row by removing the
// leftmost strip of previous sub-square
// and adding a new strip
for(let j = 1; j < n - k + 1; j++)
{
sum += (stripSum[i][j + k - 1] -
stripSum[i][j - 1]);
document.write(sum + " ");
}
document.write("<br>");
}
}
// Driver code
let mat = [ [ 1, 1, 1, 1, 1 ],
[ 2, 2, 2, 2, 2 ],
[ 3, 3, 3, 3, 3 ],
[ 4, 4, 4, 4, 4 ],
[ 5, 5, 5, 5, 5 ] ];
let k = 3;
printSumTricky(mat, k);
// This code is contributed by rag2127
</script>
Output18 18 18
27 27 27
36 36 36
Time complexity: O(n2).
Auxiliary Space: O(n2).
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 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