Matrix Rotation Techniques
Matrix rotation involves reorganizing elements in a square grid through geometric transformations. The fundamental approach combines transposition with selective reversal operations.
Clockwise 90-Degree Rotation
The process involves two sequential transformations: first transpose the matrix, then reverse each row horizontally.
public static void rotateClockwise(int[][] grid) {
int dimension = grid.length;
// Perform matrix transposition
for (int row = 0; row < dimension; row++) {
for (int col = row + 1; col < dimension; col++) {
int swapTemp = grid[row][col];
grid[row][col] = grid[col][row];
grid[col][row] = swapTemp;
}
}
// Reverse each row
for (int row = 0; row < dimension; row++) {
int left = 0;
int right = dimension - 1;
while (left < right) {
int swapTemp = grid[row][left];
grid[row][left] = grid[row][right];
grid[row][right] = swapTemp;
left++;
right--;
}
}
}
Counter-Clockwise 90-Degree Rotation
This variation follows the same transposition step but reverses each column vertically instead of reversing rows.
public static void rotateCounterClockwise(int[][] grid) {
int dimension = grid.length;
// Perform matrix transposition
for (int row = 0; row < dimension; row++) {
for (int col = row + 1; col < dimension; col++) {
int swapTemp = grid[row][col];
grid[row][col] = grid[col][row];
grid[col][row] = swapTemp;
}
}
// Reverse each column
for (int col = 0; col < dimension; col++) {
int top = 0;
int bottom = dimension - 1;
while (top < bottom) {
int swapTemp = grid[top][col];
grid[top][col] = grid[bottom][col];
grid[bottom][col] = swapTemp;
top++;
bottom--;
}
}
}
Pattern Recognition and Analysis
Identifying patterns in grid structures requires systematic traversal and observation of element relationships, value distributions, and positional characteristics.
Extreme Value Dteection
Locating maximum and minimum values through comprehensive iteration:
public static void findExtremes(int[][] data) {
int highest = Integer.MIN_VALUE;
int lowest = Integer.MAX_VALUE;
for (int[] row : data) {
for (int value : row) {
if (value > highest) highest = value;
if (value < lowest) lowest = value;
}
}
System.out.println("Maximum: " + highest);
System.out.println("Minimum: " + lowest);
}
Target Element Search
Determining presence and location of specific values:
public static boolean containsElement(int[][] data, int target) {
for (int r = 0; r < data.length; r++) {
for (int c = 0; c < data[r].length; c++) {
if (data[r][c] == target) {
System.out.printf("Found at position (%d, %d)%n", r, c);
return true;
}
}
}
return false;
}
Aggregation Operations
Computing sums across different dimensions:
Row-wise Summation
public static void calculateRowSums(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
int sum = 0;
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
System.out.println("Row " + i + " sum: " + sum);
}
}
Column-wise Summation
public static void calculateColumnSums(int[][] matrix) {
if (matrix.length == 0) return;
for (int col = 0; col < matrix[0].length; col++) {
int sum = 0;
for (int row = 0; row < matrix.length; row++) {
sum += matrix[row][col];
}
System.out.println("Column " + col + " sum: " + sum);
}
}
Diagonal Summation
public static void computeDiagonalSums(int[][] square) {
int primary = 0, secondary = 0;
int size = square.length;
for (int i = 0; i < size; i++) {
primary += square[i][i];
secondary += square[i][size - 1 - i];
}
System.out.println("Primary diagonal: " + primary);
System.out.println("Secondary diagonal: " + secondary);
}
Spiral Traversal Algorithm
Extracting elements in a clockwise spiral pattern using boundary tracking:
public static void spiralOrderTraversal(int[][] grid) {
int topBound = 0;
int bottomBound = grid.length - 1;
int leftBound = 0;
int rightBound = grid[0].length - 1;
while (topBound <= bottomBound && leftBound <= rightBound) {
// Traverse top boundary left to right
for (int col = leftBound; col <= rightBound; col++) {
System.out.print(grid[topBound][col] + " ");
}
topBound++;
// Traverse right boundary top to bottom
for (int row = topBound; row <= bottomBound; row++) {
System.out.print(grid[row][rightBound] + " ");
}
rightBound--;
// Traverse bottom boundary right to left
if (topBound <= bottomBound) {
for (int col = rightBound; col >= leftBound; col--) {
System.out.print(grid[bottomBound][col] + " ");
}
bottomBound--;
}
// Traverse left boundary bottom to top
if (leftBound <= rightBound) {
for (int row = bottomBound; row >= topBound; row--) {
System.out.print(grid[row][leftBound] + " ");
}
leftBound++;
}
}
}
Element Frequency Analysis
Counting occurrences of specific values:
public static int countOccurrences(int[][] arr, int target) {
int frequency = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target) {
frequency++;
}
}
}
return frequency;
}
Pascal's Triangle Implementation
Pascal's Triangle is a triangular array where each number equals the sum of the two directly above it. The structure follows combinatorial properties with natural symmetry.
public class PascalTriangleGenerator {
public static int[][] generate(int rows) {
int[][] triangle = new int[rows][];
for (int i = 0; i < rows; i++) {
triangle[i] = new int[i + 1];
triangle[i][0] = triangle[i][i] = 1; // First and last elements are 1
// Calculate interior values
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
return triangle;
}
public static void printTriangle(int[][] triangle) {
for (int[] row : triangle) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] result = generate(5);
printTriangle(result);
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1