The Wayback Machine - https://web.archive.org/web/20240621154222/https://www.geeksforgeeks.org/shift-matrix-elements-k/
Open In App

Shift matrix elements row-wise by k

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a square matrix mat[][] and a number k. The task is to shift the first k elements of each row to the right of the matrix.

Examples : 

Input : mat[N][N] = {{1, 2, 3},
                     {4, 5, 6},
                     {7, 8, 9}}
        k = 2
Output :mat[N][N] = {{3, 1, 2}
                     {6, 4, 5}
                     {9, 7, 8}}

Input : mat[N][N] = {{1, 2, 3, 4}
                     {5, 6, 7, 8}
                     {9, 10, 11, 12}
                     {13, 14, 15, 16}}
        k = 2
Output :mat[N][N] = {{3, 4, 1, 2}
                     {7, 8, 5, 6}
                     {11, 12, 9, 10}
                     {15, 16, 13, 14}}
Note: Matrix should be a square matrix 

Implementation:

C++




// C++ program to shift k elements in a matrix.
#include <bits/stdc++.h>
using namespace std;
#define N 4
  
// Function to shift first k elements of 
// each row of matrix.
void shiftMatrixByK(int mat[N][N], int k)
{
    if (k > N) {
        cout << "shifting is not possible" << endl;
        return;
    }
      
    int j = 0;
    while (j < N) {
          
        // Print elements from index k
        for (int i = k; i < N; i++)
            cout << mat[j][i] << " ";
              
        // Print elements before index k
        for (int i = 0; i < k; i++)
            cout << mat[j][i] << " ";
              
        cout << endl;
        j++;
    }
}
  
// Driver code
int main()
{
    int mat[N][N] = {{1, 2, 3, 4},
                     {5, 6, 7, 8},
                     {9, 10, 11, 12},
                     {13, 14, 15, 16}};
    int k = 2;
      
    // Function call
    shiftMatrixByK(mat, k);
      
    return 0;
}


Java




// Java program to shift k elements in a 
// matrix.
import java.io.*;
import java.util.*;
  
public class GFG {
       
    static int N = 4;
       
    // Function to shift first k elements 
    // of each row of matrix.
    static void shiftMatrixByK(int [][]mat,
                                    int k)
    {
        if (k > N) {
            System.out.print("Shifting is"
                        + " not possible");
            return;
        }
           
        int j = 0;
        while (j < N) {
               
            // Print elements from index k
            for (int i = k; i < N; i++)
                System.out.print(mat[j][i] + " ");
                   
            // Print elements before index k
            for (int i = 0; i < k; i++)
                System.out.print(mat[j][i] + " ");
                   
            System.out.println();
            j++;
        }
    }
       
    // Driver code
    public static void main(String args[])
    {
        int [][]mat = new int [][]
                    { {1, 2, 3, 4},
                      {5, 6, 7, 8},
                      {9, 10, 11, 12},
                      {13, 14, 15, 16} };
        int k = 2;
           
        // Function call
        shiftMatrixByK(mat, k);
    }
}
   
// This code is contributed by Manish Shaw 
// (manishshaw1)


Python3




# Python3 program to shift k 
# elements in a matrix.
  
N = 4
# Function to shift first k 
# elements of each row of 
# matrix.
def shiftMatrixByK(mat, k):
    if (k > N) :
        print ("shifting is"
            " not possible")
        return
      
    j = 0
    while (j < N) :
          
        # Print elements from
        # index k
        for i in range(k, N):
            print ("{} " . 
            format(mat[j][i]), end="")
              
        # Print elements before
        # index k
        for i in range(0, k):
            print ("{} " . 
            format(mat[j][i]), end="")
              
        print ("")
        j = j + 1
  
# Driver code
mat = [[1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12],
       [13, 14, 15, 16]]
k = 2
  
# Function call
shiftMatrixByK(mat, k)
  
# This code is contributed by 
# Manish Shaw (manishshaw1)


C#




// C# program to shift k elements in a 
// matrix.
using System;
  
class GFG {
      
    static int N = 4;
      
    // Function to shift first k elements 
    // of each row of matrix.
    static void shiftMatrixByK(int [,]mat,
                                    int k)
    {
        if (k > N) {
            Console.WriteLine("shifting is"
                        + " not possible");
            return;
        }
          
        int j = 0;
        while (j < N) {
              
            // Print elements from index k
            for (int i = k; i < N; i++)
                Console.Write(mat[j,i] + " ");
                  
            // Print elements before index k
            for (int i = 0; i < k; i++)
                Console.Write(mat[j,i] + " ");
                  
            Console.WriteLine();
            j++;
        }
    }
      
    // Driver code
    public static void Main()
    {
        int [,]mat = new int [,]
                    { {1, 2, 3, 4},
                      {5, 6, 7, 8},
                      {9, 10, 11, 12},
                      {13, 14, 15, 16} };
        int k = 2;
          
        // Function call
        shiftMatrixByK(mat, k);
    }
}
  
// This code is contributed by Manish Shaw 
// (manishshaw1)


PHP




<?php
// PHP program to shift k 
// elements in a matrix.
  
// Function to shift first k 
// elements of each row of matrix.
function shiftMatrixByK($mat, $k)
{
    $N = 4;
    if ($k > $N) 
    {
        echo ("shifting is not possible\n");
        return;
    }
      
    $j = 0;
    while ($j < $N) 
    {
          
        // Print elements from index k
        for ($i = $k; $i < $N; $i++)
            echo ($mat[$j][$i]." ");
              
        // Print elements before index k
        for ($i = 0; $i < $k; $i++)
            echo ($mat[$j][$i]." ");
              
        echo ("\n");
        $j++;
    }
}
  
// Driver code
$mat = array(array(1, 2, 3, 4),
             array(5, 6, 7, 8),
             array(9, 10, 11, 12),
             array(13, 14, 15, 16));
$k = 2;
      
// Function call
shiftMatrixByK($mat, $k);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>


Javascript




<script>
// Java Script program to shift k elements in a
// matrix.
let N = 4;
      
// Function to shift first k elements
// of each row of matrix.
function shiftMatrixByK(mat,k)
{
    if (k > N) {
        document.write("Shifting is not possible");
        return;
    }
          
    let j = 0;
    while (j < N) {
              
        // Print elements from index k
        for (let i = k; i < N; i++)
            document.write(mat[j][i] + " ");
                  
        // Print elements before index k
        for (let i = 0; i < k; i++)
            document.write(mat[j][i] + " ");
                  
        document.write("<br>");
        j++;
    }
}
      
// Driver code
      
    let mat = 
                [[1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 16]];
    let k = 2;
          
    // Function call
    shiftMatrixByK(mat, k);
      
      
// This code is contributed by sravan kumar G
</script>


Output

3 4 1 2 
7 8 5 6 
11 12 9 10 
15 16 13 14 

Complexity Analysis:

  • Time Complexity: O(n2), 
  • Auxiliary Space: O(1)


Previous Article
Next Article

Similar Reads

Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise. Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20 Approach: Following are the steps: Sort each
9 min read
Row-wise vs column-wise traversal of matrix
Two common ways of traversing a matrix are row-major-order and column-major-order. Row Major Order: When matrix is accessed row by row. Column Major Order: When matrix is accessed column by column.Examples: Input : mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : Row-wise: 1 2 3 4 5 6 7 8 9 Col-wise : 1 4 7 2 5 8 3 6 9 Difference: If we see acc
6 min read
Largest row-wise and column-wise sorted sub-matrix
Given an N * M matrix mat[][], the task is to find the area-wise largest rectangular sub-matrix such that each column and each row of the sub-matrix is strictly increasing. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}} Output: 6 Largest sub-matrix will be {{1, 2, 3}, {4, 5, 6}}. Number of elements in this sub-matrix = 6. Input: mat[]
13 min read
Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
Given a matrix mat[][] whose elements are sorted both row-wise and column-wise. The task is to find the maximum sum of any submatrix from the given matrix mat[][]. Examples: Input: mat[][] = { {-6, -4, -1}, {-3, 2, 4}, {2, 5, 8}} Output: 19 Explanation: The largest submatrix is given by: 2 4 5 8Input: mat[][] = { {-4, -3}, {-2, -1} } Output: -1 Exp
10 min read
C++ Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:   Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20   Approach: Following are the steps:  Sort e
3 min read
Java Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:   Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20   Approach: Following are the steps:  Sort e
3 min read
Python Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20 Approach: Following are the steps: Sort each r
4 min read
Php Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:   Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20   Approach: Following are the steps:  Sort e
3 min read
Javascript Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:   Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20   Approach: Following are the steps:  Sort e
2 min read
Encode given String by inserting in Matrix column-wise and printing it row-wise
Given a string S and an integer R, the task is to encode the string by first filling each character in column wise manner from top to bottom in a matrix having R rows and then printing the matrix row-wise. Examples: Input: S = "abcdefgh", R = 3Output: adgbehcfExplanation: Matrix formed is:a d gb e hc fSo when printed row wise it gives the encoded s
4 min read
Practice Tags :
three90RightbarBannerImg