Here is the problem link: COFDQ2
Here is my solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int m, n, sum=0, cn=0, cz=0;
cin >> m >> n;
long long int arr[m][n];
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
cin >> arr[i][j];
}
}
// Making the first element of every row to 1 no matter how the other
// elements will be changed according to the instruction.
for(int i=0; i<m; i++)
{
if(arr[i][0]!=1)
{
for(int j=0; j<n; j++)
{
arr[i][j]=1-arr[i][j];
}
}
}
// Now counting the number of 1 and number of 0 in every column.
// If number of 0 >number of 1 then, make all the 0 to 1 and 1 to 0.
for(int j=0; j<n; j++)
{
cn=0;
cz=0;
for(int i=0; i<m; i++)
{
if(arr[i][j]==0)
cz++;
else
cn++;
}
if(cz>cn)
{
for(int i=0; i<m; i++)
{
arr[i][j]=1-arr[i][j];
}
}
}
//Making sum of the integer value of every rows.
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
sum+=arr[i][j]*pow(2,n-j-1);
}
}
cout << sum << "\n";
return 0;
}
Need help!!!
Thanks in Advance.