Given a number N, find the number of ways to construct an array of size N such that it contains only 1s and 0s but no two consecutive indexes have value 1 in them.
Examples:
Input : 2
Output : 3
Explanation:
For n=2, the possible arrays are:
{0, 1} {1, 0} {0, 0}
Input : 3
Output : 5
Explanation:
For n=3, the possible arrays are:
{0, 0, 0} {1, 0, 0} {0, 1, 0} {0, 0, 1} {1, 0, 1}
Naive Approach:
The basic brute force approach would be to construct all the possible ways that the array can be filled with 1s and 0s, and then checking if there are any two consecutive 1s in the array if there are, do not count those arrays.
Since each element has 2 possible values, 1 and 0, and there are n total elements, the total number of arrays without any restriction will be of exponential order i.e 2n.
Efficient Approach:
If we observe a bit closely, we can notice that there is a pattern forming in the input and output.
For n = 1, number of ways is 2 i.e. {0}, {1}
for n = 2, number of ways is 3
Similarly,
for n = 3 number of ways is 5
for n = 4 number of ways is 8
and so on…
Let T() be the function which gives the number of ways the array of size n can be filled, then we get the following recurrence relation
T(n) = T(n-1) + T(n-2)
And this is the recurrence relation of Fibonacci series<.
Hence, the output for any n is equal to the (n+2)th term of the Fibonacci series starting from 1.
i.e. 1 1 2 3 5 8 11…
So now we just need to compute the Fibonacci sequence up to the (n+2)th elements and that will be the answer.
Time complexity is O(n)
C++
// C++ implementation of the // above approach #include <iostream> using namespace std; // The total number of ways // is equal to the (n+2)th // Fibonacci term, hence we // only need to find that. int nth_term(int n) { int a = 1, b = 1, c = 1; // Construct fibonacci upto // (n+2)th term the first // two terms being 1 and 1 for (int i = 0; i < n; i++) { c = a + b; a = b; b = c; } return c; } // Driver Code int main() { // Take input n int n = 10; int c = nth_term(n); // printing output cout << c; } // This code is contributed by Sumit Sudhakar. |
Java
// Java implementation of the // above approach class Main { // The total number of ways // is equal to the (n+2)th // Fibonacci term, hence we // only need to find that. public static int nth_term(int n) { int a = 1, b = 1, c = 1; // Construct fibonacci upto // (n+2)th term the first // two terms being 1 and 1 for (int i = 0; i < n; i++) { c = a + b; a = b; b = c; } return c; } // Driver program public static void main(String[] args) { // Take input n int n = 10; int c = nth_term(n); // printing output System.out.println(c); } } |
Python3
# Python3 implementation of # the above approach # The total number of ways # is equal to the (n+2)th # Fibonacci term, hence we # only need to find that. def nth_term(n) : a = 1 b = 1 c = 1 # Construct fibonacci upto # (n+2)th term the first # two terms being 1 and 1 for i in range(0, n) : c = a + b a = b b = c return c # Driver Code # Take input n n = 10c = nth_term(n) # printing output print (c) # This code is contributed by # Manish Shaw (manishshaw1) |
C#
// C# implementation of the // above approach using System; class GFG { // The total number of ways // is equal to the (n+2)th // Fibonacci term, hence we // only need to find that. static int nth_term(int n) { int a = 1, b = 1, c = 1; // Construct fibonacci upto // (n+2)th term the first // two terms being 1 and 1 for (int i = 0; i < n; i++) { c = a + b; a = b; b = c; } return c; } // Driver Code public static void Main() { // Take input n int n = 10; int c = nth_term(n); // printing output Console.WriteLine(c); } } // This code is contributed by Sam007 |
PHP
<?php // PHP implementation of the // above approach // The total number of ways // is equal to the (n+2)th // Fibonacci term, hence we // only need to find that. function nth_term($n) { $a = 1; $b = 1; $c = 1; // Construct fibonacci upto // (n+2)th term the first // two terms being 1 and 1 for ($i = 0; $i < $n; $i++) { $c = $a + $b; $a = $b; $b = $c; } return $c; } // Driver Code // Take input n $n = 10; $c = nth_term($n); // printing output echo $c; // This code is contributed by nitin mittal ?> |
Output:
144
We can further optimize above solution to work in O(Log n) using matrix exponentiation solution for finding n-th Fibonacci number (Please see methods 4, 5 and 6 of Program for Fibonacci numbers ).
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:
- Count array elements that can be represented as sum of at least two consecutive array elements
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Number of ways to choose elements from the array such that their average is K
- Check if elements of an array can be arranged in a Circle with consecutive difference as 1
- Check if an array can be split into subsets of K consecutive elements
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Number of ways to change the Array such that largest element is LCM of array
- Maximum number of elements that can be removed such that MEX of the given array remains unchanged
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find elements of array using XOR of consecutive elements
- Replace array elements by sum of next two consecutive elements
- Find minimum sum such that one of every three consecutive elements is taken
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Maximum subsequence sum such that no K elements are consecutive
- Check if the array can be sorted only if the elements on given positions can be swapped
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Number of ways to calculate a target number using only array elements
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.

