Count possible ways to construct buildings
Given an input number of sections and each section has 2 plots on either sides of the road. Find all possible ways to construct buildings in the plots such that there is a space between any 2 buildings.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
N = 1 Output = 4 Place a building on one side. Place a building on other side Do not place any building. Place a building on both sides. N = 3 Output = 25 3 sections, which means possible ways for one side are BSS, BSB, SSS, SBS, SSB where B represents a building and S represents an empty space Total possible ways are 25, because a way to place on one side can correspond to any of 5 ways on other side. N = 4 Output = 64
We strongly recommend to minimize your browser and try this yourself first
We can simplify the problem to first calculate for one side only. If we know the result for one side, we can always do the square of the result and get the result for two sides.
A new building can be placed on a section if section just before it has space. A space can be placed anywhere (it doesn’t matter whether the previous section has a building or not).
Let countB(i) be count of possible ways with i sections ending with a building. countS(i) be count of possible ways with i sections ending with a space. // A space can be added after a building or after a space. countS(N) = countB(N-1) + countS(N-1) // A building can only be added after a space. countB[N] = countS(N-1) // Result for one side is sum of the above two counts. result1(N) = countS(N) + countB(N) // Result for two sides is square of result1(N) result2(N) = result1(N) * result1(N)
Below is the implementation of the above idea.
Count of ways for 3 sections is 25
Count of ways for 3 sections is 25
Time complexity: O(N)
Auxiliary Space: O(1)
Algorithmic Paradigm: Dynamic Programming
Another Way of Thinking : (on one side only because for another side it will be the same)
Let us think of buildings as the sequence of N (because there are N plots on either side) length binary string (each digit either 0 or 1) where :
1 => Represents building has been made on the ith plot 0 => Represents building has not been made on the ith plot
Now as the problem states we have to find the number of ways such that we don’t have consecutive Buildings on plots, in the binary string, it can be interpreted as, we need to find the number of ways such that we do not have consecutive 1 in the binary string (as 1 represented building being made)
N = 3 Total Combinations = 2n = 23 = 8 This will contain some combination in there will be consecutive building, so we have to reject that 000 (No building) (Possible) 001 (Building on 3rd plot) (Possible) 010 (Building on 2nd plot) (Possible) 011 (Building on 2nd and 3rd plot) (Not Possible as there are consecutive buildings) 100 (Building on 1st plot) (Possible) 101 (Building on 1st and 3rd plot) (Possible) 110 (Building on 1st and 2nd plot) (Not Possible as there are consecutive buildings) 111 (Building on 1st, 2nd, 3rd plot) (Not Possible as there are consecutive buildings) Total Possible Ways = 5 These are only on one side, on other side it will also be same as there are N plots and same condition, so Answer = Total Possible Ways * Total Possible Ways = 25
So now our problem is reduced to find the number of ways to represent N length binary string such that it does not have consecutive 1 which is a pretty standard problem
Note that the above solution can be further optimized. If we take a closer look at the results, for different values, we can notice that the results for the two sides are squares of Fibonacci Numbers.
N = 1, result = 4 [result for one side = 2]
N = 2, result = 9 [result for one side = 3]
N = 3, result = 25 [result for one side = 5]
N = 4, result = 64 [result for one side = 8]
N = 5, result = 169 [result for one side = 13]
In general, we can say
result(N) = fib(N+2)2 fib(N) is a function that returns N'th Fibonacci Number.
Therefore, we can use the O(LogN) implementation of Fibonacci Numbers to find the number of ways in O(logN) time.
This article is contributed by GOPINATH. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above