Black and White | Practice | GeeksforGeeks

Given the chessboard dimensions. Find out the number of ways we can place a black and a white Knight on this chessboard such that they cannot attack each other.
Note:
The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.
Example 1:
Input:
N = 2, M = 2
Output: 12 

Example 2:
Input:
N = 2, M = 3
Output: 26
Your Task:
Your task is to complete the function numOfWays() which takes the chessboard dimensions N and M as inputs and returns the number of ways we can place 2 Knights on this chessboard such that they cannot attack each other. Since this number can be very large, return it modulo 109+7.
Expected Time Complexity: O(N*M).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N * M <= 105
0
debajyoti dev1 month ago
What Rubbish nowhere mentioned what M and N !
 
0
vinamrajha2 months ago
bool issafe(int i, int j, int N, int M){   return(i>=0 && i<N && j>=0 && j<M);}long long numOfWays(int N, int M){   // write code here   long long d  = 1e9+7;   long long count =0;   int dx[8] = {1,-1,1,-1,2,2,-2,-2}; // all moves of knight   int dy[8] = {2,2,-2,-2,1,-1,1,-1};   for(int i =0; i<N; ++i){       for(int j =0; j<M; ++j){           for(int k =0; k<8; k++){               int xdx = i + dx[k];               int ydy = j + dy[k];               if(issafe(xdx, ydy, N, M)) count--;           }           count +=(N*M - 1); // remaining pos where we can put                                                other knight.            count %= d;           // prompt by the Q.       }   }   return count;}
+1
parikhsharan62 months ago
class BlackAndWhite{   static final long MOD = (long)Math.pow(10, 9) + 7;   //Function to find out the number of ways we can place a black and a    //white Knight on this chessboard such that they cannot attack each other.   static long numOfWays(int N, int M)   {       int[][] moves = new int[][]{           {-1, 2},           {1, 2},           {-1, -2},           {1, -2},           {-2, -1},           {-2, 1},           {2, -1},           {2, 1}       };              long answer = 0;              for(int i = 0; i < N; i++) {           for(int j = 0; j < M; j++) {               long count = 0;                              for(int k = 0; k < moves.length; k++) {                   if(isAttackPossible(N, M, i + moves[k][0], j + moves[k][1])) {                           count++;                   }               }               count++;               answer = (answer % MOD) + ((M*N) % MOD - (count % MOD)) % MOD;           }       }              return answer;   }      static boolean isAttackPossible(int N, int M, int i, int j) {       if(i < 0 || j < 0 || i >= N || j >= M) {           return false;       }              return true;   }}
0
amanpandey30072 months ago
long long numOfWays(int N, int M){   long long ans=0;   long long count=0;   long long mod=1e9+7;      for(int i=0;i<N;i++)   {                 for(int j=0;j<M;j++)       {              count=0;                      if(i+2<N and j+1<M)           count++;                      if(i+2<N and j-1>=0)           count++;                      if(i-2>=0 and j+1<M)           count++;                      if(i-2>=0 and j-1>=0)           count++;                      if(i+1<N and j+2<M)           count++;                      if(i+1<N and j-2>=0)           count++;                      if(i-1>=0 and j+2<M)           count++;                      if(i-1>=0 and j-2>=0)           count++;                      count++; // obivously cannot place  the two Knight at same place                      ans=ans%mod+(((N%mod)*(M%mod))%mod-count)%mod;                  }                 }      return ans;}
0
jayeshkhurana6853 months ago
Why is everyone taking modulus?
0
lindan1233 months ago
int mod =1000000007;
long long numOfWays(int n, int m)
{
    long long count=0;
    long long res=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            count=0;
            if(i-1>=0 && j-2>=0)
            {
                count++;
            }
            if(i+1<n && j-2>=0)
            {
                count++;
            }
            if(j+2<m && i+1<n)
            {
                count++;
            }
            if(j+2<m && i-1>=0)
            {
                count++;
            }
            if(i-2>=0 && j-1>=0)
            {
                count++;
            }
            if(i-2>=0 && j+1<m)
            {
                count++;
            }
            if(i+2<n && j-1>=0)
            {
                count++;
            }
            if(i+2<n && j+1<m)
            {
                count++;
            }
            count++;
            
            res = res%mod + (((n%mod)*(m%mod))%mod- count)%mod;
        }
    }
    return res;
}
Time Taken : 0.0
Cpp
0
anjalipatel94313 months ago
int count(int i,int j,int N,int M){        int countt=0;        if(i+2<N  && j+1<M ){        countt++;    }     if(i+2<N  && j-1>=0){   //down        countt++;    }          if(i-2>=0 && j+1<M ){  //up        countt++;    }     if(i-2>=0 && j-1>=0){        countt++;    }          if(i+1<N  && j+2<M ){   //right        countt++;    }     if(i-1>=0&& j+2<M ){        countt++;    }              if(i+1<N && j-2>=0){  //left        countt++;    }     if(i-1>=0 && j-2>=0){        countt++;    }        return (N*M)-countt-1;}long long numOfWays(int N, int M){   // write code here   int mod=1e9+7;   int sum=0;   for(int i=0;i<N;i++)    for(int j=0;j<M;j++){                sum+=count(i,j,N,M);        sum%=mod;    }        return sum;}
0
ayushnautiyal11104 months ago
C++ IMPLEMENTATION:)
Time Taken 0.6/1.3
😎😎😎😎
vector<pair<int,int>>v={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};const long long mod=1e9+7;bool isSafe(int i,int j,int N,int M){   return (i<N && j<M && i>=0 && j>=0);}long long numOfWays(int N, int M){   long long res=0;   for(int i=0;i<N;i++){       for(int j=0;j<M;j++){           int a=i;           int b=j;           for(auto x:v){               int dx=a+x.first;               int dy=b+x.second;               if(isSafe(dx,dy,N,M)==true){                   res--;               }           }           res+=N*M-1;           res%=mod;       }   }   return res;}
+1
anutiger6 months ago
long long numOfWays(int N, int M)
{
    int mod = 1e9 + 7;
    long long res = 0;
    int dx[] = {1,1,-1,-1,2,2,-2,-2};
    int dy[] = {2,-2,2,-2,1,-1,1,-1};
    for(int i = 0; i < N ; i ++){
        for(int j = 0; j < M ; j++){
            int x = i;
            int y = j;
            for(int k = 0 ; k < 8 ; k ++){
                int x1 = x + dx[k];
                int y1 = y + dy[k];
                if(x1 >= 0 and x1 < N and y1 >= 0  and y1 < M){
                    res--;
                }
            }
            res += N * M  - 1;
            res %= mod;
        }
    }
    return res;
}
+9
sachin shet6 months ago
I wonder why this is placed in backtracking section. This should be in Matrix Section.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.