Current streak:
1 day
Longest streak:
112 days
Less
More
// n →rows
int n = mat.size();
// m →columns
int m = mat[0].size();
int total = 0;
// for traversal in all 4 - directions i.e. up, down, left, right
int drow[] ={ 0, 0, 1, -1};
int dcol[] ={ 1, -1, 0, 0};
for(int i =0; i< n; i++){
for(int j = 0; j< m; j++){
// if it is zero
if(mat[i][j] == 0){
for(int x= 0; x< 4; x++){
int nrow = i + drow[x];
int ncol = j + dcol[x];
// check validity and then if it is 1
if(nrow>= 0 && nrow< n && ncol>=0 && ncol < m && mat[nrow][ncol] == 1)
total++;
}
}
}
}
return total;
// n →rows
int n = mat.size();
// m →columns
int m = mat[0].size();
int total = 0;
// for traversal in all 4 - directions i.e. up, down, left, right
int drow[] ={ 0, 0, 1, -1};
int dcol[] ={ 1, -1, 0, 0};
for(int i =0; i< n; i++){
for(int j = 0; j< m; j++){
// if it is zero
if(mat[i][j] == 0){
for(int x= 0; x< 4; x++){
int nrow = i + drow[x];
int ncol = j + dcol[x];
// check validity and then if it is 1
if(nrow>= 0 && nrow< n && ncol>=0 && ncol < m && mat[nrow][ncol] == 1)
total++;
}
}
}
}
return total;