A child is running up a staircase with N steps and can hop either 1, 2 or 3 steps at a time. Implement a method to count how many possible ways the child can run-up up the stairs.
You need to return the number of possible ways W.
Input: 3
Output: 4
Below are the four ways
1 step + 1 step + 1 step
1 step + 2 step +
2 step + 1 step
3 step
#include
using namespace std;
int helper(int n,int count){
if(n==0)
{
return count+1;
}
else if (n<0)
{
return count;
}
count= helper(n-1,count);
count=helper(n-2,count);
count=helper(n-3,count);
}
int staircase(int n){
int temp=0;
return helper(n,temp);
}
int main() {
int n, output;
cin >> n;
output=staircase(n);
cout<< output <<endl;
}
Submitted by ARYA CHANDRAKAR (Arya)
Download packets of source code on Coders Packet
Comments