-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterchangeElementsMatrix.java
More file actions
62 lines (47 loc) · 1.68 KB
/
InterchangeElementsMatrix.java
File metadata and controls
62 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class InterchangeElementsMatrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
interchange(matrix);
for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
public static void interchange(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
for (int i = 0; i < rows; i++) {
int temp = matrix[i][0];
matrix[i][0] = matrix[i][cols - 1];
matrix[i][cols - 1] = temp;
}
}
}
/*
* Approach:
Problem:
The problem is to interchange the first and last elements of each row in a given matrix.
Preface:
The program initializes a matrix, interchanges the first and last elements of each row, and then prints the modified matrix.
Logic:
The main method initializes a matrix and calls the interchange method to interchange the first and last elements of each row.
The interchange method iterates through each row of the matrix.
For each row, it swaps the first and last elements.
After the interchange, the main method prints the modified matrix.
Pseudocode:
1. Initialize a matrix.
2. Call interchange method to interchange first and last elements of each row.
3. Iterate through each row of the matrix:
- Swap the first and last elements.
4. Print the modified matrix.
Time Complexity:
Interchanging elements: O(n), where n is the number of rows in the matrix.
Output:
The program will output the modified matrix where the first and last elements of each row are interchanged.
*/