plz provide code of this problem along with explaination…
you can only take 1 or 2 steps…
Count ways to reach the n th stair if order does not matter
@S19LPPP0202, refer this :- https://www.geeksforgeeks.org/count-ways-reach-nth-stair-using-step-1-2-3/
Here, 3 steps are also considered , but the underlying concept is same
In case of any doubt feel free to ask
concept is not at all same …in the geeks link order matters that is
{2,1} ans {1,2} is not same but in the question asked by me it is same (as order doesnt matter)…
eg if n==4 then in my doubt its answer will be 3…
Order does not matter means for n=4 {1 2 1},{2 1 1},{1 1 2} are considered same.
plz read the doubt carefully and dont just share geeks for geeks link as it is …
reply at the earliest with your solution and explaination…
Since, order does’nt matter then the sequences of steps having same number of two’s and one’s will all be same
now suppose you want to calculate the number of ways to reach stair i i.e dp[i]
if order would have mattered then
dp[i]=dp[i-1]+dp[i-2] but order does’nt matter then from i-2 we can reach at i in following ways :-
A) taking two 1 steps ( (i-2) -> (i-1) -> (i) )
B) taking one two steps ( (i-2) -> (i) )
Notice , in A we reach (i-1)th state from (i-2)th state and there is only one case where we reach at (i-1)th state without going to (i-2)th state is when we take a one 2 step from state (i-3)->(i-1) so dp[i-2] and dp[i-1] overlaps with each other except for one case
since the order does’nt matter then we won’t take overlapping cases (i.e. same number of twos and ones)
so dp[i] = 1 + dp[i-2] (1 for the case (i-3)->(i-1)->(i) )
code link :-
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.