710021. Matrix Operations
Matrix Operations
Slug: matrix-operations Difficulty: Easy Id: 710021 Topic Tags: Matrix, Data Structures Company Tags: Samsung
Summary
The problem involves performing various operations on a given matrix. The operations may include addition, subtraction, multiplication, and transposition of the matrix. This problem is based on the tags "Matrix" and "Data Structures".
Detailed Explanation
To solve this problem, we need to understand how to perform these operations on a matrix.
Addition and Subtraction: To add or subtract two matrices, they must have the same dimensions (number of rows and columns). We iterate through each element in the matrices, adding or subtracting corresponding elements.
Multiplication: Matrix multiplication is more complex. Two matrices can be multiplied only if the number of columns in the first matrix matches the number of rows in the second matrix. The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix. We iterate through each element in the matrices, multiplying corresponding elements.
Transposition: To transpose a matrix, we swap its rows with columns. This can be done by iterating through each element in the original matrix, placing it in the corresponding position in the transposed matrix.
Here is a step-by-step breakdown of the solution:
- Read the input matrix and operation type (addition, subtraction, multiplication, or transposition).
- Perform the specified operation on the matrix.
- Print the resulting matrix.
The time complexity of these operations depends on the size of the input matrix. The space complexity is O(1) as we only use a constant amount of space to store the result.
Here are some ASCII art diagrams for illustration purposes:
Addition:
Matrix 1: [[1, 2], [3, 4]]
Matrix 2: [[5, 6], [7, 8]]
Result: [[6, 8], [10, 12]]
Subtraction:
Matrix 1: [[1, 2], [3, 4]]
Matrix 2: [[-1, -2], [-3, -4]]
Result: [[2, 4], [6, 8]]
Multiplication:
Matrix 1: [[1, 2], [3, 4]]
Matrix 2: [[5, 10], [15, 20]]
Result: [[5, 20], [45, 80]]
Transposition:
Original: [[1, 2], [3, 4]]
Transposed: [[1, 3], [2, 4]]
Optimized Solutions
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[scanner.nextInt()][scanner.nextInt()];
char operation = scanner.next().charAt(0);
// Read the input matrix and perform the specified operation
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = scanner.nextInt();
}
}
int[][] result;
switch (operation) {
case 'A':
// Addition
result = addMatrices(matrix);
break;
case 'S':
// Subtraction
result = subtractMatrices(matrix, scanner.nextInt());
break;
case 'M':
// Multiplication
result = multiplyMatrices(matrix, scanner.nextInt());
break;
case 'T':
// Transposition
result = transposeMatrix(matrix);
break;
default:
System.out.println("Invalid operation");
return;
}
// Print the resulting matrix
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
public static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {
// Add corresponding elements in the two matrices
int[][] result = new int[matrix1.length][matrix1[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return result;
}
public static int[][] subtractMatrices(int[][] matrix, int k) {
// Subtract k from each element in the matrix
int[][] result = new int[matrix.length][matrix[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = matrix[i][j] - k;
}
}
return result;
}
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
// Multiply corresponding elements in the two matrices
int[][] result = new int[matrix1.length][matrix2[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
int sum = 0;
for (int k = 0; k < matrix1[0].length; k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
result[i][j] = sum;
}
}
return result;
}
public static int[][] transposeMatrix(int[][] matrix) {
// Swap rows and columns in the original matrix
int[][] result = new int[matrix[0].length][matrix.length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = matrix[j][i];
}
}
return result;
}
}