In this HackerEarth Bob's confusion problem solution You are given a 3 x 3 grid. Each number in the grid is represented by Cij where (i,j) denotes the square at the ith row from the top and jth column from the left.

Bob gets confused. According to him, there are six integers a1, a2, a3, b1, b2, b3 whose values are fixed and the number written in the square (i,j) is equal to ai + bj. You are required to determine whether Bob is correct or not.

HackerEarth Bob's confusion problem solution


HackerEarth Bob's confusion problem solution.

#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

void solve(){
int a[5],b[5],c[5][5];
rep(i,1,3) rep(j,1,3) cin>>c[i][j];
a[1]=0;
b[1]=c[1][1];
b[2]=c[1][2];
b[3]=c[1][3];
a[2]=c[2][2]-b[2];
a[3]=c[3][3]-b[3];
rep(i,1,3) rep(j,1,3){
if(a[i]+b[j]!=c[i][j]){
cout<<"NO\n";
return;
}
}
cout<<"YES\n";
}

signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}

Second solution

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e5 + 14;
int n, c[3][3];

int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
for (auto &i : c)
for (int &j : i)
cin >> j;
bool ok = true;
for (int i = 1; i < 3; ++i)
for (int j = 1; j < 3; ++j)
ok &= c[i][j] == c[0][j] + c[i][0] - c[0][0];
cout << (ok ? "YES\n" : "NO\n");
}
}