-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2D-ArrayProblem-1
More file actions
199 lines (182 loc) · 4.06 KB
/
Copy path2D-ArrayProblem-1
File metadata and controls
199 lines (182 loc) · 4.06 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Q1 - Given two integer matrices, multiply the matrices, if possible, else return “Invalid Input”. (Medium)
Assignment Solutions
Input1:
Expected Output:
n1 = 2
m1 = 3
arr1 = {{2,4,1}, {3,,6}}
n2 = 3
m2 = 2
arr2 = {{1,2}, {3,4}, {,7}}
19 27
48 68
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimensions of first array: ");
int n1 = scn.nextInt();
int m1 = scn.nextInt();
int[][] arr1 = new int[n1][m1];
System.out.println("Enter the elements of first array: ");
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
arr1[i][j] = scn.nextInt();
}
}
System.out.println("Enter the dimensions of second array: ");
int n2 = scn.nextInt();
int m2 = scn.nextInt();
int[][] arr2 = new int[n2][m2];
System.out.println("Enter the elements of second array: ");
for (int i = 0; i < n2; i++) {
for (int j = 0; j < m2; j++) {
Cracking the Coding Interview in JAVA - Foundation
arr2[i][j] = scn.nextInt();
}
}
if (m1 != n2) {
return;
}
int[][] ans = new int[n1][m2];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
for (int k = 0; k < m1; k++) {
ans[i][j] += (arr1[i][k] * arr2[k][j]);
}
}
}
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
System.out.print(ans[i][j] + " ");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimensions of the array: ");
int n = scn.nextInt();
int m = scn.nextInt();
int[][] mat = new int[n][m];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j] = scn.nextInt();
}
}
for (int x = 0; x < n / 2; x++) {
for (int y = x; y < n - x - 1; y++) {
int temp = mat[x][y];
Assignment Solutions
Cracking the Coding Interview in JAVA - Foundation
mat[x][y] = mat[y][n - 1 - x];
mat[y][n - 1 - x] = mat[n - 1 - x][n - 1 - y];
mat[n - 1 - x][n - 1 - y] = mat[n - 1 - y][x];
mat[n - 1 - y][x] = temp;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimensions of the array: ");
int n = scn.nextInt();
int m = scn.nextInt();
int[][] mat = new int[n][m];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j] = scn.nextInt();
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if(mat[i][j] != mat[i-1][j-1]){
System.out.println(false);
return;
}
}
}
System.out.println(true);
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println(]Enter the dimensions of the array: ]);
int n = scn.nextInt();
int m = scn.nextInt();
int[][] mat = new int[n][m];
System.out.println(]Enter the elements of the array: ]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j] = scn.nextInt();
}
}
int[] arr = new int[n*m];
int idx = 0;
int row = 0;
int col = 0;
while(col < m){
int i = row;
int j = col;
while(i >= 0 && j < m){
arr[idx] = mat[i][j];
idx++;
i--;
j++;
}
row++;
if(row >= n){
row = n-1;
col++;
}
}
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + ] ]);
}
}
}
import java.util.Scanner;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimensions of the array: ");
int n = scn.nextInt();
int m = scn.nextInt();
int[][] mat = new int[n][m];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j] = scn.nextInt();
}
}
Arrays.sort(mat, (a,b) -> a[0]-b[0]);
int count = 0;
int i = 0;
while(i < n){
while(i < n-1 && mat[i+1][0] <= mat[i][1]){
mat[i+1][0] = mat[i][0];
mat[i+1][1] = Math.max(mat[i][1], mat[i+1][1]);
i++;
}
i++;
count++;
}
System.out.println(count);
}
}