Given a matrix m[ ][ ] of size n x n consisting of integers and given a character ‘x’ indicating the direction. Value of ‘x’ can be ‘u’, ‘d’, ‘l’, ‘r’ indicating Up, Down, Left, Right correspondingly. The task is to move the element to given direction such that the consecutive elements having same value are added into single value and shift the rest element. Also, shift the element if the next element in given direction is 0.
For example :
Consider x = ‘l’ and matrix m[][],
32 3 3
0 0 1
10 10 8
After adding 3 in first row, 10 in third row and moving 1 in second row,
Matrix will become
32 6 0
1 0 0
20 8 0
Examples :
Input : x = 'l'
m[][] = { { 32, 3, 3, 3, 3 },
{ 0, 0, 1, 0, 0 },
{ 10, 10, 8, 1, 2},
{ 0, 0, 0, 0, 1},
{ 4, 5, 6, 7, 8 } }
Output :
32 6 6 0 0
1 0 0 0 0
20 8 1 2 0
1 0 0 0 0 0
4 5 6 7 8
Input : x = 'u'
m[][] = { { 10, 3, 32 },
{ 10, 0, 96 },
{ 5, 32, 96 } }
Output :
20 3 32
5 32 192
0 0 0
Approach : The idea is to traverse each row or column (depending on given direction) from side x of row or column towards x’ (opposite of x). For example, if the given value of x is ‘l’ (left) then start scanning each row from left side to right. While traversing, store row or column element in the temporary 1-D array (say temp[]) by skipping elements having value 0 and sum of the consecutive element if they have equal value. After that, start copying the temporary array temp[0..k] to the current row or column from the x side (of row or column) to x’ (opposite of x) and fill reset of the element by 0.
Let, x = ‘l’ i.e move towards left. So, start copying each row from left most index to right most index of the row and store in temporary array with processing of ignoring 0s and adding two consecutive element into one if they have same value. Below is the illustration for row 1,

Now, for each, copy temporary array to current row from left most index to right most index. Below is illustration for row 1,

Below is the implementation of this approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 50
void moveMatrix( char d[], int n,
int a[MAX][MAX])
{
if (d[0] == 'r' ) {
for ( int i = 0; i < n; i++) {
vector< int > v, w;
int j;
for (j = n - 1; j >= 0; j--) {
if (a[i][j])
v.push_back(a[i][j]);
}
for (j = 0; j < v.size(); j++) {
if (j < v.size() - 1 && v[j] == v[j + 1]) {
w.push_back(2 * v[j]);
j++;
}
else
w.push_back(v[j]);
}
for (j = 0; j < n; j++)
a[i][j] = 0;
j = n - 1;
for ( auto it = w.begin();
it != w.end(); it++)
a[i][j--] = *it;
}
}
else if (d[0] == 'l' ) {
for ( int i = 0; i < n; i++) {
vector< int > v, w;
int j;
for (j = 0; j < n; j++) {
if (a[i][j])
v.push_back(a[i][j]);
}
for (j = 0; j < v.size(); j++) {
if (j < v.size() - 1 && v[j] == v[j + 1]) {
w.push_back(2 * v[j]);
j++;
}
else
w.push_back(v[j]);
}
for (j = 0; j < n; j++)
a[i][j] = 0;
j = 0;
for ( auto it = w.begin();
it != w.end(); it++)
a[i][j++] = *it;
}
}
else if (d[0] == 'd' ) {
for ( int i = 0; i < n; i++) {
vector< int > v, w;
int j;
for (j = n - 1; j >= 0; j--) {
if (a[j][i])
v.push_back(a[j][i]);
}
for (j = 0; j < v.size(); j++) {
if (j < v.size() - 1 && v[j] == v[j + 1]) {
w.push_back(2 * v[j]);
j++;
}
else
w.push_back(v[j]);
}
for (j = 0; j < n; j++)
a[j][i] = 0;
j = n - 1;
for ( auto it = w.begin();
it != w.end(); it++)
a[j--][i] = *it;
}
}
else if (d[0] == 'u' ) {
for ( int i = 0; i < n; i++) {
vector< int > v, w;
int j;
for (j = 0; j < n; j++) {
if (a[j][i])
v.push_back(a[j][i]);
}
for (j = 0; j < v.size(); j++) {
if (j < v.size() - 1 && v[j] == v[j + 1]) {
w.push_back(2 * v[j]);
j++;
}
else
w.push_back(v[j]);
}
for (j = 0; j < n; j++)
a[j][i] = 0;
j = 0;
for ( auto it = w.begin();
it != w.end(); it++)
a[j++][i] = *it;
}
}
}
int main()
{
char d[2] = "l" ;
int n = 5;
int a[MAX][MAX] = { { 32, 3, 3, 3, 3 },
{ 0, 0, 1, 0, 0 },
{ 10, 10, 8, 1, 2 },
{ 0, 0, 0, 0, 1 },
{ 4, 5, 6, 7, 8 } };
moveMatrix(d, n, a);
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << a[i][j] << " " ;
cout << endl;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void moveMatrix( char d,
int n,
int a[][])
{
if (d == 'r' ) {
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> v = new ArrayList<Integer>();
ArrayList<Integer> w = new ArrayList<Integer>();
int j;
for (j = n - 1 ; j >= 0 ; j--) {
if (a[i][j] != 0 )
v.add(a[i][j]);
}
for (j = 0 ; j < v.size(); j++) {
if (j < v.size() - 1 && v.get(j) == v.get(j + 1 )) {
w.add( 2 * v.get(j));
j++;
}
else
w.add(v.get(j));
}
for (j = 0 ; j < n; j++)
a[i][j] = 0 ;
j = n - 1 ;
for ( int it = 0 ; it < w.size(); it++)
a[i][j--] = w.get(it);
}
}
else if (d == 'l' ) {
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> v = new ArrayList<Integer>();
ArrayList<Integer> w = new ArrayList<Integer>();
int j;
for (j = 0 ; j < n; j++) {
if (a[i][j] != 0 )
v.add(a[i][j]);
}
for (j = 0 ; j < v.size(); j++) {
if (j < v.size() - 1 && v.get(j) == v.get(j + 1 )) {
w.add( 2 * v.get(j));
j++;
}
else
w.add(v.get(j));
}
for (j = 0 ; j < n; j++)
a[i][j] = 0 ;
j = 0 ;
for ( int it = 0 ; it < w.size(); it++)
a[i][j++] = w.get(it);
}
}
else if (d == 'd' ) {
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> v = new ArrayList<Integer>();
ArrayList<Integer> w = new ArrayList<Integer>();
int j;
for (j = n - 1 ; j >= 0 ; j--) {
if (a[j][i] != 0 )
v.add(a[j][i]);
}
for (j = 0 ; j < v.size(); j++) {
if (j < v.size() - 1 && v.get(j) == v.get(j + 1 )) {
w.add( 2 * v.get(j));
j++;
}
else
w.add(v.get(j));
}
for (j = 0 ; j < n; j++)
a[j][i] = 0 ;
j = n - 1 ;
for ( int it = 0 ; it < w.size(); it++)
a[j--][i] = w.get(it);
}
}
else if (d == 'u' ) {
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> v = new ArrayList<Integer>();
ArrayList<Integer> w = new ArrayList<Integer>();
int j;
for (j = 0 ; j < n; j++) {
if (a[j][i] != 0 )
v.add(a[j][i]);
}
for (j = 0 ; j < v.size(); j++) {
if (j < v.size() - 1 && v.get(j) == v.get(j + 1 )) {
w.add( 2 * v.get(j));
j++;
}
else
w.add(v.get(j));
}
for (j = 0 ; j < n; j++)
a[j][i] = 0 ;
j = 0 ;
for ( int it = 0 ; it < w.size(); it++)
a[j++][i] = w.get(it);
}
}
}
public static void main(String args[])
{
char d = 'l' ;
int n = 5 ;
int a[][] = { { 32 , 3 , 3 , 3 , 3 },
{ 0 , 0 , 1 , 0 , 0 },
{ 10 , 10 , 8 , 1 , 2 },
{ 0 , 0 , 0 , 0 , 1 },
{ 4 , 5 , 6 , 7 , 8 } };
moveMatrix(d, n, a);
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++)
System.out.print(a[i][j] + " " );
System.out.println();
}
}
}
|
Python3
MAX = 50
def moveMatrix(d, n, a):
if (d[ 0 ] = = 'r' ):
for i in range (n):
v = []
w = []
for j in range (n - 1 , - 1 , - 1 ):
if (a[i][j]):
v.append(a[i][j])
j = 0
while (j < len (v)):
if (j < len (v) - 1 and
v[j] = = v[j + 1 ]):
w.append( 2 * v[j])
j + = 1
else :
w.append(v[j])
j + = 1
for j in range (n):
a[i][j] = 0
j = n - 1
for it in w:
a[i][j] = it
j - = 1
elif (d[ 0 ] = = 'l' ):
for i in range (n):
v = []
w = []
for j in range (n):
if (a[i][j]):
v.append(a[i][j])
j = 0
while (j < len (v)):
if (j < len (v) - 1 and
v[j] = = v[j + 1 ]):
w.append( 2 * v[j])
j + = 1
else :
w.append(v[j])
j + = 1
for j in range (n):
a[i][j] = 0
j = 0
for it in w:
a[i][j] = it
j + = 1
elif (d[ 0 ] = = 'd' ):
for i in range (n):
v = []
w = []
for j in range (n - 1 , - 1 , - 1 ):
if (a[j][i]):
v.append(a[j][i])
j = 0
while (j < len (v)):
if (j < len ( v) - 1 and
v[j] = = v[j + 1 ]):
w.append( 2 * v[j])
j + = 1
else :
w.append(v[j])
j + = 1
for j in range (n):
a[j][i] = 0
j = n - 1
for it in w:
a[j][i] = it
j - = 1
elif (d[ 0 ] = = 'u' ):
for i in range (n):
v = []
w = []
for j in range (n):
if (a[j][i]):
v.append(a[j][i])
j = 0
while (j < len (v)):
if (j < len (v) - 1 and
v[j] = = v[j + 1 ]):
w.append( 2 * v[j])
j + = 1
else :
w.append(v[j])
j + = 1
for j in range (n):
a[j][i] = 0
j = 0
for it in w:
a[j][i] = it
j + = 1
if __name__ = = "__main__" :
d = [ "l" ] * 2
n = 5
a = [ [ 32 , 3 , 3 , 3 , 3 ],
[ 0 , 0 , 1 , 0 , 0 ],
[ 10 , 10 , 8 , 1 , 2 ],
[ 0 , 0 , 0 , 0 , 1 ],
[ 4 , 5 , 6 , 7 , 8 ] ]
moveMatrix(d, n, a)
for i in range (n):
for j in range (n):
print (a[i][j], end = " " )
print ()
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void moveMatrix( char d, int n,
int [, ] a)
{
if (d == 'r' ) {
for ( int i = 0; i < n; i++) {
List< int > v = new List< int >();
List< int > w = new List< int >();
int j;
for (j = n - 1; j >= 0; j--) {
if (a[i, j] != 0)
v.Add(a[i, j]);
}
for (j = 0; j < v.Count; j++) {
if (j < v.Count - 1 && v[j] == v[j + 1]) {
w.Add(2 * v[j]);
j++;
}
else
w.Add(v[j]);
}
for (j = 0; j < n; j++)
a[i, j] = 0;
j = n - 1;
for ( int it = 0; it < w.Count; it++)
a[i, j--] = w[it];
}
}
else if (d == 'l' ) {
for ( int i = 0; i < n; i++) {
List< int > v = new List< int >();
List< int > w = new List< int >();
int j;
for (j = 0; j < n; j++) {
if (a[i, j] != 0)
v.Add(a[i, j]);
}
for (j = 0; j < v.Count; j++) {
if (j < v.Count - 1 && v[j] == v[j + 1]) {
w.Add(2 * v[j]);
j++;
}
else
w.Add(v[j]);
}
for (j = 0; j < n; j++)
a[i, j] = 0;
j = 0;
for ( int it = 0; it < w.Count; it++)
a[i, j++] = w[it];
}
}
else if (d == 'd' ) {
for ( int i = 0; i < n; i++) {
List< int > v = new List< int >();
List< int > w = new List< int >();
int j;
for (j = n - 1; j >= 0; j--) {
if (a[j, i] != 0)
v.Add(a[j, i]);
}
for (j = 0; j < v.Count; j++) {
if (j < v.Count - 1 && v[j] == v[j + 1]) {
w.Add(2 * v[j]);
j++;
}
else
w.Add(v[j]);
}
for (j = 0; j < n; j++)
a[j, i] = 0;
j = n - 1;
for ( int it = 0; it < w.Count; it++)
a[j--, i] = w[it];
}
}
else if (d == 'u' ) {
for ( int i = 0; i < n; i++) {
List< int > v = new List< int >();
List< int > w = new List< int >();
int j;
for (j = 0; j < n; j++) {
if (a[j, i] != 0)
v.Add(a[j, i]);
}
for (j = 0; j < v.Count; j++) {
if (j < v.Count - 1 && v[j] == v[j + 1]) {
w.Add(2 * v[j]);
j++;
}
else
w.Add(v[j]);
}
for (j = 0; j < n; j++)
a[j, i] = 0;
j = 0;
for ( int it = 0; it < w.Count; it++)
a[j++, i] = w[it];
}
}
}
static void Main()
{
char d = 'l' ;
int n = 5;
int [, ] a = new int [, ] { { 32, 3, 3, 3, 3 },
{ 0, 0, 1, 0, 0 },
{ 10, 10, 8, 1, 2 },
{ 0, 0, 0, 0, 1 },
{ 4, 5, 6, 7, 8 } };
moveMatrix(d, n, a);
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
Console.Write(a[i, j] + " " );
Console.WriteLine();
}
}
}
|
PHP
<?php
$MAX = 50;
function moveMatrix( $d , $n , & $a )
{
global $MAX ;
if ( $d [0] == 'r' )
{
for ( $i = 0; $i < $n ; $i ++)
{
$v = array ();
$w = array ();
$j = 0;
for ( $j = $n - 1; $j >= 0; $j --)
{
if ( $a [ $i ][ $j ])
array_push ( $v , $a [ $i ][ $j ]);
}
for ( $j = 0; $j < count ( $v ); $j ++)
{
if ( $j < count ( $v ) - 1 &&
$v [ $j ] == $v [ $j + 1])
{
array_push ( $w , 2 * $v [ $j ]);
$j ++;
}
else
array_push ( $w , $v [ $j ]);
}
for ( $j = 0; $j < $n ; $j ++)
$a [ $i ][ $j ] = 0;
$j = $n - 1;
for ( $it = 0; $it != count ( $w ); $it ++)
$a [ $i ][ $j --] = $w [ $it ];
}
}
else if ( $d [0] == 'l' )
{
for ( $i = 0; $i < $n ; $i ++)
{
$v = array (); $w = array ();
$j = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $a [ $i ][ $j ])
array_push ( $v ,
$a [ $i ][ $j ]);
}
for ( $j = 0; $j < count ( $v ); $j ++)
{
if ( $j < count ( $v ) - 1 &&
$v [ $j ] == $v [ $j + 1])
{
array_push ( $w , 2 * $v [ $j ]);
$j ++;
}
else
array_push ( $w , $v [ $j ]);
}
for ( $j = 0; $j < $n ; $j ++)
$a [ $i ][ $j ] = 0;
$j = 0;
for ( $it = 0; $it != count ( $w ); $it ++)
$a [ $i ][ $j ++] = $w [ $it ];
}
}
else if ( $d [0] == 'd' )
{
for ( $i = 0; $i < $n ; $i ++)
{
$v = array (); $w = array ();
$j = 0;
for ( $j = $n - 1; $j >= 0; $j --)
{
if ( $a [ $j ][ $i ])
array_push ( $v , $a [ $j ][ $i ]);
}
for ( $j = 0; $j < count ( $v ); $j ++)
{
if ( $j < count ( $v ) - 1 &&
$v [ $j ] == $v [ $j + 1])
{
array_push ( $w , 2 * $v [ $j ]);
$j ++;
}
else
array_push ( $w , $v [ $j ]);
}
for ( $j = 0; $j < $n ; $j ++)
$a [ $j ][ $i ] = 0;
$j = $n - 1;
for ( $it = 0; $it != count ( $w ); $it ++)
$a [ $j --][ $i ] = $w [ $it ];
}
}
else if ( $d [0] == 'u' )
{
for ( $i = 0; $i < $n ; $i ++)
{
$v = array (); $w = array ();
$j = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $a [ $j ][ $i ])
array_push ( $v ,
$a [ $j ][ $i ]);
}
for ( $j = 0; $j < count ( $v ); $j ++)
{
if ( $j < count ( $v ) - 1 &&
$v [ $j ] == $v [ $j + 1])
{
array_push ( $w , 2 * $v [ $j ]);
$j ++;
}
else
array_push ( $w , $v [ $j ]);
}
for ( $j = 0; $j < $n ; $j ++)
$a [ $j ][ $i ] = 0;
$j = 0;
for ( $it = 0; $it != count ( $w ); $it ++)
$a [ $j ++][ $i ] = $w [ $it ];
}
}
}
$d = array ( "l" );
$n = 5;
$a = array ( array (32, 3, 3, 3, 3),
array (0, 0, 1, 0, 0),
array (10, 10, 8, 1, 2),
array (0, 0, 0, 0, 1),
array (4, 5, 6, 7, 8));
moveMatrix( $d , $n , $a );
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
echo ( $a [ $i ][ $j ]. " " );
echo ( "\n" );
}
?>
|
Javascript
<script>
function moveMatrix(d,n,a)
{
if (d == 'r' ) {
for (let i = 0; i < n; i++) {
let v = [] ;
let w = [] ;
let j;
for (j = n - 1; j >= 0; j--) {
if (a[i][j] != 0)
v.push(a[i][j]);
}
for (j = 0; j < v.length; j++) {
if (j < v.length - 1 && v[j] == v[j+1]) {
w.push(2 * v[j]);
j++;
}
else
w.push(v[j]);
}
for (j = 0; j < n; j++)
a[i][j] = 0;
j = n - 1;
for (let it = 0; it < w.length; it++)
a[i][j--] = w[it];
}
}
else if (d == 'l' ) {
for (let i = 0; i < n; i++) {
let v = [] ;
let w = [] ;
let j;
for (j = 0; j < n; j++) {
if (a[i][j] != 0)
v.push(a[i][j]);
}
for (j = 0; j < v.length; j++) {
if (j < v.length - 1 && v[j] == v[j+1]) {
w.push(2 * v[j]);
j++;
}
else
w.push(v[j]);
}
for (j = 0; j < n; j++)
a[i][j] = 0;
j = 0;
for (let it = 0; it < w.length; it++)
a[i][j++] = w[it];
}
}
else if (d == 'd' ) {
for (let i = 0; i < n; i++) {
let v = [];
let w = [];
let j;
for (j = n - 1; j >= 0; j--) {
if (a[j][i] != 0)
v.push(a[j][i]);
}
for (j = 0; j < v.length; j++) {
if (j < v.length - 1 && v[j] == v[j+1]) {
w.push(2 * v[j]);
j++;
}
else
w.push(v[j]);
}
for (j = 0; j < n; j++)
a[j][i] = 0;
j = n - 1;
for (let it = 0; it < w.length; it++)
a[j--][i] = w[it];
}
}
else if (d == 'u' ) {
for (let i = 0; i < n; i++) {
let v = [];
let w = [];
let j;
for (j = 0; j < n; j++) {
if (a[j][i] != 0)
v.push(a[j][i]);
}
for (j = 0; j < v.length; j++) {
if (j < v.length - 1 && v[j] == v[j+1]) {
w.push(2 * v[j]);
j++;
}
else
w.push(v[j]);
}
for (j = 0; j < n; j++)
a[j][i] = 0;
j = 0;
for (let it = 0; it < w.length; it++)
a[j++][i] = w[it];
}
}
}
let d = 'l' ;
let n = 5;
let a = [ [ 32, 3, 3, 3, 3 ],
[ 0, 0, 1, 0, 0 ],
[ 10, 10, 8, 1, 2 ],
[ 0, 0, 0, 0, 1 ],
[ 4, 5, 6, 7, 8 ] ]
moveMatrix(d, n, a);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++)
document.write(a[i][j] + " " );
document.write( "<br>" );
}
</script>
|
Output:
32 6 6 0 0
1 0 0 0 0
20 8 1 2 0
1 0 0 0 0
4 5 6 7 8
Time Complexity: O(n2)
Auxiliary Space: O(n), since n extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Aug, 2022
Like Article
Save Article