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)
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.
- Count numbers from given range having odd digits at odd places and even digits at even places
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Total ways of choosing X men and Y women from a total of M men and W women
- Number of ways to arrange a word such that no vowels occur together
- Number of ways to arrange a word such that all vowels occur together
- Number of ways to place two queens on a N*N chess-board
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Count ways to place all the characters of two given strings alternately
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Seating arrangement of N boys sitting around a round table such that two particular boys sit together
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Arrangement of the characters of a word such that all vowels are at odd places
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Check if product of digits of a number at even and odd places is equal
- Find the sum of digits of a number at even and odd places
- Number of ways to sum up a total of N from limited denominations
- Distance Traveled by Two Trains together in the same Direction
- Probability of not getting two consecutive heads together in N tosses of coin
- Primality test for the sum of digits at odd places of a number
- Check whether sum of digits at odd places of a number is divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.