Given that **N** people are sitting in a circular queue numbered from **1** to **N**, the task is to count the number of ways to select a subset of them such that no two consecutive are sitting together. The answer could be large so compute the answer modulo **10 ^{9} + 7**.

**Note**that an empty subset is also a valid subset.

**Examples:**

Input:N = 2

Output:3

All possible subsets are {}, {1} and {2}.

Input:N = 3

Output:4

**Approach:** Let’s find the answer for small values of **N**.

**N = 1** -> All possible subset are **{}, {1}**.

**N = 2** -> All possible subset are **{}, {1}, {2}**.

**N = 3** -> All possible subset are **{}, {1}, {2}, {3}**.

**N = 4** -> All possible subset are **{}, {1}, {2}, {3}, {4}, {1, 3}, {2, 4}**.

So the sequence will be **2, 3, 4, 7, …**

When **N = 5** the count will be **11** and if **N = 6** then the count will be **18**.

It can now be observed that the sequence is similar to a Fibonacci series starting from the second term with the first two terms as 3 and 4.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long ` ` ` `const` `ll N = 10000; ` `const` `ll MOD = 1000000007; ` ` ` `ll F[N]; ` ` ` `// Function to pre-compute the sequence ` `void` `precompute() ` `{ ` ` ` ` ` `// For N = 1 the answer will be 2 ` ` ` `F[1] = 2; ` ` ` ` ` `// Starting two terms of the sequence ` ` ` `F[2] = 3; ` ` ` `F[3] = 4; ` ` ` ` ` `// Comute the rest of the sequence ` ` ` `// with the relation ` ` ` `// F[i] = F[i - 1] + F[i - 2] ` ` ` `for` `(` `int` `i = 4; i < N; i++) ` ` ` `F[i] = (F[i - 1] + F[i - 2]) % MOD; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` ` ` `// Pre-compute the sequence ` ` ` `precompute(); ` ` ` ` ` `cout << F[n]; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` `static` `int` `N = ` `10000` `; ` `static` `int` `MOD = ` `1000000007` `; ` ` ` `static` `int` `[]F = ` `new` `int` `[N]; ` ` ` `// Function to pre-compute the sequence ` `static` `void` `precompute() ` `{ ` ` ` ` ` `// For N = 1 the answer will be 2 ` ` ` `F[` `1` `] = ` `2` `; ` ` ` ` ` `// Starting two terms of the sequence ` ` ` `F[` `2` `] = ` `3` `; ` ` ` `F[` `3` `] = ` `4` `; ` ` ` ` ` `// Comute the rest of the sequence ` ` ` `// with the relation ` ` ` `// F[i] = F[i - 1] + F[i - 2] ` ` ` `for` `(` `int` `i = ` `4` `; i < N; i++) ` ` ` `F[i] = (F[i - ` `1` `] + F[i - ` `2` `]) % MOD; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `n = ` `8` `; ` ` ` ` ` `// Pre-compute the sequence ` ` ` `precompute(); ` ` ` ` ` `System.out.println(F[n]); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` `N ` `=` `10000` `; ` `MOD ` `=` `1000000007` `; ` ` ` `F ` `=` `[` `0` `] ` `*` `N; ` ` ` `# Function to pre-compute the sequence ` `def` `precompute(): ` ` ` ` ` `# For N = 1 the answer will be 2 ` ` ` `F[` `1` `] ` `=` `2` `; ` ` ` ` ` `# Starting two terms of the sequence ` ` ` `F[` `2` `] ` `=` `3` `; ` ` ` `F[` `3` `] ` `=` `4` `; ` ` ` ` ` `# Comute the rest of the sequence ` ` ` `# with the relation ` ` ` `# F[i] = F[i - 1] + F[i - 2] ` ` ` `for` `i ` `in` `range` `(` `4` `,N): ` ` ` `F[i] ` `=` `(F[i ` `-` `1` `] ` `+` `F[i ` `-` `2` `]) ` `%` `MOD; ` ` ` `# Driver code ` `n ` `=` `8` `; ` ` ` `# Pre-compute the sequence ` `precompute(); ` `print` `(F[n]); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `N = 10000; ` `static` `int` `MOD = 1000000007; ` ` ` `static` `int` `[]F = ` `new` `int` `[N]; ` ` ` `// Function to pre-compute the sequence ` `static` `void` `precompute() ` `{ ` ` ` ` ` `// For N = 1 the answer will be 2 ` ` ` `F[1] = 2; ` ` ` ` ` `// Starting two terms of the sequence ` ` ` `F[2] = 3; ` ` ` `F[3] = 4; ` ` ` ` ` `// Comute the rest of the sequence ` ` ` `// with the relation ` ` ` `// F[i] = F[i - 1] + F[i - 2] ` ` ` `for` `(` `int` `i = 4; i < N; i++) ` ` ` `F[i] = (F[i - 1] + F[i - 2]) % MOD; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `n = 8; ` ` ` ` ` `// Pre-compute the sequence ` ` ` `precompute(); ` ` ` ` ` `Console.WriteLine(F[n]); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

47

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:

- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Count of sub-arrays whose elements can be re-arranged to form palindromes
- Check if an array can be Arranged in Left or Right Positioned Array
- Check if elements of an array can be arranged in a Circle with consecutive difference as 1
- Check if elements of an array can be arranged satisfying the given condition
- Check if elements of array can be arranged in AP, GP or HP
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Random number generator in arbitrary probability distribution fashion
- Convert array into Zig-Zag fashion
- Converting an array of integers into Zig-Zag fashion!
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count subsets having distinct even numbers
- Sum of all subsets of a set formed by first n natural numbers
- Sum of sum of all subsets of a set formed by first N natural numbers
- Closest sum partition (into two subsets) of numbers from 1 to n

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.