Skip to content

Commit fb4d7a6

Browse files
committed
Added Recursion Problems
1 parent b6cc56d commit fb4d7a6

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
created: 2025-12-17
3+
modified:
4+
completed: true
5+
platform: gfg
6+
problem-id:
7+
link: https://www.geeksforgeeks.org/problems/find-all-factorial-numbers-less-than-or-equal-to-n3548/0
8+
difficulty: Easy
9+
tags:
10+
- gfg
11+
---
12+
# Factorials Less than or Equal to n | Practice | GeeksforGeeks
13+
14+
## Question
15+
### Factorials Less than or Equal to n
16+
17+
A number **n** is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are 1, 2, 6, 24, 120,
18+
Given a number **n**, the task is to return the list/vector of the factorial numbers smaller than or equal to n.
19+
20+
**Examples:**
21+
22+
```
23+
Input: n = 3
24+
Output: 1 2
25+
Explanation: The first factorial number is 1 which is less than equal to n. The second number is 2 which is less than equal to n,but the third factorial number is 6 which is greater than n. So we print only 1 and 2.
26+
```
27+
```
28+
Input: n = 6
29+
Output: 1 2 6
30+
Explanation: The first three factorial numbers are less than equal to n but the fourth factorial number 24 is greater than n. So we print only first three factorial numbers.
31+
```
32+
33+
**Constraints:**
34+
1<=n<=10 <sup>18</sup>
35+
---
36+
37+
## Solution
38+
39+
### Intuition
40+
41+
### Approach
42+
43+
### Complexity
44+
- Time:
45+
- Space:
46+
47+
### Code
48+
---
49+
[!]
50+
```cpp
51+
// User function Template for C++
52+
class Solution {
53+
public:
54+
vector<long long> factorialNumbers(long long n) {
55+
vector<long long> vec;
56+
if (n<=1) return vec;
57+
for(int i = 1,temp=1;i<=n;){
58+
cout<<i<<" ";
59+
i=i*(++temp);
60+
}
61+
62+
}
63+
};
64+
```
65+
66+
### Optimal Code
67+
---
68+
[!]
69+
```cpp
70+
vector<long long> result;
71+
72+
long long fact = 1;
73+
long long k = 1;
74+
75+
while (fact <= n) {
76+
result.push_back(fact);
77+
k++;
78+
if (fact > n / k) break; // prevent overflow
79+
fact *= k;
80+
}
81+
82+
return result;
83+
```
84+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
created: 2025-12-17
3+
modified:
4+
completed: true
5+
platform: gfg
6+
problem-id:
7+
link: https://www.geeksforgeeks.org/problems/print-1-to-n-without-using-loops-1587115620/1
8+
difficulty: Basic
9+
tags:
10+
---
11+
# Print 1 To N Without Loop | Practice | GeeksforGeeks
12+
13+
## Question
14+
### Print 1 To N Without Loop
15+
16+
You are given an integer **n**. You have  to print all numbers from **1** to **n**.
17+
**Note**: You must use **recursion** only,andprint all numbers from **1** to **n** in a single line, separated by spaces.
18+
19+
**Examples:
20+
**
21+
22+
```
23+
Input: n = 10
24+
Output: 1 2 3 4 5 6 7 8 9 10
25+
```
26+
```
27+
Input: n = 5
28+
Output: 1 2 3 4 5
29+
```
30+
```
31+
Input: n = 1
32+
Output: 1
33+
```
34+
35+
**Constraints:**
36+
1 ≤ n ≤ 10 <sup>3</sup>
37+
38+
If you are facing any issue on this page. Please let us know.
39+
40+
---
41+
42+
## Solution
43+
44+
### Intuition
45+
46+
### Approach
47+
48+
### Complexity
49+
- Time:
50+
- Space:
51+
52+
### Code
53+
---
54+
[!]
55+
```cpp
56+
class Solution {
57+
public:
58+
void printNos(int n) {
59+
if(n==0) return;
60+
printNos(n-1);
61+
cout<<n<<" ";
62+
}
63+
};
64+
```
65+
66+
### Optimal Code
67+
---
68+
[!]
69+
```cpp
70+
71+
```
72+

0 commit comments

Comments
 (0)