Python Program to Efficiently compute sums of diagonals of a matrix Last Updated : 31 May, 2022 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 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The 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 : 4 1 2 3 4 4 3 2 1 7 8 9 6 6 5 4 3 Output : Principal Diagonal: 16 Secondary Diagonal: 20 Input : 3 1 1 1 1 1 1 1 1 1 Output : Principal Diagonal: 3 Secondary Diagonal: 3 Method 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: Python3 # A simple Python program to # find sum of diagonals MAX = 100 def printDiagonalSums(mat, n): principal = 0 secondary = 0; for i in range(0, n): for j in range(0, n): # Condition for principal diagonal if (i == j): principal += mat[i][j] # Condition for secondary diagonal if ((i + j) == (n - 1)): secondary += mat[i][j] print("Principal Diagonal:", principal) print("Secondary Diagonal:", secondary) # Driver code a = [[ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ]] printDiagonalSums(a, 4) # This code is contributed # by ihritik Output: Principal Diagonal:18 Secondary Diagonal:18 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: Python3 # A simple Python3 program to find # sum of diagonals MAX = 100 def printDiagonalSums(mat, n): principal = 0 secondary = 0 for i in range(0, n): principal += mat[i][i] secondary += mat[i][n - i - 1] print("Principal Diagonal:", principal) print("Secondary Diagonal:", secondary) # Driver code a = [[ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ]] printDiagonalSums(a, 4) # This code is contributed # by ihritik Output : Principal Diagonal:18 Secondary Diagonal:18 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 Next Article Python Program to Efficiently compute sums of diagonals of a matrix K kartik Follow Improve Article Tags : Python school-programming CBSE - Class 11 Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 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 Like