- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ program to Replace Every Matrix Element with Maximum of GCD of Row or Column

In this method, we need to replace every element in the given matrix with the maximum Greatest Common Divisor (GCD) of that row and column.

**Let us look at some input scenarios −**

Suppose we are given a 2D matrix of dimensions m*n is;

Input: [[3, 2, 1, 4] [7, 6, 2, 8] [14, 20, 25, 17]];

In the above matrix, row 1 gcd(3, 2, 1, 4) = 1 and Column 2 gcd(3, 7, 14) = 1. So element 2 (1st row and 2nd column) becomes the maximum (1, 1) = 1. So on for all elements and our output should become

Result: [ [1, 2, 1, 1] [1, 2, 1, 1] [1, 2, 1, 1] ]

Looking at another 2D matrix of dimensions m*n;

Input: [[3, 2, 5, 4] [6, 6, 15, 8] [12, 20, 25, 16]];

In the above matrix, row 1 gcd(3, 2, 5, 4) = 1 and Column 2 gcd(2, 6, 20) = 2. So element 2 (1st row and 2nd column) becomes the maximum (1, 2) = 2. Repeating the process for all elements, our output will be −

Result: [ [3 2 5 4] [3 2 5 4] [3 2 5 4] ]

## Algorithm

Two functions GCDArrayRow and GCDArrayColumn are declared to find the greatest common divisor of elements in rows and columns respectively.

Find the maximum greatest common divisor of ith row and jth column to replace with the matrix[i, j] element.

Repeating this process for every element in the matrix, the updated matrix is printed as the output result.

## Example

Following is a C++ implementation to replace every matrix element with maximum of GCD of row or column −

In this program, we can create two arrays called *GCDArrayRow* and *GCDArrayColumn* in which we can store the gcd of respective rows and columns. After storing the gcd of all rows and columns, we can iterate over the array and compute the maximum for each element one by one.

#include <iostream> #include <vector> #include <algorithm> using namespace std; void solve(vector<vector<int>>& arr) { vector<int> GCDArrayRow(arr.size(), 0), GCDArrayColumn(arr[0].size(), 0); for(int i=0;i<arr.size();i++) { for(int j=0;j<arr[i].size();j++) { GCDArrayRow[i] = __gcd(GCDArrayRow[i], arr[i][j]); GCDArrayColumn[j] = __gcd(GCDArrayColumn[j], arr[i][j]); } } for(int i=0;i<arr.size();i++) { for(int j=0;j<arr[i].size();j++) { arr[i][j] = max(GCDArrayColumn[j], GCDArrayRow[i]); } } } void printArray(vector<vector<int>>& arr) { for(int i=0;i<arr.size();i++) { for(int j=0;j<arr[i].size();j++) { cout << arr[i][j] << " "; } cout << "

"; } cout << "

"; } int main() { vector<vector<int>> arr = { {2, 6, 4, 8}, {4, 6, 3, 9}, {7, 21, 28, 7} }; printArray(arr); solve(arr); printArray(arr); return 0; }

## Output

2 6 4 8 4 6 3 9 7 21 28 7 2 3 2 2 1 3 1 1 7 7 7 7

## Conclusion

So, we can observe that creating additional arrays of rows and columns does the job for us. We precompute the GCD of each row and column and then traverse finding maximum in both. The algorithm library calculates GCD in the built function in __gcd function in GCD. We can also write our GCD calculating function based on Euclid's Algorithm.

- Related Questions & Answers
- Python - Sort Matrix by Maximum Row element
- Maximum determinant of a matrix with every values either 0 or n in C++
- Find maximum element of each row in a matrix in C++
- Find maximum element of each column in a matrix in C++
- C++ program to find the Sum of each Row and each Column of a Matrix
- How to minus every element of a vector with every element of another vector in R?
- Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix using c++
- Maximum weight path ending at any element of last row in a matrix in C++
- Program to find number of elements in matrix follows row column criteria in Python
- Program to find smallest intersecting element of each row in a matrix in Python
- Count of elements of an array present in every row of NxM matrix in C++
- Maximum GCD of N integers with given product in C++
- Java program to find the GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers in C++
- Matrix row sum and column sum using C program
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program