700817. Matrix Interchange

Easy

Matrix Interchange

Slug: matrix-interchange

Difficulty: Easy

Id: 700817

Topic Tags: Arrays, Data Structures

Company Tags: None

Summary

Given a 2D matrix and two indices (i1, j1) and (i2, j2), swap the elements at these two positions. This problem requires understanding of basic array manipulation and indexing.

Detailed Explanation

To solve this problem, we will iterate over the given matrix to find the specified indices. Then, we will use temporary variables to store the values at those indices, swap them, and finally assign the swapped values back to their original positions.

Here's a step-by-step breakdown of the solution:

  1. Initialize two variables temp and swap to store the values at (i1, j1) and (i2, j2), respectively.
  2. Iterate over the matrix using nested loops (or array indexing if you're familiar with it).
  3. Check if the current iteration corresponds to the desired indices (i1, j1) or (i2, j2).
  4. If the current iteration is for (i1, j1), store its value in temp. If it's for (i2, j2), store its value in swap.
  5. Once you've found both indices, swap their values by assigning swap to the first index and temp to the second index.
  6. Return the modified matrix.

Time complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix. Space complexity: O(1), as we only use a constant amount of space for the temporary variables.

Optimized Solutions

Java

View on GeeksforGeeks
public static void matrixInterchange(int[][] matrix, int i1, int j1, int i2, int j2) {
    if (i1 == i2 && j1 != j2) {
        // Swap columns
        int temp = matrix[i1][j1];
        matrix[i1][j1] = matrix[i2][j2];
        matrix[i2][j2] = temp;
    } else if (i1 != i2 && j1 == j2) {
        // Swap rows
        int[][] tempMatrix = new int[matrix.length][];
        for (int i = 0; i < matrix.length; i++) {
            tempMatrix[i] = new int[matrix[0].length];
            for (int j = 0; j < matrix[0].length; j++) {
                if (i == i1) {
                    tempMatrix[i][j] = matrix[i2][j];
                } else if (i == i2) {
                    tempMatrix[i][j] = matrix[i1][j];
                } else {
                    tempMatrix[i][j] = matrix[i][j];
                }
            }
        }
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (i == i1) {
                    matrix[i][j] = tempMatrix[i2][j];
                } else if (i == i2) {
                    matrix[i][j] = tempMatrix[i1][j];
                } else {
                    matrix[i][j] = tempMatrix[i][j];
                }
            }
        }
    } else {
        int temp = matrix[i1][j1];
        matrix[i1][j1] = matrix[i2][j2];
        matrix[i2][j2] = temp;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.