Matrix - Find Missing Element

Matrix - Find Missing Element 

PROBLEM STATEMENT :

    The program must accept an integer matrix of size R*C and an integer S as the input. Exactly one element in the given matrix is missing, which is denoted by the character M. The integer S represents the sum of all the integers in the column of the missing element. The program must find the value of M and replace it in the given matrix. Finally, the program must print the integer matrix with all the R*C integers as the output. 

Boundary Condition(s): 

2 <= R, C <= 50 

1 <= Matrix element value <= 1000 

1 <= S <= 10^5 

Input Format: 

The first line contains R and C separated by a space. 

The next R lines contain the matrix. The (R+2)nd line contains S. 

Output Format: 

The first R lines, each contains C integer values separated by a space. 

Example Input/Output 1: 
Input: () 
3 3 
2 5 8 
1 5 M 
9 7 6 
17 
Output: 2 5 8 1 5 3 9 7 6

Explanation: 

The missing element is present in the 3rd column. The sum of integers in the 3rd column is 17. So the value of the missing element M is 3 (17 - (8 + 6)). 

Example Input/Output 2: 
Input: () 
4 5 
62 25 69 62 80 
15 10 17 34 75 
99 M 76 79 23 
48 15 73 22 68 
100 
Output: 62 25 69 62 80 15 10 17 34 75 99 50 76 79 23 48 15 73 22 68




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int R,C,sum; scanf("%d %d",&R,&C); char mat[R][C][10]; int colSums[C]; memset(colSums,0,C*sizeof(int)); for(int i=0;i<R;i++) { for(int j=0;j<C;j++) { scanf("%s ",&mat[i][j]); colSums[j] += atoi(mat[i][j]); } } scanf("%d",&sum); for(int i=0;i<R;i++) { for(int j=0;j<C;j++) { printf("%d ",strcmp(mat[i][j],"M")==0 ? sum-colSums[j] : atoi(mat[i][j])); } printf("\n"); } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int r , c; cin >> r >> c ; string mat[r][c]; int ind = 0 ; for(int i = 0 ; i<r ; i++) { for(int j = 0 ; j<c ; j++) { cin >> mat[i][j]; if(isalpha(mat[i][j][0])) ind = j; } } int sum ; cin >> sum ; for(int i = 0 ; i<r ; i++) { if(!isalpha(mat[i][ind][0])) sum-=stoi(mat[i][ind]); } for(int i = 0 ; i<r ; i++) { for(int j = 0 ; j<c ; j++) { if(isalpha(mat[i][j][0])) cout << sum << " "; else cout << mat[i][j] << " "; } cout << endl; } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int r=sc.nextInt(),c=sc.nextInt(),sum,colSum=0,col=0,row=0; String m[][]=new String[r][c]; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ m[i][j]=sc.next(); if (m[i][j].equals("M")){ col=j; row=i; } } } sum=sc.nextInt(); for(int i=0;i<r;i++) if (i!=row) colSum+=Integer.parseInt(m[i][col]); m[row][col]=Integer.toString(sum-colSum); for(String[] i:m){ for (String j:i){ System.out.print(j+" "); } System.out.println(); } } }



PYTHON

       

r,c=map(int,input().split()) l=[input().split() for _ in range(r)] s=int(input()) for i in range(r): if 'M' in l[i]: row=i col=l[i].index('M') colSum=sum([int(l[i][col]) for i in range(r) if l[i][col]!='M']) l[row][col]=s-colSum for i in l: print(*i)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right