
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Replace Matrix Elements with Maximum GCD of Row or Column in C++
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 << "\n"; } cout << "\n"; } 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.