File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
src/hackerrank/problem-solving Expand file tree Collapse file tree 2 files changed +48
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
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 ) ;
You can’t perform that action at this time.
0 commit comments