Total number of ways to place X and Y at n places such that no two X are together

Given N positions, the task is to count the total number of ways to place X and Y such that no two X are together.

Examples:

Input: 3
Output: 5
XYX, YYX, YXY, XYY and YYY 

Input: 4
Output: 8
XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY

Approach:
For N = 1, X and Y are 2 possible ways.
For N = 2, XY, YX and YY are the 3 possible ways.
For N = 3, XYX, YYX, YXY, XYY and YYY are 5 possible ways.
For N = 4, XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY are 8 possible ways.
On solving for values of N, a Fibonacci pattern series is observed.

Below is the iterative implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the 
// number of ways Calculate
// total ways to place 'x' 
// and 'y' at n places such 
// that no two 'x' are together
#include <iostream>
using namespace std;
  
    // Function to return
    // number of ways 
    int ways(int n)
    {
        // for n=1
        int first = 2;
  
        // for n=2
        int second = 3;
        int res = 0;
  
        // iterate to find 
        // Fibonacci term
        for (int i = 3; i <= n; i++) 
        {
            res = first + second;
            first = second;
            second = res;
        }
  
        return res;
    }
  
// Driver Code
int main() 
{
    // total number of places
    int n = 7;
    cout << "Total ways are : ";
    cout << ways(n);
  
    return 0;
}
  
// This code is contributed 
// by jit_t

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the number of ways 
// Calculate total ways to place 'x' and 'y' 
// at n places such that no two 'x' are together
public class GFG {
      
    // Function to return number of ways 
    static int ways(int n)
    {
        // for n=1
        int first = 2;
  
        // for n=2
        int second = 3;
        int res = 0;
  
        // iterate to find Fibonacci term
        for (int i = 3; i <= n; i++) {
            res = first + second;
            first = second;
            second = res;
        }
  
        return res;
    }
    public static void main(String[] args)
    {
  
        // total number of places
        int n = 7;
  
        System.out.print("Total ways are: " + ways(n));
    }
}

chevron_right


Pyhton3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the
# number of ways Calculate 
# total ways to place 'x' 
# and 'y' at n places such 
# that no two 'x' are together
  
# Function to return
# number of ways 
def ways(n):
      
    # for n=1
    first = 2;
  
    # for n=2
    second = 3;
    res = 0;
  
    # iterate to find 
    # Fibonacci term
    for i in range(3, n + 1):
        res = first + second;
        first = second;
        second = res;
  
    return res;
      
# Driver Code 
  
# total number of places
n = 7;
print("Total ways are: "
                 ways(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the 
// number of ways. Calculate 
// total ways to place 'x'
// and 'y' at n places such 
// that no two 'x' are together
using System;
  
class GFG
{
      
    // Function to return
    // number of ways 
    static int ways(int n)
    {
        // for n=1
        int first = 2;
  
        // for n=2
        int second = 3;
        int res = 0;
  
        // iterate to find 
        // Fibonacci term
        for (int i = 3; i <= n; i++) 
        {
            res = first + second;
            first = second;
            second = res;
        }
  
        return res;
    }
      
    // Driver Code
    static public void Main ()
    {
          
        // total number
        // of places
        int n = 7;
  
        Console.WriteLine("Total ways are: "
                                     ways(n));
    }
}
  
//This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the
// number of ways Calculate 
// total ways to place 'x' 
// and 'y' at n places such 
// that no two 'x' are together
  
// Function to return
// number of ways 
function ways($n)
{
    // for n=1
    $first = 2;
  
    // for n=2
    $second = 3;
    $res = 0;
  
    // iterate to find 
    // Fibonacci term
    for ($i = 3; $i <= $n; $i++)
    {
        $res = $first + $second;
        $first = $second;
        $second = $res;
    }
  
    return $res;
}
// Driver Code 
  
// total number of places
$n = 7;
echo "Total ways are: " , ways($n);
  
// This code is contributed by ajit 
?>

chevron_right


Output:

Total ways are: 34

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Enthusiastic Coder Developer and always a learner

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.



Improved By : jit_t, Mithun Kumar