Skip to main content

๐Ÿ“‰ Printing Patterns

One-line summary: Master nested loops and logical thinking through visual pattern printing โ€” the foundation for understanding 2D array manipulation and algorithmic problem-solving.


Conceptโ€‹

Pattern printing problems help develop:

  • Nested loop control โ€” understanding row-column relationships
  • Mathematical thinking โ€” finding formulas for positions and values
  • Logical reasoning โ€” breaking complex patterns into simple rules
  • 2D visualization โ€” essential for matrix and grid problems

Common patterns: stars, numbers, alphabets, geometric shapes, mathematical sequences.


Common Techniquesโ€‹

Basic Triangle Patternโ€‹

function printTriangle(n) {
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '* ';
}
console.log(row);
}
}

Number Pyramidโ€‹

function numberPyramid(n) {
for (let i = 1; i <= n; i++) {
let spaces = ' '.repeat(n - i);
let numbers = '';
for (let j = 1; j <= i; j++) {
numbers += j + ' ';
}
console.log(spaces + numbers);
}
}

Diamond Patternโ€‹

function diamond(n) {
// Upper half
for (let i = 1; i <= n; i++) {
console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
// Lower half
for (let i = n - 1; i >= 1; i--) {
console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
}

Practice Problemsโ€‹

Basic Patterns (Easy)โ€‹

ProblemDifficultySolution

| Concentric Rectangle | Easy | View Solution | | Print Floyd's Triangle | Easy | View Solution | | Print Matrix Diagonal | Easy | View Solution | | Print Rectangle Pattern | Easy | View Solution | | Print X in Box | Easy | View Solution | | Star Triangle Pattern | Easy | | | Right Triangle Pattern | Easy | | | Inverted Triangle | Easy | | | Hollow Rectangle | Easy | | | Square Pattern | Easy | | | Number Triangle | Easy | | | Alphabet Triangle | Easy | | | Plus Pattern | Easy | | | Cross Pattern | Easy | | | Simple Diamond | Easy | | | Arrow Pattern | Easy | | | Checkerboard Pattern | Easy | | | Binary Pattern | Easy | | | Multiplication Table Pattern | Easy | | | Even-Odd Pattern | Easy | |

Intermediate Patterns (Medium)โ€‹

ProblemDifficultySolution
Number PyramidMedium
Diamond PatternMedium
Pascal's TriangleMedium
Butterfly PatternMedium
Zigzag PatternMedium
Spiral MatrixMedium
Heart PatternMedium
Number DiamondMedium
Alphabet PatternMedium
Hourglass PatternMedium
Rhombus PatternMedium
Wave PatternMedium
Hollow DiamondMedium
Fibonacci TriangleMedium
Prime Number PatternMedium
Matrix SpiralMedium
Sandglass PatternMedium
Hexagon PatternMedium
Star DiamondMedium
Number SpiralMedium

Advanced Patterns (Hard)โ€‹

ProblemDifficultySolution
Complex Fractal PatternHard
Sierpinski TriangleHard
Matrix Rotation PatternHard
Multi-layer DiamondHard
3D Cube PatternHard
Mandala PatternHard
Celtic Knot PatternHard
Maze PatternHard
Tessellation PatternHard
Golden Ratio SpiralHard

CodeChef Pattern Problemsโ€‹

ProblemDifficultySolution
CC โ€” Pattern Printing (PATTERN)Easy
CC โ€” Star Pattern (STARPTR)Easy
CC โ€” Number Pattern (NUMPTR)Medium
CC โ€” Triangle Pattern (TRIPTR)Medium
CC โ€” Diamond Design (DIAMPTR)Medium
CC โ€” Complex Pattern (COMPLXPTR)Hard

Tips for Pattern Problemsโ€‹

  1. Analyze the pattern first โ€” identify rows, columns, and relationships
  2. Find the mathematical formula โ€” express position in terms of row/column indices
  3. Handle spaces carefully โ€” leading spaces often determine alignment
  4. Test with small inputs โ€” verify logic with n=3 or n=4 before scaling
  5. Break complex patterns โ€” divide into simpler sub-patterns

  • Matrix โ€” 2D array manipulation
  • Math โ€” mathematical sequences and formulas
  • Recursion โ€” recursive pattern generation
  • School Basics โ€” fundamental programming concepts

โ† Back to Home ยท ยฉ sparshjaswal