Count ways to N'th Stair(Order does not matter)
PROBLEM :
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does not matter). Order does not matter means for n=4 {1 2 1},{2 1 1},{1 1 2} are considered same.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, an integer N will be given.
Output:
Print number of possible ways to reach Nth stair.
Constraints:
1<=T<=1000
1<=N<=100
Example:
Input:
1
4
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long Count_nth_stair(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=Count_nth_stair(no) ;
cout<<no<<endl ;
}
return 0;
}
long long Count_nth_stair(long long no)
{
long long arr[no+1]={0} ;
int i ;
arr[0]=1 ;
for(i=1;i<=no;i++)
arr[i]=arr[i]+arr[i-1] ;
for(i=2;i<=no;i++)
arr[i]=arr[i]+arr[i-2] ;
return arr[no] ;
}
---------------------------------------------------------------------------------
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does not matter). Order does not matter means for n=4 {1 2 1},{2 1 1},{1 1 2} are considered same.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, an integer N will be given.
Output:
Print number of possible ways to reach Nth stair.
Constraints:
1<=T<=1000
1<=N<=100
Example:
Input:
1
4
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long Count_nth_stair(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=Count_nth_stair(no) ;
cout<<no<<endl ;
}
return 0;
}
long long Count_nth_stair(long long no)
{
long long arr[no+1]={0} ;
int i ;
arr[0]=1 ;
for(i=1;i<=no;i++)
arr[i]=arr[i]+arr[i-1] ;
for(i=2;i<=no;i++)
arr[i]=arr[i]+arr[i-2] ;
return arr[no] ;
}
---------------------------------------------------------------------------------
cout<<(n/2)+1<<endl;
ReplyDeleteHi @Hitesh, try out your solution here: https://practice.geeksforgeeks.org/problems/count-ways-to-reach-the-nth-stair/0
ReplyDelete