-
Notifications
You must be signed in to change notification settings - Fork 1
/
25_December.cpp
48 lines (48 loc) · 1.26 KB
/
25_December.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
long long int MissingNo(vector<vector<int> >& matrix) {
// Code here
int n = matrix.size(),k,l;
vector<long long int> row(n,0),col(n,0);
long long int d1=0,d2=0,temp,ele;
for(int i=0;i<n;i++){
long long int sum=0;
for(int j=0;j<n;j++){
sum+=matrix[i][j];
if(matrix[i][j]==0){
k=i;
l=j;
}
col[j]+=matrix[i][j];
}
row[i]=sum;
}
if(k==0){
temp=row[1];
}
else{
temp=row[0];
}
for(int i=0;i<n;i++){
if(i==k){
ele = temp-row[i];
}
else if(row[i]!=temp)return -1;
}
for(int i=0;i<n;i++){
if(i==l){
if(ele+col[i]!=temp)return -1;
}
else if(col[i]!=temp)return -1;
}
for(int i=0;i<n;i++){
d1+=matrix[i][i];
d2+=matrix[i][n-1-i];
if(matrix[i][i]==0){
d1+=ele;
}
if(matrix[i][n-1-i]==0){
d2+=ele;
}
}
if((d1!=temp) or (d2!=temp) or (ele<=0))return -1;
return ele;
}