Day 70 of 100 days dsa coding challenge
Source: Dev.to

Introduction
Taking on a new challenge: solving GeeksforGeeks Problem of the Day (POTD) daily and sharing my solutions! 💻🔥
The goal is to sharpen problem‑solving skills, level up coding, and learn something new every day. Follow my journey! 🚀
Problem: Swap Diagonals
Difficulty: Easy | Accuracy: 85.42%
Given a square matrix mat[][], swap the elements of the major and minor diagonals.
- Major Diagonal: Elements from the top‑left corner to the bottom‑right corner (where row index equals column index).
- Minor Diagonal: Elements from the top‑right corner to the bottom‑left corner (where
row + column = n - 1).
Examples
Example 1
Input:
mat[][] = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
Output:
[
[2, 1, 0],
[3, 4, 5],
[8, 7, 6]
]
Explanation:
Major diagonal = [0, 4, 8]
Minor diagonal = [2, 4, 6]
Swapping the diagonal elements of the same row results in the major diagonal becoming the minor diagonal and vice‑versa.
Example 2
Input:
mat[][] = [
[2, 3],
[5, 4]
]
Output:
[
[3, 2],
[4, 5]
]
Explanation:
Major diagonal = [2, 4]
Minor diagonal = [3, 5]
After swapping, the diagonals are exchanged.
Constraints
1 ≤ mat.size() ≤ 5001 ≤ mat[i][j] ≤ 10⁶
Solution
class Solution:
def swapDiagonal(self, mat):
n = len(mat)
for i in range(n):
# Swap the element on the major diagonal with the corresponding element on the minor diagonal
mat[i][i], mat[i][n - 1 - i] = mat[i][n - 1 - i], mat[i][i]
return mat