Skip to content

Commit fba16f1

Browse files
Merge pull request #78 from wakidurrahman/feat/problem-solve-03
Feat/problem solve 03
2 parents 987439d + 6efd4d3 commit fba16f1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 004. Sum of an Array
2+
3+
/*
4+
* Complete the 'aVeryBigSum' function below.
5+
*
6+
* The function is expected to return a LONG_INTEGER.
7+
* The function accepts LONG_INTEGER_ARRAY ar as parameter.
8+
*/
9+
10+
function aVeryBigSum(ar) {
11+
// Write your code here
12+
const sum = ar.reduce((acct, curr) => {
13+
return acct + curr;
14+
}, 0);
15+
return sum;
16+
}
17+
18+
// 004:
19+
20+
function diagonalDifference(arr) {
21+
const lengthOfArray = arr.length; // Length of the array
22+
let leftToRight = 0;
23+
let rightToLeft = 0;
24+
25+
for (let i = 0; i < lengthOfArray; i++) {
26+
for (let j = 0; j < lengthOfArray; j++) {
27+
// Find The left-to-right diagonal
28+
if (i === j) {
29+
leftToRight += arr[i][j];
30+
}
31+
// Find The right to left diagonal diagonal
32+
if (i + j === lengthOfArray - 1) {
33+
rightToLeft += arr[i][j];
34+
}
35+
}
36+
}
37+
38+
// calculate the absolute difference between the sums of its diagonals.
39+
return Math.abs(leftToRight - rightToLeft);
40+
}
41+
42+
const myArray = [
43+
[0, 1, 2, 3],
44+
[4, 5, 6, 7],
45+
[8, 9, 0, 0],
46+
[4, 5, 6, 7],
47+
];
48+
diagonalDifference(myArray);

0 commit comments

Comments
 (0)