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.
Input: 3 Output: 5 XYX, YYX, YXY, XYY and YYY Input: 4 Output: 8 XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY
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:
Total ways are: 34
Time Complexity: O(N)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Number of ways to place two queens on a N*N chess-board
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Total ways of choosing X men and Y women from a total of M men and W women
- Total ways of selecting a group of X men from N men with or without including a particular man
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Find the sum of digits of a number at even and odd places
- Check whether sum of digits at odd places of a number is divisible by K
- Primality test for the sum of digits at odd places of a number
- Check whether product of digits at even places of a number is divisible by K
- Check if product of digits of a number at even and odd places is equal
- Find the total number of composite factor for a given number
- Count total number of digits from 1 to n
- Total number of Subsets of size at most K