Print matrix in zig-zag fashionGiven a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: {{1, 2, 3}{4, 5, 6}{7, 8, 9}}Output: 1 2 4 7 5 3 6 8 9Input : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]Output:: 1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16Thi
10 min read
Program for scalar multiplication of a matrixGiven a 2D matrix mat[][] with n rows and m columns and a scalar element k, the task is to find out the scalar product of the given matrix.Examples: Input: mat[][] = [[2, 3], [5, 4]]k = 5Output: [[10, 15], [25, 20]]Input:mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]k = 4Output: [[4, 8, 12], [16, 20, 2
4 min read
Print a given matrix in spiral formGiven a matrix mat[][] of size m x n, the task is to print all elements of the matrix in spiral form.Examples: Input: mat[][] = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]Output: [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]Example of matrix in spiral formInput: mat[]
14 min read
Find distinct elements common to all rows of a matrixGiven a n x n matrix. The problem is to find all the distinct elements common to all rows of the matrix. The elements can be printed in any order. Examples: Input : mat[][] = { {2, 1, 4, 3}, {1, 2, 3, 2}, {3, 6, 2, 3}, {5, 2, 5, 3} } Output : 2 3 Input : mat[][] = { {12, 1, 14, 3, 16}, {14, 2, 1, 3,
15+ min read
Find unique elements in a matrixGiven a matrix mat[][] having n rows and m columns. The task is to find unique elements in the matrix i.e., those elements which are not repeated in the matrix or those elements whose frequency is 1. Examples: Input: mat[][] = [[2, 1, 4, 3], [1, 2, 3, 2], [3, 6, 2, 3], [5, 2, 5, 3]]Output: 4 6Input:
5 min read
Find maximum element of each row in a matrixGiven a matrix mat[][], the task is to find the maximum element of each row.Examples: Input: mat[][] = [[1, 2, 3] [1, 4, 9] [76, 34, 21]]Output :3976Input: mat[][] = [[1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]]Output :216556The idea is to run the loop for no_of_rows. Check each element inside the r
4 min read
Shift matrix elements row-wise by kGiven a square matrix mat[][] and a number k. The task is to shift the first k elements of each row to the right of the matrix. Examples : Input : mat[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} k = 2 Output :mat[N][N] = {{3, 1, 2} {6, 4, 5} {9, 7, 8}} Input : mat[N][N] = {{1, 2, 3, 4} {5, 6, 7, 8} {9
6 min read
Program for subtraction of matricesGiven two m x n matrices m1 and m2, the task is to subtract m2 from m1 and return res.Input: m1 = {{1, 2}, {3, 4}}, m2 = {{4, 3}, {2, 1}}Output: {{-3, -1}, {1, 3}}Input: m1 = {{3, 3, 3}, {3, 3, 3}}, m1 = {{2, 2, 2}, {1, 1, 1}},Output: {{1, 1, 1}, {2, 2, 2}},We traverse both matrices element by eleme
5 min read