Program to print hollow pyramid, diamond pattern and their modifications
Last Updated :
27 Mar, 2023
For Prerequisite : Loops, If Else Statement
1. Hollow pyramid/triangle pattern
The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row.
Examples:
Input: n=6
Output:
#
# #
# #
# #
# #
# #
###########
C++14
// CPP program to print a hollow pyramid pattern
#include <iostream>
using namespace std;
void printPattern(int);
int main()
{
int n = 6;
printPattern(n);
}
void printPattern(int n)
{
int i, j, k = 0;
for (i = 1; i <= n; i++) // row=6
{
// Print spaces
for (j = i; j < n; j++) {
cout << " ";
}
// Print #
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
cout << "#";
else
cout << " ";
k++;
}
k = 0;
cout << endl; // print next row
}
// print last row
for (i = 0; i < 2 * n - 1; i++) {
cout << "#";
}
}
// this article is contributed by Shivani Ghughtyal
Java
// JAVA program to print a hollow
// pyramid pattern
class GFG{
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
static void printPattern(int n)
{
int i, j, k = 0;
for (i = 1; i <= n; i++) // row=6
{
// Print spaces
for (j = i; j < n; j++) {
System.out.print(" ");
}
// Print #
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
System.out.print("#");
else
System.out.print(" ");
k++;
;
}
k = 0;
// print next row
System.out.println();
}
// print last row
for (i = 0; i < 2 * n - 1; i++) {
System.out.print("#");
}
}
}
/*This code is contributed by Nikita Tiwari.*/
Python
# Python program to print a hollow
# pyramid pattern
def printPattern( n) :
k = 0
for i in range(1,n+1) : #row 6
# Print spaces
for j in range(i,n) :
print(' ', end='')
# Print #
while (k != (2 * i - 1)) :
if (k == 0 or k == 2 * i - 2) :
print('#', end='')
else :
print(' ', end ='')
k = k + 1
k = 0;
print ("") # print next row
# print last row
for i in range(0, 2 * n -1) :
print ('#', end = '')
# Driver code
n = 6
printPattern(n)
# This code is contributed by Nikita Tiwari.
C#
using System;
public class GFG{
public static void Main()
{
int n = 6;
printPattern(n);
}
static void printPattern(int n)
{
int i, j, k = 0;
for (i = 1; i <= n; i++) // row=6
{
// Print spaces
for (j = i; j < n; j++) {
Console.Write(" ");
}
// Print #
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
Console.Write("#");
else
Console.Write(" ");
k++;
;
}
k = 0;
// print next row
Console.WriteLine();
}
// print last row
for (i = 0; i < 2 * n - 1; i++) {
Console.Write("#");
}
}
}
// This code is contributed by laxmigangarajula03
PHP
<?php
// php program to print a
// hollow pyramid pattern
function printPattern($n)
{
$k = 0;
// row=6
for ($i = 1; $i <= $n; $i++)
{
// Print spaces
for ($j = $i; $j < $n; $j++)
{
echo " ";
}
// Print #
while ($k != (2 * $i - 1))
{
if ($k == 0 || $k == 2 *
$i - 2)
echo "#";
else
echo " ";
$k++;
}
$k = 0;
// print next row
echo "\n";
}
// print last row
for ($i = 0; $i < 2 * $n - 1; $i++)
{
echo "#";
}
}
//Driver Code
$n = 6;
printPattern($n);
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript program to print a hollow pyramid pattern
var n = 6;
printPattern(n);
function printPattern(n)
{
var i, j, k = 0;
for (i = 1; i <= n; i++)// row=6
{
// Print spaces
for (j = i; j < n; j++)
{
document.write(" ");
}
// Print #
while (k != 2 * i - 1)
{
if (k == 0 || k == 2 * i - 2)
document.write("#");
else
document.write(" ");
k++;
}
k = 0;
document.write("<br>"); // print next row
}
// print last row
for (i = 0; i < 2 * n - 1; i++)
{
document.write("#");
}
}
// This code is contributed by rdtank.
</script>
Output #
# #
# #
# #
# #
# #
###########
Time complexity: O(N^2),This algorithm has a time complexity of O(N^2), where N is the number of rows. This is because we are looping through the rows and columns of the pattern, which takes O(N^2) time to complete.
Space complexity: O(1),This algorithm does not require any additional space, so its space complexity is O(1).
2. Hollow Diamond
Note: For even input, print the pattern for n-1.
Example:
Input: 1
Output:
For n=1
Input: 7
Output:
For n=7
Input: 9
Output:
For n=9
Approach: To print diamond we need to print spaces before star and after the star to achieve constant increasing distance of stars.
To print the box shape we need to print '-' for i==1 (first row) & i==n (last row) and '|' for j==1 (first column) and j==n (last column).
Algorithm: 1. If n is odd increment n.
2. Find mid=n/2.
3. Traverse from 1 to mid to print upper half of the pattern (say i).
4. Traverse from 1 to mid-i to print spaces for upper left most outer box (say j).
5. If (i==1) print '*' (since for first row we need only one star).
6. else print '*' and traverse from 1 to 2*i-3 to print spaces for hollow diamond (say j) and print '*' after loop is over.
7. Traverse from 1 to mid-i to print spaces again for upper right most outer box (say j).
8. Close the loop at step 3.
9. Traverse from mid+1 to n-1 to print lower half of the pattern (say i).
4. Traverse from 1 to i-mid to print spaces for lower left most outer box (say j).
5. If (i==n-1) print '*' (since for last row we need only one star).
6. else print '*' and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond (say j) and print '*' after loop is over.
7. Traverse from 1 to i-mid to print spaces again for lower right most outer box (say j).
8. Close the loop at step 9.
C++14
#include <bits/stdc++.h>
using namespace std;
// function to print the pattern
void printPattern(int& n)
{
int i,j,mid;
if(n%2==1) //when n is odd, increase it by 1 to make it even
n++;
mid = n/2;
// upper half pattern
for(i = 1; i<= mid; i++) {
for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star
cout<<" ";
if(i == 1) {
cout << "*";
}else{
cout << "*"; //in each line star at start and end position
for(j = 1; j<=2*i-3; j++) { //print space to make hollow
cout << " ";
}
cout << "*";
}
for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star
cout<<" ";
cout << endl;
}
// lower half pattern
for(i = mid+1; i<n; i++) {
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star
cout<<" ";
if(i == n-1) {
cout << "*";
}else{
cout << "*"; //in each line star at start and end position
for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
cout << " ";
}
cout << "*";
}
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
cout<<" ";
cout << endl;
}
}
// driver's code
int main() {
int n=7;
printPattern(n);
}
// this code is contributed by prophet1999
Java
// JAVA program
class GFG{
// function to print the pattern
static void printPattern(int n)
{
int i,j,mid;
if(n%2==1) //when n is odd, increase it by 1 to make it even
n++;
mid = n/2;
// upper half pattern
for(i = 1; i<= mid; i++) {
for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star
System.out.print(" ");
if(i == 1) {
System.out.print("*");
}else{
System.out.print("*"); //in each line star at start and end position
for(j = 1; j<=2*i-3; j++) { //print space to make hollow
System.out.print(" ");
}
System.out.print("*");
}
for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star
System.out.print(" ");
System.out.println();
}
// lower half pattern
for(i = mid+1; i<n; i++) {
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star
System.out.print(" ");
if(i == n-1) {
System.out.print("*");
}else{
System.out.print("*"); //in each line star at start and end position
for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
System.out.print(" ");
}
System.out.print("*");
}
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
System.out.print(" ");
System.out.println();
}
}
// driver's code
public static void main(String args[])
{
int n = 7;
printPattern(n);
}
}
// This code is contributed by Pushpesh Raj.
C#
// C# program
using System;
public class GFG{
// function to print the pattern
public static void printPattern(int n)
{
int i, j, mid;
if(n % 2 == 1) //when n is odd, increase it by 1 to make it even
n++;
mid = n/2;
// upper half pattern
for(i = 1; i <= mid; i++) {
for(j = 1; j <= mid - i; j++) //print the blank spaces and outer box before star
Console.Write(" ");
if(i == 1) {
Console.Write("*");
}else{
Console.Write("*"); //in each line star at start and end position
for(j = 1; j <= 2 * i - 3; j++) { //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for(j = 1; j <= mid - i; j++) //print the blank spaces and outer box after star
Console.Write(" ");
Console.WriteLine();
}
// lower half pattern
for(i = mid + 1; i < n; i++) {
for(j = 1; j <= i - mid; j++) //print the blank spaces and outer box before star
Console.Write(" ");
if(i == n - 1) {
Console.Write("*");
}else{
Console.Write("*"); //in each line star at start and end position
for(j = 1; j <= 2*(n - i)-3; j++) { //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
Console.Write(" ");
Console.WriteLine();
}
}
// driver's code
public static void Main()
{
int n = 7;
printPattern(n);
}
}
// This code is contributed by Aman Kumar.
JavaScript
// function to print the pattern
function printPattern( n)
{
let i,j,mid;
if(n % 2 == 1) //when n is odd, increase it by 1 to make it even
n++;
mid = n/2;
// upper half pattern
for(i = 1; i <= mid; i++) {
for(j = 1; j <= mid - i; j++) //print the blank spaces and outer box before star
console.log(" ");
if(i == 1) {
console.log("*");
}else{
console.log("*"); //in each line star at start and end position
for(j = 1; j<=2*i-3; j++) { //print space to make hollow
console.log(" ");
}
console.log("*");
}
for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star
console.log(" ");
console.log("<br>");
}
// lower half pattern
for(i = mid+1; i<n; i++) {
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star
console.log(" ");
if(i == n-1) {
console.log("*");
}else{
console.log("*"); //in each line star at start and end position
for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
console.log(" ");
}
console.log("*");
}
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
console.log(" ");
console.log("<br>");
}
}
// driver's code
let n=7;
printPattern(n);
Python3
# Python program
class GFG:
# function to print the pattern
@staticmethod
def printPattern(n):
i, j, mid = 0, 0, 0
if n % 2 == 1: #when n is odd, increase it by 1 to make it even
n += 1
mid = n // 2
# upper half pattern
for i in range(1, mid+1):
for j in range(1, mid-i+1): #print the blank spaces and outer box before star
print(" ", end="")
if i == 1:
print("*", end="")
else:
print("*", end="") #in each line star at start and end position
for j in range(1, 2*i-3+1): #print space to make hollow
print(" ", end="")
print("*", end="")
for j in range(1, mid-i+1): #print the blank spaces and outer box after star
print(" ", end="")
print()
# lower half pattern
for i in range(mid+1, n):
for j in range(1, i-mid+1): #print the blank spaces and outer box before star
print(" ", end="")
if i == n-1:
print("*", end="")
else:
print("*", end="") #in each line star at start and end position
for j in range(1, 2*(n-i)-3+1): #print space to make hollow
print(" ", end="")
print("*", end="")
for j in range(1, i-mid+1): #print the blank spaces and outer box after star
print(" ", end="")
print()
# driver's code
if __name__ == '__main__':
n = 7
GFG.printPattern(n)
# This code is contributed by sharmashivam215.
Output *
* *
* *
* *
* *
* *
*
Time Complexity: O(n^2) for given input n
Auxiliary Space: O(1)
3. Hollow Diamond bounded inside a rectangular box made of horizontal and vertical dashes(-).
Write a program to Print hollow diamond pattern bound inside a box made of dash(-) and bitwise-OR(|) as shown below.
Note: For even input, print the pattern for n-1.
Example:
Input: 1
Output:
For n=1
Input: 7
Output:
For n=7
Input: 9
Output:
For n=9
Approach: To print diamond we need to print spaces before star and after the star to achieve constant increasing distance of stars.
To print the box shape we need to print '-' for i==1 (first row) & i==n (last row) and '|' for j==1 (first column) and j==n (last column).
Algorithm: 1. If n is odd increment n.
2. Find mid=n/2.
3. Traverse from 1 to mid to print upper half of the pattern (say i).
4. Traverse from 1 to mid-i to print upper left most outer box (say j).
5. If (i==1) print '*' (since for first row we need only one star).
6. else print '*' and traverse from 1 to 2*i-3 to print spaces for hollow diamond (say j) and print '*' after loop is over.
7. Traverse from 1 to mid-i to print upper right most outer box (say j).
8. Close the loop at step 3.
9. Traverse from mid+1 to n-1 to print lower half of the pattern (say i).
4. Traverse from 1 to i-mid to print lower left most outer box (say j).
5. If (i==n-1) print '*' (since for last row we need only one star).
6. else print '*' and traverse from 1 to 2*(n-i)-3 to print spaces for hollow diamond (say j) and print '*' after loop is over.
7. Traverse from 1 to i-mid to print lower right most outer box (say j).
8. Close the loop at step 9.
C++14
#include <bits/stdc++.h>
using namespace std;
// function to print the pattern
void printPattern(int& n)
{
int i,j,mid;
if(n%2==1) //when n is odd, increase it by 1 to make it even
n++;
mid = n/2;
// upper half pattern
for(i = 1; i<= mid; i++) {
for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star
if(i==1)
cout<<"-";
else if(j==1)
cout << "|";
else cout<<" ";
}
if(i == 1) {
cout << "*";
}else{
cout << "*"; //in each line star at start and end position
for(j = 1; j<=2*i-3; j++) { //print space to make hollow
cout << " ";
}
cout << "*";
}
for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star
if(i==1)
cout<<"-";
else if(j==mid-i)
cout << "|";
else cout<<" ";
}
cout << endl;
}
// lower half pattern
for(i = mid+1; i<n; i++) {
for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star
if(i==n-1)
cout<<"-";
else if(j==1)
cout << "|";
else cout<<" ";
}
if(i == n-1) {
cout << "*";
}else{
cout << "*"; //in each line star at start and end position
for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
cout << " ";
}
cout << "*";
}
for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star
if(i==n-1)
cout<<"-";
else if(j==i-mid)
cout << "|";
else cout<<" ";
}
cout << endl;
}
}
// driver's code
int main() {
int n=12;
printPattern(n);
}
// this code is contributed by prophet1999
Java
// Java code to implement the above approach
import java.io.*;
import java.util.*;
class GFG
{
// function to print the pattern
public static void printPattern(int n)
{
int i,j,mid;
if(n%2==1) //when n is odd, increase it by 1 to make it even
n++;
mid = n/2;
// upper half pattern
for(i = 1; i<= mid; i++) {
for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star
if(i==1)
System.out.print("-");
else if(j==1)
System.out.print("|");
else
System.out.print(" ");
}
if(i == 1) {
System.out.print("*");
}
else{
System.out.print("*"); //in each line star at start and end position
for(j = 1; j<=2*i-3; j++) { //print space to make hollow
System.out.print(" ");
}
System.out.print("*");
}
for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star
if(i==1)
System.out.print("-");
else if(j==mid-i)
System.out.print("|");
else System.out.print(" ");
}
System.out.print("\n");
}
// lower half pattern
for(i = mid+1; i<n; i++) {
for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star
if(i==n-1)
System.out.print("-");
else if(j==1)
System.out.print("|");
else System.out.print(" ");
}
if(i == n-1) {
System.out.print("*");
}else{
System.out.print("*"); //in each line star at start and end position
for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
System.out.print(" ");
}
System.out.print("*");
}
for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star
if(i==n-1)
System.out.print("-");
else if(j==i-mid)
System.out.print("|");
else System.out.print(" ");
}
System.out.print("\n");
}
}
// Driver Code
public static void main(String[] args)
{
int n=12;
printPattern(n);
}
}
//this code is contributed by adityapatil12
Python3
# function to print the pattern
def printPattern(n):
if n % 2 == 1: # when n is odd, increase it by 1 to make it even
n += 1
mid = n // 2
# upper half pattern
for i in range(1, mid+1):
for j in range(1, mid-i+1): # print the blank spaces and outer box before star
if i == 1:
print("-", end="")
elif j == 1:
print("|", end="")
else:
print(" ", end="")
if i == 1:
print("*", end="")
else:
print("*", end="") # in each line star at start and end position
for j in range(1, 2*i-2): # print space to make hollow
print(" ", end="")
print("*", end="")
for j in range(1, mid-i+1): # print the blank spaces and outer box after star
if i == 1:
print("-", end="")
elif j == mid-i:
print("|", end="")
else:
print(" ", end="")
print()
# lower half pattern
for i in range(mid+1, n):
for j in range(1, i-mid+1): # print the blank spaces and outer box before star
if i == n-1:
print("-", end="")
elif j == 1:
print("|", end="")
else:
print(" ", end="")
if i == n-1:
print("*", end="")
else:
print("*", end="") # in each line star at start and end position
for j in range(1, 2*(n-i)-2): # print space to make hollow
print(" ", end="")
print("*", end="")
for j in range(1, i-mid+1): # print the blank spaces and outer box after star
if i == n-1:
print("-", end="")
elif j == i-mid:
print("|", end="")
else:
print(" ", end="")
print()
# driver's code
n = 12
printPattern(n)
# This code is contributed by prasad264
C#
using System;
namespace Pattern
{
class Program
{
static void PrintPattern(ref int n)
{
int i, j, mid;
if (n % 2 == 1) //when n is odd, increase it by 1 to make it even
n++;
mid = n / 2;
// upper half pattern
for (i = 1; i <= mid; i++)
{
for (j = 1; j <= mid - i; j++)
{ //print the blank spaces and outer box before star
if (i == 1)
Console.Write("-");
else if (j == 1)
Console.Write("|");
else Console.Write(" ");
}
if (i == 1)
{
Console.Write("*");
}
else
{
Console.Write("*"); //in each line star at start and end position
for (j = 1; j <= 2 * i - 3; j++)
{ //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for (j = 1; j <= mid - i; j++)
{ //print the blank spaces and outer box after star
if (i == 1)
Console.Write("-");
else if (j == mid - i)
Console.Write("|");
else Console.Write(" ");
}
Console.WriteLine();
}
// lower half pattern
for (i = mid + 1; i < n; i++)
{
for (j = 1; j <= i - mid; j++)
{ //print the blank spaces and outer box before star
if (i == n - 1)
Console.Write("-");
else if (j == 1)
Console.Write("|");
else Console.Write(" ");
}
if (i == n - 1)
{
Console.Write("*");
}
else
{
Console.Write("*"); //in each line star at start and end position
for (j = 1; j <= 2 * (n - i) - 3; j++)
{ //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for (j = 1; j <= i - mid; j++)
{ //print the blank spaces and outer box after star
if (i == n - 1)
Console.Write("-");
else if (j == i - mid)
Console.Write("|");
else Console.Write(" ");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
int n = 12;
PrintPattern(ref n);
}
}
}
// This code is contributed by factworx412
JavaScript
// JavaScript code to implement the above approach
// function to print the pattern
function printPattern(n) {
let i, j, mid;
if (n % 2 === 1) { // when n is odd, increase it by 1 to make it even
n++;
}
mid = n / 2;
// upper half pattern
for (i = 1; i <= mid; i++) {
let line = "";
for (j = 1; j <= mid - i; j++) { // print the blank spaces and outer box before star
if (i === 1) {
line += "-";
}
else if (j === 1) {
line += "|";
}
else {
line += " ";
}
}
if (i === 1) {
line += "*";
}
else {
line += "*"; // in each line star at start and end position
for (j = 1; j <= 2 * i - 3; j++) { // print space to make hollow
line += " ";
}
line += "*";
}
for (j = 1; j <= mid - i; j++) { // print the blank spaces and outer box after star
if (i === 1) {
line += "-";
}
else if (j === mid - i) {
line += "|";
}
else {
line += " ";
}
}
console.log(line);
}
// lower half pattern
for (i = mid + 1; i < n; i++) {
let line = "";
for (j = 1; j <= i - mid; j++) { // print the blank spaces and outer box before star
if (i === n - 1) {
line += "-";
}
else if (j === 1) {
line += "|";
}
else {
line += " ";
}
}
if (i === n - 1) {
line += "*";
}
else {
line += "*"; // in each line star at start and end position
for (j = 1; j <= 2 * (n - i) - 3; j++) { // print space to make hollow
line += " ";
}
line += "*";
}
for (j = 1; j <= i - mid; j++) { // print the blank spaces and outer box after star
if (i === n - 1) {
line += "-";
}
else if (j === i - mid) {
line += "|";
}
else {
line += " ";
}
}
console.log(line);
}
}
// Driver Code
let n = 12;
printPattern(n);
Output-----*-----
| * * |
| * * |
| * * |
|* *|
* *
|* *|
| * * |
| * * |
| * * |
-----*-----
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Java Program to Print Hollow Diamond Star Pattern
Java Program to Print Hollow Diamond Star Pattern
CPP Program to Print Hollow Diamond Star Pattern
Python Program to Print Stars in Hollow Diamond Shape
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