Four boys are playing a game with a ball. In each turn, the player (who has the ball currently) passes it to a **different** player randomly. Bob always starts the game. The task is to find in how many ways the ball will come back to Bob after **N** passes.

**Examples:**

Input:N = 3

Output:6

Here are all the possible ways:

Bob -> Boy1 -> Boy2 -> Bob

Bob -> Boy1 -> Boy3 -> Bob

Bob -> Boy2 -> Boy1 -> Bob

Bob -> Boy2 -> Boy3 -> Bob

Bob -> Boy3 -> Boy1 -> Bob

Bob -> Boy3 -> Boy2 -> Bob

Input:N = 10

Output:14763

**Approach:** Let the number of sequences that get back to Bob after **N** passes are **P(N)**. The are two cases, either pass **N – 2** is to Bob or it is not. Note that Bob can’t have the ball at **(N – 1) ^{th}** pass because then he won’t have the ball at the

**N**pass.

^{th}**Case 1:**If pass**N – 2**is to Bob then the pass**N – 1**can be to any of the other 3 boys. Thus, the number of such sequences is**3 * P(N – 2)**.**Case 2:**If pass**N – 2**is not to Bob then pass**N – 1**is to one of the 2 boys other than Bob and the one who got the ball in hand. So, substitute Bob for the receiver of pass**N – 1**, and get a unique**N – 1**sequence. So, the number of such sequences are**2 * P(N – 1)**.

Hence the recurrence relation will be **P(N) = 2 * P(N – 1) + 3 * P(N – 2)** where **P(0) = 1** and **P(1) = 0**.

After solving the recurrence relation, **P(N) = (3 ^{N} + 3 * (-1^{N})) / 4**

Below is the implementation of the above approach:

## C++

`// Function to return the number of ` `// sequences that get back to Bob ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `numSeq(` `int` `n) ` `{ ` ` ` `return` `(` `pow` `(3, n) + 3 * ` `pow` `(-1, n)) / 4; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 10; ` ` ` `printf` `(` `"%d"` `, numSeq(N)); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Mohit kumar ` |

*chevron_right*

*filter_none*

## Java

`// Function to return the number of ` `// sequences that get back to Bob ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `numSeq(` `int` `n) ` `{ ` ` ` `return` `(` `int` `) ((Math.pow(` `3` `, n) + ` `3` `* ` ` ` `Math.pow(-` `1` `, n)) / ` `4` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `10` `; ` ` ` `System.out.printf(` `"%d"` `, numSeq(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Function to return the number of ` `# sequences that get back to Bob ` `def` `numSeq(n): ` ` ` `return` `(` `pow` `(` `3` `, n) ` `+` `3` `*` `pow` `(` `-` `1` `, n))` `/` `/` `4` ` ` `# Driver code ` `N ` `=` `10` `print` `(numSeq(N)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `// Function to return the number of ` `// sequences that get back to Bob ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `numSeq(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `) ((Math.Pow(3, n) + 3 * ` ` ` `Math.Pow(-1, n)) / 4); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 10; ` ` ` `Console.WriteLine(numSeq(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

14763

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 come back to starting point in a circular tour
- Ways to choose balls such that at least one ball is chosen
- Find out the correct position of the ball after shuffling
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Permutations of n things taken all at a time with m things never come together
- Puzzle | Weight of Heavy Ball
- Python | Catching the ball game
- Find the position of box which occupies the given ball
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Back-Face Detection Method
- Back-off Algorithm for CSMA/CD
- Deep Neural net with forward and back propagation from scratch - Python
- Write Through and Write Back in Cache
- back driver method - Selenium Python
- How to move back and forward in History using Selenium Python ?
- PyQt5 QCalendarWidget - Making Cursor Shape back to normal
- LSTM - Derivation of Back propagation through time
- PyQt5 QCommandLinkButton - Setting Default Cursor Back
- Python | Ways to split a string in different ways
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts

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.