Update appNew update is available. Click here to update.
About
I love solving a problem than creating one :) ~Byahut
Indian Institute of Technology (BHU), Varanasi 2025
My Stats
EXP gained
yellow-spark
30166
Level
7 (Expert)
Community stats
Discussions
0
Upvotes
0
Know more
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
805
Total problems solved
692
Easy
93
Moderate
20
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

1 day

Longest streak:

112 days

Less

More

Achievements
5
Ronin
Topics
Sorting
Tries
Strings
+ 2 more
3
Samurai
Topics
Arrays
Linked List
+ 1 more
Discussions
Easiest Approach || Array Implementation
General discussion

    // 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;

profile
nkb02
Published On 09-Sep-2023
75 views
0 replies
2 upvotes
Easiest Approach || Array Implementation
General discussion

    // 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;

profile
nkb02
Published On 09-Sep-2023
75 views
0 replies
2 upvotes