-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14thClass2Array.js
More file actions
135 lines (123 loc) · 2.53 KB
/
14thClass2Array.js
File metadata and controls
135 lines (123 loc) · 2.53 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
let matrix = [
[2, 14, 15, 16],
[7, 18, 20, 22],
[9, 21, 23, 24],
[10, 26, 27, 28]
]
//console.log(diagonalMatrix(matrix, matrix.length, matrix[0].length));
function diagonalMatrix(matrix, m, n) {
for(let i=0;i<m;i++) {
for(let j=0;j<n;j++) {
if(i!==j && matrix[i][j] !== 0) {
return false;
}
}
}
return true;
}
/*
00, 01, 02, 03
10, 11, 12, 13
20, 21, 22, 23
*/
//console.log(columnWiseSum(matrix, matrix.length, matrix[0].length));
function columnWiseSum(matrix, m, n) {
let result = [];
for(let j=0;j<n;j++) {
let sum = 0;
for(let i=0;i<m;i++) {
sum+=matrix[i][j];
}
result.push(sum);
}
return result;
}
console.log(searchElement(matrix, matrix.length, matrix[0].length, 10))
function searchElement(matrix, m, n, element) {
let i=0;
let j=n-1;
while(j >= 0 && i < m) {
if(matrix[i][j] === element) {
return [i,j];
} else if(matrix[i][j] < element) {
i++;
} else {
j--;
}
}
return false;
}
/*
[
[00, 01, 02, 03]
[10, 11, 12, 13]
[20, 21, 22, 23]
]
*/
/*
[
[-1, -9, -8, -22]
[10, 11, 12, 13]
[20, 21, 22, 23]
]
*/
function maxInARow(matrix, m, n) {
let result = [];
for(let insideMatrix of matrix) {
let max = Number.MIN_SAFE_INTEGER;
for(let value of insideMatrix) {
max = value > max ? value : max;
}
result.push(max);
}
}
function uniqueElement(matrix) {
let obj = {};
for(let insideMatrix of matrix) {
for(let value of insideMatrix) {
if(obj[value]) {
obj[value] = obj[value] + 1;
} else {
obj[value] = 1;
}
}
}
/*
*/
for(let [key, value] of Object.entries(obj)) {
if(value === 1) {
console.log(key);
}
}
}
let result = multiplyMatrix(
[
[1, 2, 3],
[4, 5, 6]
],
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
2,
3,
3,
3
)
result.forEach((inside) => console.log(inside))
function multiplyMatrix(matrix1, matrix2, r1, c1, r2, c2) {
let result = Array.from(Array(r1), function() {
return new Array(c2);
});
for(let i=0;i<r1;i++) {
for(let j=0;j<c2;j++) {
let sum = 0;
for(let k=0;k<r2;k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
result[i][j] = sum;
}
}
return result;
}