Given a 2D-array mat[][] of order M*N. The task is to replace every element of each row with the product of other elements of the same row.
Examples:
Input: mat[][] = {{3, 4, 1}, {6, 1, 7}, {2, 9, 8}}
Output:
4, 3, 12
7, 42, 6
72, 16, 18
Input: mat[][] = {{13, 4, 5}, {6, 11, 1}}
Output:
20, 65, 52
11, 6, 66
Approach: The idea is straightforward. Traverse the matrix row-wise and find the product of each row. In the second traversal find the replacement value for each cell of the row. Follow the steps below to solve the problem:
- Initialize the variables mul, i and j.
- Iterate over the range [0, M) using the variable i and perform the following tasks:
- Set the value of mul as 1.
- Iterate over the range [0, N) using the variable j and perform the following tasks:
- Set the value of mul as mul*mat[i][j].
- Iterate over the range [0, N) using the variable j and perform the following tasks:
- Set the value of mat[i][j] as mul/mat[i][j].
- After performing the above steps, print the values of mat[][] as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
const int M = 3, N = 3;
void showMatrix(vector<vector< int > >& mat)
{
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
cout << mat[i][j] << " " ;
}
cout << endl;
}
}
void ReplaceWithProduct(vector<vector< int > >& mat)
{
int mul, i, j;
for (i = 0; i < M; i++) {
mul = 1;
for (j = 0; j < N; j++)
mul *= mat[i][j];
for (j = 0; j < N; j++)
mat[i][j] = mul / mat[i][j];
}
showMatrix(mat);
}
int main()
{
vector<vector< int > > mat = { { 3, 6, 2 },
{ 1, 4, 9 },
{ 5, 3, 8 } };
ReplaceWithProduct(mat);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int M = 3 , N = 3 ;
static void showMatrix( int [][]mat)
{
int i, j;
for (i = 0 ; i < M; i++) {
for (j = 0 ; j < N; j++) {
System.out.print(mat[i][j] + " " );
}
System.out.println();
}
}
static void ReplaceWithProduct( int [][]mat)
{
int mul, i, j;
for (i = 0 ; i < M; i++) {
mul = 1 ;
for (j = 0 ; j < N; j++)
mul *= mat[i][j];
for (j = 0 ; j < N; j++)
mat[i][j] = mul / mat[i][j];
}
showMatrix(mat);
}
public static void main(String args[])
{
int [][]mat = { { 3 , 6 , 2 },
{ 1 , 4 , 9 },
{ 5 , 3 , 8 } };
ReplaceWithProduct(mat);
}
}
|
Python3
M = 3
N = 3
def showMatrix(mat):
for i in range ( 0 , M):
for j in range ( 0 , N):
print (mat[i][j], end = " " )
print ()
def ReplaceWithProduct(mat):
for i in range ( 0 , M):
mul = 1
for j in range ( 0 , N):
mul * = mat[i][j]
for j in range ( 0 , N):
mat[i][j] = mul / / mat[i][j]
showMatrix(mat)
if __name__ = = "__main__" :
mat = [[ 3 , 6 , 2 ],
[ 1 , 4 , 9 ],
[ 5 , 3 , 8 ]]
ReplaceWithProduct(mat)
|
C#
using System;
public class GFG
{
static int M = 3, N = 3;
static void showMatrix( int [,]mat)
{
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
Console.Write(mat[i,j] + " " );
}
Console.WriteLine();
}
}
static void ReplaceWithProduct( int [,]mat)
{
int mul, i, j;
for (i = 0; i < M; i++) {
mul = 1;
for (j = 0; j < N; j++)
mul *= mat[i,j];
for (j = 0; j < N; j++)
mat[i,j] = mul / mat[i,j];
}
showMatrix(mat);
}
public static void Main(String []args)
{
int [,]mat = { { 3, 6, 2 },
{ 1, 4, 9 },
{ 5, 3, 8 } };
ReplaceWithProduct(mat);
}
}
|
Javascript
<script>
let M = 3, N = 3;
function showMatrix(mat) {
let i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
document.write(mat[i][j] + " " );
}
document.write( '<br>' )
}
}
function ReplaceWithProduct(mat) {
let mul, i, j;
for (i = 0; i < M; i++) {
mul = 1;
for (j = 0; j < N; j++)
mul *= mat[i][j];
for (j = 0; j < N; j++)
mat[i][j] = mul / mat[i][j];
}
showMatrix(mat);
}
let mat = [[3, 6, 2],
[1, 4, 9],
[5, 3, 8]];
ReplaceWithProduct(mat);
</script>
|
Output: 12 6 18
36 9 4
24 40 15
Time Complexity: O(N*N)
Auxiliary Space: O(1)