Matrix Different Dimension HCF
Matrix Different Dimension HCF
PROBLEM STATEMENT :
The program must accept two integer matrices of size MxN and RxC as the input. The program must print the HCF of the integer values present in the intersecting area of the given two matrices.
Boundary Condition(s):
2 <= M, N, R, C <= 50
1 <= Matrix element value <= 1000
Input Format:The first line contains M and N separated by a space.
The next M lines, each contains N integers separated by a space.
The (M+2)th line contains R and C separated by a space.
The next R lines, each contains C integers separated by a space.
Output Format:
The lines, each contains the integer values separated by a space.
Example Input/Output 1:
Input: ()
2 6
4 4 3 2 2 4
8 8 6 3 8 5
3 3
4 2 6
7 2 6
5 1 8
Output:
4 2 3
1 2 6
Explanation:
The integers present in the intersecting area of the first matrix are highlighted below.
8 8 6 3 8 5
The integers present in the intersecting area of the second matrix are highlighted below.
7 2 6
5 1 8
The HCF of the integer values present in the intersecting area are given below
1 2 6
Example Input/Output 2:
Input: ()
3 4
4 8 5 1
1 8 4 8
4 6 5 9
4 5
1 1 15 5 14
7 6 16 10 15
3 9 18 16 1
7 10 18 12 14
Output:
1 1 5 1
1 2 4 2
1 3 1 1
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int hcf(int a,int b)
{
if(a==0)
return b;
return hcf(b%a,a);
}
int main()
{
int M,N,R,C;
scanf("%d %d",&M,&N);
int mat1[M][N];
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++) scanf("%d ",&mat1[i][j]);
}
scanf("%d %d",&R,&C);
int mat2[R][C];
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++) scanf("%d ",&mat2[i][j]);
}
int minRow = M<R ? M : R;
int minCol = N<C ? N : C;
for(int i=0;i<minRow;i++)
{
for(int j=0;j<minCol;j++)
{
printf("%d ",hcf(mat1[i][j],mat2[i][j]));
}
printf("\n");
}
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int r1 , c1 ;
cin >> r1 >> c1 ;
int mat1[r1][c1];
for(int i = 0 ; i<r1 ; i++)
{
for(int j = 0 ; j<c1 ; j++)
cin >> mat1[i][j];
}
int r2 , c2 ;
cin >> r2 >> c2 ;
int mat2[r2][c2];
for(int i = 0 ; i<r2 ; i++)
{
for(int j = 0 ; j<c2 ; j++)
cin >> mat2[i][j];
}
for(int i = 0 ; i<min(r1,r2) ; i++)
{
for(int j = 0 ; j<min(c1,c2) ; j++)
cout << __gcd(mat1[i][j] , mat2[i][j]) << " ";
cout << endl;
}
}
JAVA
import java.util.*;
public class Hello {
public static int gcd(int a,int b)
{
if (b==0)
return a;
else
return gcd(b,a%b);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int r1=sc.nextInt(),c1=sc.nextInt();
int[][] mat1 =new int[r1][c1];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
mat1[i][j]=sc.nextInt();
}
}
int r2=sc.nextInt(),c2=sc.nextInt();
int[][] mat2=new int [r2][c2];
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
mat2[i][j]=sc.nextInt();
}
}
for(int i=0;i<Math.min(r1,r2);i++)
{
for(int j=0;j<Math.min(c1,c2);j++)
{
System.out.print(gcd(mat1[i][j],mat2[i][j])+" ");
}
System.out.println();
}
}
}
PYTHON
import math
m,n=map(int,input().split())
l1=[list(map(int,input().split())) for _ in range(m)]
r,c=map(int,input().split())
l2=[list(map(int,input().split())) for _ in range(r)]
for i in range(min(m,r)):
for j in range(min(n,c)):
print(int(math.gcd(l1[i][j],l2[i][j])),end=' ')
print()
I didn't understand this explaination of case test please give any other brief explaination of these test cases.
ReplyDeleteNow, I highlighted the intersection part of the two given matrix.
DeleteThe gcd/hcf of the two numbers from two matrix from same position is replaced in the corresponding position.
In 2nd test case there is problem that as on given program it to take minimum row & min column from given 2 matrix but on output it give greater one & gcd is also different.
DeleteYeah. I corrected the output in case 2.
DeleteThank you for your queries.