C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Count the total number ways to reach the nth stair using step 1, 2 or 3 but the step 3 can be taken only once

## Count the total number ways to reach the nth stair using step 1, 2 or 3 but the step 3 can be taken only once Tag : algorithm , By : user109127 Date : January 12 2021, 07:00 PM

I hope this helps you . Let res0[n] be the number of ways to reach n steps without using a 3-step, and let res1[n] be the number of ways to reach n steps after having used a 3-step.
res0[i] and res1[i] are easily calculated from the previous values, in a manner similar to your existing code.

Share :

## Count Number of ways to reach Nth step using steps of lengths 1,2,3,4,......m.(where m<=n)

Tag : cpp , By : moss
Date : March 29 2020, 07:55 AM
seems to work fine Your inner loop adds res[i-1] + res[i-2] + ... + res[i-m] to the result.
Let s be the sum of the first i elements in res. Then you can simply add s[i-1] - s[i-m-1] to the result.
``````ll countWays_(ll n, ll m){
ll res[n];
res[0] = 1; res[1] = 1;
s[0] = 1; s[1] = 2;
for (ll i=2; i<n; i++)
{
if (i <= m)
res[i] = s[i-1] % MOD;
else
res[i] = (s[i-1] - s[i - m - 1] + MOD) % MOD;
s[i] = (s[i-1] + res[i]) % MOD;
}
return res[n-1];
}
``````

## Trying to understand lists in recursive ways step by step

Tag : prolog , By : user152423
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'll help you through your first example. This should give you enough information to tackle the second on your own...
``````concatenar([], Xs, Xs).
concatenar([X|Xs], Ys, [X|Zs]) :- concatenar(Xs,  Ys, Zs).
``````
``````?- concatenar([a,b,c], [x,y,z], L).
``````

## How to interpret code for counting ways to reach the n’th stair?

Tag : python , By : Lee KW
Date : March 29 2020, 07:55 AM
it helps some times I am trying to interpret this code from geeksforgeeks: , Check it out like this:
``````# Python 3
def fib(n):
print(f'Fib: {n}')
if n <= 1:
print('End')
return n
else:
print(f'Send: {n-1} and {n-2}')
return fib(n-1) + fib(n-2)

def countWays(s):
return fib(s + 1)

s = 4
print("Number of ways = ")
print(countWays(s))
``````
``````Fib: 5           # A
Send: 4 and 3    # A.B and A.C
Fib: 4           # A.B
Send: 3 and 2    # A.B.1 and A.B.2
Fib: 3           # A.B.1
Send: 2 and 1    # A.B.1.1 and A.B.1.2
Fib: 2           # A.B.1.1
Send: 1 and 0    # A.B.1.1.1 and A.B.1.1.2
Fib: 1           # A.B.1.1.1
End              # (END) A.B.1.1.1       -> 1
Fib: 0           # A.B.1.1.2
End              # (END) A.B.1.1.2       -> 0
Fib: 1           # A.B.1.2
End              # (END) A.B.1.2         -> 1
Fib: 2           # A.B.2
Send: 1 and 0    # A.B.2.1 and A.B.2.2
Fib: 1           # A.B.2.1
End              # (END) A.B.2.1         -> 1
Fib: 0           # A.B.2.2
End              # (END) A.B.2.2         -> 0
Fib: 3           # A.C
Send: 2 and 1    # A.C.1 and A.C.2
Fib: 2           # A.C.1
Send: 1 and 0    # A.C.1.1 and A.C.1.2
Fib: 1           # A.C.1.1
End              # (END) A.C.1.1         -> 1
Fib: 0           # A.C.1.2
End              # (END) A.C.1.2         -> 0
Fib: 1           # A.C.2
End              # (END) A.C.2           -> 1
5                                     # 1 + 0 + 1 + 1 + 0 + 1 + 0 + 1
``````
``````>>> print(fib(5))
Fib: 5
Send: 4 and 3
Fib: 4
...
Fib: 1
End
5
``````

## Count ways to reach the 4th stair using step 1, 2 or 3

Tag : java , By : user186876
Date : March 29 2020, 07:55 AM
will be helpful for those in need Your for loop starts with i = 3, but you already filled in res[3]. That is not a problem if the number of ways to reach 3 would be the same as in your hardcoded value arr[3] = 4;, but it is not.
Indeed, you never have set res[0] to 1, and therefore, it will calculate res[3] = res[2] + res[1] + res[0] as 3, not 4.
``````public static void main(String[] args) {
int n = 46;
int [] arr = new int[n+1];
arr[0] = 1;
arr[1] = 1;
arr[2] = 2;
arr[3] = 4;
for(int i = 3; i < n; i++){
arr[i]= arr[i-1]+arr[i-2]+arr[i-3];
}
System.out.println(arr[n]);
}``````

Tag : algorithm , By : Der Ketzer
Date : March 29 2020, 07:55 AM