Given two integers N, M denoting N×M chessboard, the task is to count the number of ways a knight can reach (N, M) starting from (0, 0). Since the answer can be very large, print the answer modulo 109+7.
Example:
Input: N =3, M= 3
Output: 2
Explanation:
Two ways to reach (3, 3) form (0, 0) are as follows:
(0, 0) → (1, 2) → (3, 3)
(0, 0) → (2, 1) → (3, 3)
Input: N=4, M=3
Output: 0
Explanation: No possible way exists to reach (4, 3) form (0, 0).
Approach: Idea here is to observe the pattern that each move increments the value of the x-coordinate + value of y-coordinate by 3. Follow the steps below to solve the problem.
- If (N + M) is not divisible by 3 then no possible path exists.
- If (N + M) % 3==0 then count the number of moves of type (+1, +2) i.e, X and count the number of moves of type (+2, +1) i.e, Y.
- Find the equation of the type (+1, +2) i.e. X + 2Y = N
- Find the equation of the type (+2, +1) i.e. 2X + Y = M
- Find the calculated values of X and Y, if X < 0 or Y < 0, then no possible path exists.
- Otherwise, calculate (X+Y)CY.
Below is the implementation of the above approach:
C++14
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; const int Mod = 1e9 + 7; // Function to return X^Y % Mod int power(int X, int Y, int Mod) { // Base Case if (Y == 0) return 1; int p = power(X, Y / 2, Mod) % Mod; p = (p * p) % Mod; if (Y & 1) { p = (X * p) % Mod; } return p; } // Function to return the // inverse of factorial of N int Inversefactorial(int N) { // Base case if (N <= 0) return 1; int fact = 1; for (int i = 1; i <= N; i++) { fact = (fact * i) % Mod; } return power(fact, Mod - 2, Mod); } // Function to return factorial // of n % Mod int factorial(int N) { // Base case if (N <= 0) return 1; int fact = 1; for (int i = 1; i <= N; i++) { fact = (fact * i) % Mod; } return fact; } // Function to return the value // of n! / (( n- k)! * k!) int nck(int N, int K) { int factN = factorial(N); int inv = Inversefactorial(K); int invFact = Inversefactorial(N - K); return (((factN * inv) % Mod) * invFact) % Mod; } // Function to return the count of // ways to reach (n, m) from (0, 0) int TotalWaYs(int N, int M) { // If (N + M) % 3 != 0 if ((N + M) % 3 != 0) // No possible way exists return 0; // Calculate X and Y from the // equations X + 2Y = N // and 2X + Y == M int X = N - (N + M) / 3; int Y = M - (N + M) / 3; if (X < 0 || Y < 0) return 0; return nck(X + Y, Y); } // Driver Code int main() { int N = 3, M = 3; cout << TotalWaYs(N, M); return 0; } |
Java
// Java Program to implement // the above approach import java.util.*; class GFG{ static int Mod = (int) (1e9 + 7); // Function to return X^Y % Mod static int power(int X, int Y, int Mod) { // Base Case if (Y == 0) return 1; int p = power(X, Y / 2, Mod) % Mod; p = (p * p) % Mod; if ((Y & 1) != 0) { p = (X * p) % Mod; } return p; } // Function to return the // inverse of factorial of N static int Inversefactorial(int N) { // Base case if (N <= 0) return 1; int fact = 1; for (int i = 1; i <= N; i++) { fact = (fact * i) % Mod; } return power(fact, Mod - 2, Mod); } // Function to return factorial // of n % Mod static int factorial(int N) { // Base case if (N <= 0) return 1; int fact = 1; for (int i = 1; i <= N; i++) { fact = (fact * i) % Mod; } return fact; } // Function to return the value // of n! / (( n- k)! * k!) static int nck(int N, int K) { int factN = factorial(N); int inv = Inversefactorial(K); int invFact = Inversefactorial(N - K); return (((factN * inv) % Mod) * invFact) % Mod; } // Function to return the count of // ways to reach (n, m) from (0, 0) static int TotalWaYs(int N, int M) { // If (N + M) % 3 != 0 if (((N + M) % 3 )!= 0) // No possible way exists return 0; // Calculate X and Y from the // equations X + 2Y = N // and 2X + Y == M int X = N - (N + M) / 3; int Y = M - (N + M) / 3; if (X < 0 || Y < 0) return 0; return nck(X + Y, Y); } // Driver Code public static void main(String[] args) { int N = 3, M = 3; System.out.print(TotalWaYs(N, M)); } } // This code is contributed by Rohit_ranjan |
Python3
# Python3 program to implement # above approach Mod = int(1e9 + 7) # Function to return X^Y % Mod def power(X, Y, Mod): # Base case if Y == 0: return 1 p = power(X, Y // 2, Mod) % Mod p = (p * p) % Mod if Y & 1: p = (X * p) % Mod return p # Function to return the # inverse of factorial of N def Inversefactorial(N): # Base case if N <= 0: return 1 fact = 1 for i in range(1, N + 1): fact = (fact * i) % Mod return power(fact, Mod - 2, Mod) # Function to return factorial # of n % Mod def factorial(N): # Base case if N <= 0: return 1 fact = 1 for i in range(1, N + 1): fact = (fact * i) % Mod return fact # Function to return the value # of n! / (( n- k)! * k!) def nck(N, K): factN = factorial(N) inv = Inversefactorial(K) invFact = Inversefactorial(N - K) return (((factN * inv) % Mod) * invFact) % Mod # Function to return the count of # ways to reach (n, m) from (0, 0) def TotalWays(N, M): # If (N + M) % 3 != 0 if (N + M) % 3 != 0: # No possible way exists return 0 # Calculate X and Y from the # equations X + 2Y = N # and 2X + Y == M X = N - (N + M) // 3 Y = M - (N + M) // 3 if X < 0 or Y < 0: return 0 return nck(X + Y, Y) # Driver code N, M = 3, 3 print(TotalWays(N, M)) # This code is contributed by Stuti Pathak |
C#
// C# program to implement // the above approach using System; class GFG{ static int Mod = (int)(1e9 + 7); // Function to return X^Y % Mod static int power(int X, int Y, int Mod) { // Base Case if (Y == 0) return 1; int p = power(X, Y / 2, Mod) % Mod; p = (p * p) % Mod; if ((Y & 1) != 0) { p = (X * p) % Mod; } return p; } // Function to return the // inverse of factorial of N static int Inversefactorial(int N) { // Base case if (N <= 0) return 1; int fact = 1; for(int i = 1; i <= N; i++) { fact = (fact * i) % Mod; } return power(fact, Mod - 2, Mod); } // Function to return factorial // of n % Mod static int factorial(int N) { // Base case if (N <= 0) return 1; int fact = 1; for(int i = 1; i <= N; i++) { fact = (fact * i) % Mod; } return fact; } // Function to return the value // of n! / (( n- k)! * k!) static int nck(int N, int K) { int factN = factorial(N); int inv = Inversefactorial(K); int invFact = Inversefactorial(N - K); return (((factN * inv) % Mod) * invFact) % Mod; } // Function to return the count of // ways to reach (n, m) from (0, 0) static int TotalWaYs(int N, int M) { // If (N + M) % 3 != 0 if (((N + M) % 3 ) != 0) // No possible way exists return 0; // Calculate X and Y from the // equations X + 2Y = N // and 2X + Y == M int X = N - (N + M) / 3; int Y = M - (N + M) / 3; if (X < 0 || Y < 0) return 0; return nck(X + Y, Y); } // Driver Code public static void Main(String[] args) { int N = 3, M = 3; Console.Write(TotalWaYs(N, M)); } } // This code is contributed by Amit Katiyar |
2
Time Complexity: O(X + Y + log(mod)).
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum steps to reach target by a Knight | Set 1
- Minimum steps to reach target by a Knight | Set 2
- Puzzle | Can a Knight reach bottom from top by visiting all squares
- Possible moves of knight
- Find minimum moves to reach target on an infinite line
- Minimum moves to reach target on a infinite line | Set 2
- The Knight's tour problem | Backtracking-1
- Probability of Knight to remain in the chessboard
- Warnsdorff's algorithm for Knight’s tour problem
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Count ways to reach the n'th stair
- Count number of ways to reach a given score in a Matrix
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Count ways to reach the nth stair using step 1, 2 or 3
- Number of ways to reach Nth floor by taking at-most K leaps
- Number of ways to reach the end of matrix with non-zero AND value
- Find the number of ways to reach Kth step in stair case
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Number of ways to reach (X, Y) in a matrix starting from the origin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

