-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2D-Array-problem
More file actions
111 lines (101 loc) · 2.69 KB
/
2D-Array-problem
File metadata and controls
111 lines (101 loc) · 2.69 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
Q1. Given a matrix arr[][] of integers, print the prefix sum matrix for it.
Input:
n = 3
m = 3
[[1,2,3],[4,5,6],[7,8,9]]
Expected Output:
[[1,3,6],[5,13,25],[12,33,67]]
Explanation:
Prefix sum matrix for any cell can be calculated by the sum of its value, prefix sum of upper cell, prefix sum
of left cell.
For the first row and column, we calculate it by prefix sum of its value, and the cell before, for row the left cell,
and for column, the upper cell.
•
•
Assignment Solutions
Cracking the Coding Interview in Java - Foundation
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimension of the array: ");
int n = scn.nextInt();
int[][] mat = new int[n][n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mat[i][j] = scn.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(i == j || i+j == n-1){
if(mat[i][j] == 0){
System.out.println("false");
return;
}
Code:
Explanation:
A matrix generally has 2 diagonals, which are formed by cells where row == column or row + column == n-1.
Q2. A square matrix is said to be an perfect Matrix if both of the following conditions hold:
a) All the elements in the diagonals of the matrix are non-zero integers.
b) All other elements except the diagonal elements are 0.
Given a 2D integer array grid of size n*n representing a square matrix, return true if the grid is a perfect matrix.
Otherwise, return false.
Input:
n = 4
arr[] = [[1,0,0,1],[0,2,1,0],[0,1,2,0],[3,0,0,1]]
Expected Output:
true
•
Assignment Solutions
Cracking the Coding Interview in Java - Foundation
}else{
if(mat[i][j] != 0){
System.out.println("false");
return;
}
}
}
}
System.out.println("true");
}
}
Q3. Write a user defined function upper() which takes an integer square matrix as an input and its size N and prints
the upper half of the matrix.
•
Input:
N=4
arr[][]=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Expected Output:
1 2 3 4
6 7 8
11 12
16
Explanation:
Elements of the upper matrix are those cells where column index exceeds or is equal to row index.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimension of the array: ");
int n = scn.nextInt();
int[][] mat = new int[n][n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mat[i][j] = scn.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(j >= i){
System.out.print(mat[i][j] + " ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}