LeetCode 1572 Matrix Diagonal Sum Solution

🚀 100 Days LeetCode Challenge – Day 33 🚀 Solved LeetCode 1572: Matrix Diagonal Sum ✅ Today’s problem was simple in concept but a good reminder of index relationships in matrices. 💡 Core Idea: In a square matrix: Primary diagonal → i == j Secondary diagonal → i + j == n - 1 Traverse the matrix and: Add elements where i == j Add elements where j == n - i - 1 Make sure not to double count the center element (when n is odd) 🧠 Key Learnings: Understanding diagonal patterns is important for matrix problems Index math (i + j = n - 1) is extremely useful Avoid unnecessary nested loops — this can be optimized to O(n) 👉 Instead of O(n²), we can directly compute using: mat[i][i] mat[i][n-1-i] ⏱️ Complexity: Time: O(n²) Space: O(1) #100DaysOfCode #LeetCode #Matrix #Cpp #ProblemSolving #DSA

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories