Open In App

Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1

Last Updated : 12 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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) 

Implementation:

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 = 10
c = 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
?>


Javascript




<script>
 
// Javascript 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)
{
    let a = 1, b = 1, c = 1;
       
    // Construct fibonacci upto
    // (n+2)th term the first
    // two terms being 1 and 1
    for(let i = 0; i < n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}
 
// Driver code
 
// Take input n
let n = 10;
let c = nth_term(n);
 
// Printing output
document.write(c);
 
// This code is contributed by rameshtravel07
 
</script>


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 ). 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads