-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundaryElements.java
More file actions
74 lines (55 loc) · 2.09 KB
/
BoundaryElements.java
File metadata and controls
74 lines (55 loc) · 2.09 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
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
public class BoundaryElements {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printBoundary(matrix);
}
public static void printBoundary(int[][] matrix) {
int rows = matrix.length;
int columns = matrix[0].length;
for (int i = 0; i < columns; i++) {
System.out.print(matrix[0][i] + " ");
}
for (int i = 1; i < rows - 1; i++) {
System.out.print(matrix[i][columns - 1] + " ");
}
if (rows > 1) {
for (int i = columns - 2; i >= 0; i--) {
System.out.print(matrix[rows - 1][i] + " ");
}
}
if (columns > 1) {
for (int i = rows - 2; i > 0; i--) {
System.out.print(matrix[i][0] + " ");
}
}
}
}
/*
* Approach:
Problem:
The problem aims to print the boundary elements of a given matrix.
Preface:
The program defines a method printBoundary() to print the boundary elements of a given matrix.
Logic:
Retrieve the number of rows and columns in the matrix.
Print the first row from left to right.
Print the last column from top to bottom (excluding the first and last elements).
If there are more than one row, print the last row from right to left (excluding the first and last elements).
If there are more than one column, print the first column from bottom to top (excluding the first and last elements).
Pseudocode:
1. Get the number of rows and columns in the matrix.
2. Print the first row from left to right.
3. Print the last column from top to bottom (excluding the first and last elements).
4. If there are more than one row, print the last row from right to left (excluding the first and last elements).
5. If there are more than one column, print the first column from bottom to top (excluding the first and last elements).
Time Complexity:
Retrieving matrix dimensions: O(1)
Printing boundary elements: O(rows + columns)
Output:
The program will output the boundary elements of the given matrix.
*/