Day 70 of 100 days dsa coding challenge

Published: (December 12, 2025 at 11:39 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Cover image for Day 70 of 100 days dsa coding challenge

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() ≤ 500
  • 1 ≤ 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
Back to Blog

Related posts

Read more »

Day 1/30 back to DSA challenge

DSA - Reviewed notes on DSA. - Solved the Two Sum problem using a hashmap and the complement technique. - Instead of storing each element and checking sums, st...

Advent of Code 2025 - Day 5: Cafeteria

!Me solving AoC Day 5https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazo...