Header Ads Widget

Responsive Advertisement

Frequency Weaving Logic & Spiral Printing of a Rectangle

What is the purpose of Rectangle Spiral Printing?

When we create a weave of frequency then we use this logic or principal, we saw different type of frequency in different manner. Now this is the one of the manners of frequency and Spiral Printing of Rectangle.

 

Java Implementation of Spiral Order Printing using iteration

To print a rectangular matrix in a spiral order, you need to start from the outermost elements and move inward, following the spiral pattern. Below is the Java implementation that prints a matrix in a spiral order:

java

public class RectangleSpiralPrint {

 

    public static void main(String[] args) {

        int[][] matrix = {

            { 1, 2, 3, 4 },

            { 5, 6, 7, 8 },

            { 9, 10, 11, 12 },

            { 13, 14, 15, 16 }

        };

 

        System.out.println("Spiral Order of the Matrix:");

        printSpiral(matrix);

    }

 

    public static void printSpiral(int[][] matrix) {

        int top = 0, bottom = matrix.length - 1;

        int left = 0, right = matrix[0].length - 1;

 

        while (top <= bottom && left <= right) {

            // Print top row

            for (int i = left; i <= right; i++) {

                System.out.print(matrix[top][i] + " ");

            }

            top++;

 

            // Print right column

            for (int i = top; i <= bottom; i++) {

                System.out.print(matrix[i][right] + " ");

            }

            right--;

 

            // Print bottom row if any

            if (top <= bottom) {

                for (int i = right; i >= left; i--) {

                    System.out.print(matrix[bottom][i] + " ");

                }

                bottom--;

            }

 

            // Print left column if any

            if (left <= right) {

                for (int i = bottom; i >= top; i--) {

                    System.out.print(matrix[i][left] + " ");

                }

                left++;

            }

        }

    }

}

 

Explanation

  1. Initialization:
    • We start by defining four boundaries: top, bottom, left, and right.
    • These boundaries will help us determine which elements of the matrix to print as we traverse in a spiral order.
  2. Spiral Traversal:
    • Top Row: Start from the left boundary and move to the right boundary along the top row. After printing the top row, move the top boundary one row down.
    • Right Column: From the updated top boundary, move down along the right column until the bottom boundary. After printing the right column, move the right boundary one column to the left.
    • Bottom Row: If there are any rows left between the top and bottom boundaries, print the bottom row from right to left. After printing the bottom row, move the bottom boundary one row up.
    • Left Column: If there are any columns left between the left and right boundaries, print the left column from bottom to top. After printing the left column, move the left boundary one column to the right.
  3. Termination:
    • The process continues until the top boundary exceeds the bottom boundary or the left boundary exceeds the right boundary.

 

Java Implementation of Spiral Order Printing using recursive

Printing a rectangular matrix in spiral order using recursion requires breaking down the problem into smaller sub-problems. The key idea is to print the boundary elements (top row, right column, bottom row, and left column) and then recursively call the function to handle the sub-matrix that remains.

java

public class RectangleSpiralPrintRecursive {

 

    public static void main(String[] args) {

        int[][] matrix = {

            { 1, 2, 3, 4 },

            { 5, 6, 7, 8 },

            { 9, 10, 11, 12 },

            { 13, 14, 15, 16 }

        };

 

        System.out.println("Spiral Order of the Matrix:");

        printSpiral(matrix, 0, 0, matrix.length, matrix[0].length);

    }

 

    // Recursive method to print the matrix in spiral order

    public static void printSpiral(int[][] matrix, int startRow, int startCol, int endRow, int endCol) {

        // Base case: if there's nothing left to print

        if (startRow >= endRow || startCol >= endCol) {

            return;

        }

 

        // Print the top row from the remaining rows

        for (int i = startCol; i < endCol; i++) {

            System.out.print(matrix[startRow][i] + " ");

        }

 

        // Print the last column from the remaining columns

        for (int i = startRow + 1; i < endRow; i++) {

            System.out.print(matrix[i][endCol - 1] + " ");

        }

 

        // Print the bottom row from the remaining rows, if there's more than one row

        if ((endRow - 1) != startRow) {

            for (int i = endCol - 2; i >= startCol; i--) {

                System.out.print(matrix[endRow - 1][i] + " ");

            }

        }

 

        // Print the first column from the remaining columns, if there's more than one column

        if ((endCol - 1) != startCol) {

            for (int i = endRow - 2; i > startRow; i--) {

                System.out.print(matrix[i][startCol] + " ");

            }

        }

 

        // Recur for the sub-matrix

        printSpiral(matrix, startRow + 1, startCol + 1, endRow - 1, endCol - 1);

    }

}

 

Explanation

  1. Base Case:
    • The recursion terminates when the starting row (startRow) exceeds or equals the ending row (endRow), or the starting column (startCol) exceeds or equals the ending column (endCol). This happens when there are no more elements to print.
  2. Top Row:
    • Print the elements in the top row of the current sub-matrix from startCol to endCol - 1.
  3. Right Column:
    • Print the elements in the rightmost column of the current sub-matrix from startRow + 1 to endRow - 1.
  4. Bottom Row:
    • If the top row and the bottom row are different (i.e., the matrix has more than one row), print the elements in the bottom row of the current sub-matrix from endCol - 2 to startCol.
  5. Left Column:
    • If the left column and the right column are different (i.e., the matrix has more than one column), print the elements in the leftmost column of the current sub-matrix from endRow - 2 to startRow + 1.
  6. Recursive Call:
    • After printing the boundary elements, the function calls itself recursively to print the inner sub-matrix by incrementing the starting row and column indices by 1 and decrementing the ending row and column indices by 1.

 

 

Both Example Output

For the input matrix:

1  2  3  4

5  6  7  8

9 10 11 12

13 14 15 16

 

The output will be:

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

 

Summary

This code efficiently prints a matrix in a spiral order by updating the boundaries after each step of the traversal. This approach works for any rectangular matrix, regardless of its size, making it a versatile solution for spiral matrix traversal.

 

  


 

"Frequency Weaving Logic in Java" refers to a concept or algorithm where you interlace or weave different frequencies or values in a particular pattern or order. Although it's not a standard term in computer science, it could be used in contexts such as signal processing, data patterns, or custom algorithms that require handling multiple frequencies or repetitive values.

Example Concepts Where Frequency Weaving Logic Might Apply:

  1. Signal Processing: Combining multiple signals of different frequencies into a single signal while preserving their distinct characteristics.
  2. Data Structures: Creating a data structure that interlaces elements from multiple lists or arrays based on certain frequency patterns.
  3. Algorithm Design: Designing an algorithm that processes or organizes data based on the frequency of occurrence in a specific pattern.

Implementing a Basic Concept in Java:

Here’s a simple example in Java that might resemble frequency weaving, where you interleave elements from two arrays:

java

public class FrequencyWeaving {

    public static int[] weaveArrays(int[] array1, int[] array2) {

        int[] wovenArray = new int[array1.length + array2.length];

        int index = 0, i = 0, j = 0;

 

        while (i < array1.length && j < array2.length) {

            wovenArray[index++] = array1[i++];

            wovenArray[index++] = array2[j++];

        }

 

        // If array1 has more elements

        while (i < array1.length) {

            wovenArray[index++] = array1[i++];

        }

 

        // If array2 has more elements

        while (j < array2.length) {

            wovenArray[index++] = array2[j++];

        }

 

        return wovenArray;

    }

 

    public static void main(String[] args) {

        int[] array1 = {1, 3, 5};

        int[] array2 = {2, 4, 6, 8, 10};

 

        int[] result = weaveArrays(array1, array2);

 

        for (int num : result) {

            System.out.print(num + " ");

        }

    }

}

 

Explanation:

Ø  Weaving Logic: In this example, the elements from array1 and array2 are interleaved (woven) into a single array. The result is an array where elements from the first and second arrays alternate.

Ø  Extension: This basic logic can be expanded to weave more complex data types, frequencies, or even to handle specific patterns based on given requirements.

 


Spiral Printing of Rectangle
Spiral Printing of Rectangle




Post a Comment

0 Comments