Javascript Program to Efficiently compute sums of diagonals of a matrix Last Updated : 10 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Diagonal: The row-column condition is row = column. The secondary diagonal is formed by the elements A03, A12, A21, A30.Condition for Secondary Diagonal: The row-column condition is row = numberOfRows - column -1.Examples : Input : 41 2 3 44 3 2 17 8 9 66 5 4 3Output :Principal Diagonal: 16Secondary Diagonal: 20Input :31 1 11 1 11 1 1Output :Principal Diagonal: 3Secondary Diagonal: 3Method 1 (O(n ^ 2) :In this method, we use two loops i.e. a loop for columns and a loop for rows and in the inner loop we check for the condition stated above: JavaScript // A simple Javascript program to find sum of diagonals const MAX = 100; function printDiagonalSums(mat, n) { let principal = 0, secondary = 0; for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { // Condition for principal diagonal if (i == j) principal += mat[i][j]; // Condition for secondary diagonal if ((i + j) == (n - 1)) secondary += mat[i][j]; } } console.log("Principal Diagonal:" + principal); console.log("Secondary Diagonal:" + secondary); } // Driver code let a = [[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]; printDiagonalSums(a, 4); OutputPrincipal Diagonal:18 Secondary Diagonal:18 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.Method 2 (O(n) :In this method we use one loop i.e. a loop for calculating sum of both the principal and secondary diagonals: JavaScript // An efficient Javascript program to find // sum of diagonals function printDiagonalSums(mat, n) { let principal = 0, secondary = 0; for (let i = 0; i < n; i++) { principal += mat[i][i]; secondary += mat[i][n - i - 1]; } console.log("Principal Diagonal:" + principal); console.log("Secondary Diagonal:" + secondary); } // Driver code let a = [[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]; printDiagonalSums(a, 4); // This code is contributed Bobby OutputPrincipal Diagonal:18 Secondary Diagonal:18 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 Efficiently compute sums of diagonals of a matrix for more details! Comment More infoAdvertise with us K kartik Follow Improve Article Tags : JavaScript school-programming CBSE - Class 11 Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Types of Operating Systems Operating Systems can be categorized according to different criteria like whether an operating system is for mobile devices (examples Android and iOS) or desktop (examples Windows and Linux). Here, we are going to classify based on functionalities an operating system provides.8 Main Operating System 11 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application 8 min read Array Reverse - Complete Tutorial Given an array arr[], the task is to reverse the array. Reversing an array means rearranging the elements such that the first element becomes the last, the second element becomes second last and so on.Examples: Input: arr[] = {1, 4, 3, 2, 6, 5} Output: {5, 6, 2, 3, 4, 1}Explanation: The first elemen 15+ min read Linear Search Algorithm Given an array, arr of n integers, and an integer element x, find whether element x is present in the array. Return the index of the first occurrence of x in the array, or -1 if it doesn't exist.Input: arr[] = [1, 2, 3, 4], x = 3Output: 2Explanation: There is one test case with array as [1, 2, 3 4] 9 min read C++ Data Types Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ 7 min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read Understanding file sizes | Bytes, KB, MB, GB, TB, PB, EB, ZB, YB In the digital world, data is stored, processed, and transferred using a binary system that revolves around bits and bytes. Understanding how this system works is essential for anyone looking to navigate the world of computers, whether you're a student, a professional, or just someone curious about 11 min read Introduction to Operating System An operating system acts as an intermediary between the user of a computer and computer hardware. In short its an interface between computer hardware and user. The purpose of an operating system is to provide an environment in which a user can execute programs conveniently and efficiently. An operat 10 min read Like