Problem Link : CodeChef: Practical coding for everyone
First note that when And operation is done between two numbers then the 1’s come in the result are present in both operands. Hence the OR if all the given AND will give that particular number.
For more clarification, see the code.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int n;
cin >> n;
int arr[n][n];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
cin >> arr[i][j];
}
}
ll sum = 0;
for(int i = 0 ; i < n ; i++){
int x = 0;
for(int j = 0 ; j < n ; j++){
if(i != j){
x |= arr[i][j];
}
}
sum += x;
}
cout << sum << "\n";
return 0;
}