Program to create Pascal's Triangle — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Program to create Pascal's Triangle
Detailed explanation and reference materials
Problem Overview

Constructing Pascal's Triangle

Problem Description

Pascal's Triangle is a triangular array of numbers where:

  1. The first and last elements of each row are 1.
  2. Each intermediate element is the sum of the two numbers directly above it.

Given an integer numRows, construct Pascal's Triangle as a 2D array with exactly numRows rows. Each row should only contain the elements that belong to the triangle (no trailing zeros).


Objective

To construct a 2-dimensional array that represents Pascal's Triangle.


Steps

1. Initialize the 2D Array

  • Create a 2-dimensional array pascalTriangle of size numRows x numRows initialized with 0s.

2. Declare and Initialize Variables

  • Declare a variable cols and initialize it to 0. This variable will keep track of the number of columns in the current row.

3. Construct Pascal's Triangle

  • Use a for loop to iterate through each row (variable i from 0 to numRows - 1):
    • Increment cols by 1 for each new row.
    • Use a nested for loop to iterate through each column in the current row (variable j from 0 to cols - 1):
      • If j is 0 or j is cols - 1, assign the value 1 to pascalTriangle[i][j]. This handles the edges of the triangle.
      • Otherwise, set pascalTriangle[i][j] to the sum of the two numbers directly above it in the triangle (pascalTriangle[i-1][j-1] and pascalTriangle[i-1][j]).

4. Remove Extra Zeros

  • After constructing the triangle, remove all trailing zeros from each row to ensure the triangle has the correct shape.

Example Input and Output

Example 1

Input:
numRows = 5

Output:

json
[
  [1],
  [1, 1],
  [1, 2, 1],
  [1, 3, 3, 1],
  [1, 4, 6, 4, 1]
]
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
array
striver's-sde-sheet
java
Reference Link
https://leetcode.com/problems/pascals-triangle/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.