4.4 Nested Iteration

How to iterate through with a time complexity of O(n^2)

for (int i = 1; i <= 3; i++) { // Outer loop
    System.out.println("Outer loop iteration: " + i);
    for (int j = 1; j <= 3; j++) { // Inner loop
        System.out.println("    Inner loop iteration: " + j);
        }
    }
Outer loop iteration: 1
    Inner loop iteration: 1
    Inner loop iteration: 2
    Inner loop iteration: 3
Outer loop iteration: 2
    Inner loop iteration: 1
    Inner loop iteration: 2
    Inner loop iteration: 3
Outer loop iteration: 3
    Inner loop iteration: 1
    Inner loop iteration: 2
    Inner loop iteration: 3

What is wrong with this code cell(Hack)

//Hint: Check the Syntax and look at the equals to signs on the example above

import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
for (int i = rows; i>1; i-) {
    for (int j = 1; j <= i; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
    }
        
scanner.close();
    


Enter the number of rows: 

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Sample input: 5

Sample Output 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

Screenshot 2024-09-19 at 20 45 04

Answer here + One sentence Explanation

Screenshot 2024-09-29 at 15 54 38

Answer here + One sentence Explanation

Video To watch later if you need more help

Cool Usecase of nested loops


    // Define the dimensions
    int rows = 5;

    // First loop to generate a diamond pattern
    System.out.println("Diamond Pattern:");
    for (int i = 1; i <= rows; i++) {
        // Print spaces for left alignment
        for (int j = i; j < rows; j++) {
            System.out.print(" ");
        }
        // Print asterisks for the upper part of the diamond
        for (int k = 1; k <= (2 * i - 1); k++) {
            System.out.print("*");
        }
        System.out.println();
    }
    for (int i = rows - 1; i >= 1; i--) {
        // Print spaces for right alignment
        for (int j = rows; j > i; j--) {
            System.out.print(" ");
        }
        // Print asterisks for the lower part of the diamond
        for (int k = 1; k <= (2 * i - 1); k++) {
            System.out.print("*");
        }
        System.out.println();
    }

    // Second loop: Magic Square (Latin Square)
    System.out.println("\nMagic Square (Latin Square):");
    int size = 4;
    int[][] magicSquare = new int[size][size];
    int num = 1, row = 0, col = size / 2;

    while (num <= size * size) {
        magicSquare[row][col] = num;
        num++;
        int newRow = (row - 1 + size) % size;
        int newCol = (col + 1) % size;

        if (magicSquare[newRow][newCol] != 0) {
            row = (row + 1) % size;
        } else {
            row = newRow;
            col = newCol;
        }
    }

    // Print the magic square
    for (int[] r : magicSquare) {
        for (int c : r) {
            System.out.print(c + "\t");
        }
        System.out.println();
    }
    
    // Third loop: Prime Number Spiral
    System.out.println("\nPrime Number Spiral:");
    int spiralSize = 5;
    int[][] spiral = new int[spiralSize][spiralSize];
    int val = 1, startRow = 0, endRow = spiralSize - 1, startCol = 0, endCol = spiralSize - 1;

    while (startRow <= endRow && startCol <= endCol) {
        // Fill top row
        for (int i = startCol; i <= endCol; i++) {
            spiral[startRow][i] = isPrime(val) ? val : 0;
            val++;
        }
        startRow++;

        // Fill right column
        for (int i = startRow; i <= endRow; i++) {
            spiral[i][endCol] = isPrime(val) ? val : 0;
            val++;
        }
        endCol--;

        // Fill bottom row
        if (startRow <= endRow) {
            for (int i = endCol; i >= startCol; i--) {
                spiral[endRow][i] = isPrime(val) ? val : 0;
                val++;
            }
            endRow--;
        }

        // Fill left column
        if (startCol <= endCol) {
            for (int i = endRow; i >= startRow; i--) {
                spiral[i][startCol] = isPrime(val) ? val : 0;
                val++;
            }
            startCol++;
        }
    }

    // Print the spiral
    for (int[] r : spiral) {
        for (int c : r) {
            System.out.print(c + "\t");
        }
        System.out.println();
    }


// Method to check if a number is prime
static boolean isPrime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i <= Math.sqrt(num); i++) {
        if (num % i == 0) return false;
    }
    return true;
}

Diamond Pattern:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Magic Square (Latin Square):
9	15	1	7	
14	4	6	12	
3	5	11	13	
8	10	16	2	

Prime Number Spiral:
0	2	3	0	5	
0	17	0	19	0	
0	0	0	0	7	
0	23	0	0	0	
13	0	11	0	0	
2
15


Iteration: 0

Current Velocity: 2, 2