LeetCode 1572 Matrix Diagonal Sum Solution

Starting with #1/100DaysOfCode 💪 Sharing something from LeetCode today 🚀 Recently solved 1572. Matrix Diagonal Sum, and it had a small twist that can confuse many people. 💡 Approach : Traverse the matrix using loops Add elements where: i == j → Primary diagonal i + j == n - 1 → Secondary diagonal ⚠️ Common Confusion: Most people think that for this matrix: [[1,2,3], [4,5,6], [7,8,9]] the element 5 will be counted twice ❌ But it’s NOT counted twice ✅ Because we use: if(i == j) else if(i + j == n - 1) So once the first condition is true, the else if will not run. That’s why the middle element is added only one time. Grateful to learn and grow with guidance from: RAVI KUMAR Coding Blocks Sunstone #LeetCode #CodingJourney #Java #ProblemSolving #Consistency #CodingBlocks #SUNSTONE

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories