Given a matrix of n rows and m columns. The task is to replace each matrix element with Greatest Common Divisor of its row or column, whichever is maximum. That is, for each element (i, j) replace it from GCD of i’th row or GCD of j’th row, whichever is greater.
Examples :
Input : mat[3][4] = {1, 2, 3, 3,
4, 5, 6, 6
7, 8, 9, 9}
Output : 1 1 3 3
1 1 3 3
1 1 3 3
For index (0,2), GCD of row 0 is 1, GCD of row 2 is 3.
So replace index (0,2) with 3 (3>1).
The idea is to us concept discussed here LCM of an array to find the GCD of row and column.
Using the brute force, we can traverse element of matrix, find the GCD of row and column corresponding to the element and replace it with maximum of both.
An Efficient method is to make two arrays of size n and m for row and column respectively. And store the GCD of each row and each column. An Array of size n will contain GCD of each row and array of size m will contain the GCD of each column. And replace each element with maximum of its corresponding row GCD or column GCD.
Below is the implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
#define R 3
#define C 4
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
void replacematrix( int mat[R][C], int n, int m)
{
int rgcd[R] = { 0 }, cgcd[C] = { 0 };
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
rgcd[i] = gcd(rgcd[i], mat[i][j]);
cgcd[j] = gcd(cgcd[j], mat[i][j]);
}
}
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
mat[i][j] = max(rgcd[i], cgcd[j]);
}
int main()
{
int m[R][C] =
{
1, 2, 3, 3,
4, 5, 6, 6,
7, 8, 9, 9,
};
replacematrix(m, R, C);
for ( int i = 0; i < R; i++)
{
for ( int j = 0; j < C; j++)
cout << m[i][j] << " " ;
cout<<endl;
}
return 0;
}
|
Java
import java .io.*;
class GFG
{
static int R = 3 ;
static int C = 4 ;
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a%b);
}
static void replacematrix( int [][]mat, int n, int m)
{
int []rgcd = new int [R] ;
int []cgcd = new int [C];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
rgcd[i] = gcd(rgcd[i], mat[i][j]);
cgcd[j] = gcd(cgcd[j], mat[i][j]);
}
}
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < m; j++)
mat[i][j] = Math.max(rgcd[i], cgcd[j]);
}
static public void main (String[] args){
int [][]m =
{
{ 1 , 2 , 3 , 3 },
{ 4 , 5 , 6 , 6 },
{ 7 , 8 , 9 , 9 },
};
replacematrix(m, R, C);
for ( int i = 0 ; i < R; i++)
{
for ( int j = 0 ; j < C; j++)
System.out.print(m[i][j] + " " );
System.out.println();
}
}
}
|
Python3
R = 3
C = 4
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def replacematrix(mat, n, m):
rgcd = [ 0 ] * R
cgcd = [ 0 ] * C
for i in range (n):
for j in range (m):
rgcd[i] = gcd(rgcd[i], mat[i][j])
cgcd[j] = gcd(cgcd[j], mat[i][j])
for i in range (n):
for j in range (m):
mat[i][j] = max (rgcd[i], cgcd[j])
if __name__ = = "__main__" :
m = [[ 1 , 2 , 3 , 3 ],
[ 4 , 5 , 6 , 6 ],
[ 7 , 8 , 9 , 9 ]]
replacematrix(m, R, C)
for i in range (R):
for j in range (C):
print ( m[i][j], end = " " )
print ()
|
C#
using System;
class GFG
{
static int R = 3;
static int C = 4;
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
static void replacematrix( int [,]mat, int n, int m)
{
int []rgcd = new int [R] ;
int []cgcd = new int [C];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
rgcd[i] = gcd(rgcd[i], mat[i,j]);
cgcd[j] = gcd(cgcd[j], mat[i,j]);
}
}
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
mat[i,j] = Math.Max(rgcd[i], cgcd[j]);
}
static public void Main (){
int [,]m =
{
{1, 2, 3, 3},
{4, 5, 6, 6},
{7, 8, 9, 9},
};
replacematrix(m, R, C);
for ( int i = 0; i < R; i++)
{
for ( int j = 0; j < C; j++)
Console.Write(m[i,j] + " " );
Console.WriteLine();
}
}
}
|
Javascript
<script>
let R = 3;
let C = 4;
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
function replacematrix(mat, n, m)
{
let rgcd = new Array(R);
rgcd.fill(0);
let cgcd = new Array(C);
cgcd.fill(0);
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
rgcd[i] = gcd(rgcd[i], mat[i][j]);
cgcd[j] = gcd(cgcd[j], mat[i][j]);
}
}
for (let i = 0; i < n; i++)
for (let j = 0; j < m; j++)
mat[i][j] = Math.max(rgcd[i], cgcd[j]);
}
let m = [ [1, 2, 3, 3],
[4, 5, 6, 6],
[7, 8, 9, 9] ];
replacematrix(m, R, C);
for (let i = 0; i < R; i++)
{
for (let j = 0; j < C; j++)
document.write(m[i][j] + " " );
document.write( "</br>" );
}
</script>
|
Output
1 1 3 3
1 1 3 3
1 1 3 3
Time Complexity : O(mn).
Auxiliary Space : O(m + n). Since m + n extra space has been taken.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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!