Matrix


  1. Boolean matrix

    Given a boolean matrix, modify it so that if a cell contains 1, all cells in its row and column are set to 1. Mark row i and column j to 1 if matrix[i][j] = 1.

  2. Print boundary elements of a matrix

    Given an n x m matrix, write an algorithm to print all the elements that lie on its outer edges. These include the elements in the first row, the last row, the first column, and the last column of the matrix.

  3. Find common elements in all rows in one iteration

    Design an algorithm that efficiently identifies all elements that appear in every row of a given m x n matrix. The algorithm must have a time complexity of O(mn) and should only require a single pass through the matrix.

  4. Find number of islands inside a matrix.

    Find the total number of distinct landmasses. In a 2D grid where each cell is either land (represented by '1') or water (represented by '0'). Two land cells are considered part of the same landmass if they are adjacent horizontally or vertically.

  5. Find max number in each row of a matrix

    Given a table of numbers, write an algorithm to find the highest number in each row of the table.

  6. Multiplay two matrices

    To multiply matrix, take a row from the first matrix and a column from the second matrix, multiply corresponding numbers (first number in the row with the first number in the column, second with second, and so on). Add up all the products. This sum becomes one entry in the new, multiplied matrix. Repeat: Do this for every possible row-column combination to fill the new matrix.

  7. Find peaks inside matrix

    Given a 2D matrix of integers, find and return any "peak" element within the matrix. A peak element is an element in the matrix that is greater than or equal to all of its adjacent neighbors. Adjacent neighbors are the elements directly to its left, right, top, and bottom. For elements at the edges or corners of the matrix, missing neighbors are considered to have a value of negative infinity.

  8. Rotate matrix clockwise by 1

    You are given a square matrix. Write an algorithm that shifts each element in the matrix one position clockwise. For example, the element in the top-left corner should move to the top-right corner.

  9. Rotate matrix 180 degree

    Rotate matrix 180 degree

  10. Rotate matrix 270 degree

    Rotate matrix 270 degree

  11. Rotate matrix 90 degree

    Rotate matrix 90 degree

  12. Sort matrix in a strict order

  13. Find max sub rectangle

    Given a 2D array of numbers, find the sub-matrix (a rectangular portion of the array) that has the greatest sum of its constituent elements.

  14. Transpose of Matrix

    Transpose of Matrix

  15. Travesal matrix in a spiral form

    Design an algorithm to traverse and print all elements of a given m x n matrix in a spiral order, starting from the top-left corner and moving clockwise.