-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec5_countNoOfHops.cpp
More file actions
51 lines (40 loc) · 971 Bytes
/
Copy pathlec5_countNoOfHops.cpp
File metadata and controls
51 lines (40 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//https://www.geeksforgeeks.org/problems/count-number-of-hops-1587115620/1?utm_source=geeksforgeeks&utm_medium=ml_article_practice_tab&utm_campaign=article_practice_tab
// time limit exceeded due to recursion
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to count the number of ways in which frog can reach the top.
long long countWays(int n)
{
// your code here
if(n<=1)
{
return 1;
}
if(n== 2) return 2;
if(n == 3) return 4;
return countWays(n - 1) + countWays(n-2) + countWays(n-3);
}
};
//{ Driver Code Starts.
int main()
{
//taking testcases
int t;
cin >> t;
while(t--)
{
//taking number of steps in stair
int n;
cin>>n;
Solution ob;
//calling function countWays()
cout << ob.countWays(n) << endl;
}
return 0;
}
// } Driver Code Ends