Javascript Program to Find difference between sums of two diagonals Last Updated : 10 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given a matrix of n X n. The task is to calculate the absolute difference between the sums of its diagonal.Examples: Input : mat[][] = 11 2 4 4 5 6 10 8 -12 Output : 15Sum of primary diagonal = 11 + 5 + (-12) = 4.Sum of primary diagonal = 4 + 5 + 10 = 19.Difference = |19 - 4| = 15.Input : mat[][] = 10 2 4 5Output : 7Calculate the sums across the two diagonals of a square matrix. Along the first diagonal of the matrix, row index = column index i.e mat[i][j] lies on the first diagonal if i = j. Along the other diagonal, row index = n - 1 - column index i.e mat[i][j] lies on the second diagonal if i = n-1-j. By using two loops we traverse the entire matrix and calculate the sum across the diagonals of the matrix.Below is the implementation of this approach: JavaScript // Javascript Code for Find difference between sums // of two diagonals function difference(arr, n) { // Initialize sums of diagonals let d1 = 0, d2 = 0; for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { // finding sum of primary diagonal if (i == j) d1 += arr[i][j]; // finding sum of secondary diagonal if (i == n - j - 1) d2 += arr[i][j]; } } // Absolute difference of the sums // across the diagonals return Math.abs(d1 - d2); } /* Driver program to test above function */ let n = 3; let arr = [ [11, 2, 4], [4, 5, 6], [10, 8, -12] ]; console.log(difference(arr, n)); // This code is contributed Bobby Output15 Complexity Analysis:Time Complexity: O(N*N), as we are using nested loops to traverse N*N times.Auxiliary Space: O(1), as we are not using any extra space.We can optimize above solution to work in O(n) using the patterns present in indexes of cells. JavaScript // JAVA SCRIPT Code for Find difference between sums // of two diagonals function difference(arr, n) { // Initialize sums of diagonals let d1 = 0, d2 = 0; for (let i = 0; i < n; i++) { d1 += arr[i][i]; d2 += arr[i][n - i - 1]; } // Absolute difference of the sums // across the diagonals return Math.abs(d1 - d2); } /* Driver program to test above function */ let n = 3; let arr = [[11, 2, 4], [4, 5, 6], [10, 8, -12]]; console.log(difference(arr, n)); // This code is contributed by sravan kumar Gottumukkala Output15 Complexity Analysis:Time Complexity: O(N), as we are using a loop to traverse N times.Auxiliary Space: O(1), as we are not using any extra space.Please refer complete article on Find difference between sums of two diagonals for more details! Comment More infoAdvertise with us Next Article Javascript Program to Find difference between sums of two diagonals K kartik Follow Improve Article Tags : Matrix JavaScript Web Technologies DSA Practice Tags : Matrix Similar Reads Find difference between sums of two diagonals Given a matrix of n X n. The task is to calculate the absolute difference between the sums of its diagonal. Examples: Input : mat[][] = 11 2 4 4 5 6 10 8 -12 Output : 15 Sum of primary diagonal = 11 + 5 + (-12) = 4. Sum of secondary diagonal = 4 + 5 + 10 = 19. Difference = |19 - 4| = 15. Input : mat 9 min read Javascript Program to Efficiently compute sums of diagonals of a matrix Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix.A00 A01 A02 A03A10 A11 A12 A13A20 A21 A22 A23A30 A31 A32 A33The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal Diago 3 min read Javascript Program to Find a pair with the given difference Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78Output: Pair Found: (2, 80)Input: arr[] = {90, 70, 20, 80, 50}, n = 45Output: No Such PairNative Approach:The simplest method is t 4 min read Javascript Program for Diagonally Dominant Matrix In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant 2 min read Find N integers with given difference between product and sum Given two integers N and D, Find a set of N integers such that the difference between their product and sum is equal to D. Examples: Input : N = 2, D = 1 Output : 2 3 Explanation: product = 2*3 = 6, Sum = 2 + 3 = 5. Hence, 6 - 5 = 1(D). Input : N = 3, D = 5. Output : 1 2 8 Explanation : Product = 1* 3 min read Like