Skip to content

Commit 5bf8a65

Browse files
solved on 10/10/23
1 parent b451de8 commit 5bf8a65

5 files changed

+135
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void findEvenNumbers(int arr[], int n, int index, vector<int> &evens)
5+
{
6+
if (index >= n)
7+
{
8+
return;
9+
}
10+
if (!(arr[index] & 1))
11+
{
12+
evens.push_back(arr[index]);
13+
}
14+
findEvenNumbers(arr, n, index + 1, evens);
15+
}
16+
17+
int main()
18+
{
19+
int arr[] = {1, 2, 3, 4, 5};
20+
int n = 5;
21+
vector<int> evens;
22+
findEvenNumbers(arr, n, 0, evens);
23+
cout << "Even numbers in the array are: ";
24+
for (int i = 0; i < evens.size(); i++)
25+
{
26+
cout << evens[i] << " ";
27+
}
28+
cout << endl;
29+
return 0;
30+
}

LC_53_ClimbingStairs.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// can either take 1 step or 2 steps at a time to reach the top of the stairs (n)
5+
int climbStairs(int n)
6+
{
7+
// TLE
8+
// if(n == 2){
9+
// return 2;
10+
// }
11+
// if(n == 1){
12+
// return 1;
13+
// }
14+
// int ans = climbStairs(n-1) + climbStairs(n-2);
15+
// return ans;
16+
17+
// Brute
18+
int prev1 = 0;
19+
int prev2 = 1;
20+
for (int i = 0; i <= n - 2; i++)
21+
{
22+
int temp = prev1 + prev2;
23+
prev1 = prev2;
24+
prev2 = temp;
25+
}
26+
return prev1 + prev2;
27+
}
28+
29+
int main()
30+
{
31+
int n;
32+
cin >> n;
33+
cout << climbStairs(n) << endl;
34+
return 0;
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void findMin(int arr[], int n, int index, int &min)
5+
{
6+
if (index >= n)
7+
{
8+
return;
9+
}
10+
if (arr[index] < min)
11+
{
12+
min = arr[index];
13+
}
14+
findMin(arr, n, index + 1, min);
15+
}
16+
17+
int main()
18+
{
19+
int arr[] = {1, 2, 3, 4, 5};
20+
int n = 5;
21+
int min = INT_MAX;
22+
findMin(arr, n, 0, min);
23+
cout << "Minimum element in the array is: ";
24+
cout << min << endl;
25+
return 0;
26+
}

PrintArrayUsingRecursion.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void printArray(vector<int> &nums, int idx)
5+
{
6+
if (idx == nums.size())
7+
{
8+
return;
9+
}
10+
cout << nums[idx] << " ";
11+
printArray(nums, idx + 1);
12+
}
13+
14+
int main()
15+
{
16+
vector<int> nums = {1, 2, 3, 4, 5};
17+
printArray(nums, 0);
18+
return 0;
19+
}

SearchInArrayUsingRecursion.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int search(int arr[], int n, int key, int index)
5+
{
6+
if (index == n)
7+
{
8+
return -1;
9+
}
10+
if (arr[index] == key)
11+
{
12+
return index;
13+
}
14+
return search(arr, n, key, index + 1);
15+
}
16+
17+
int main()
18+
{
19+
int arr[] = {1, 2, 3, 4, 5};
20+
int n = 5;
21+
int key = 3;
22+
cout << key << " found at index: ";
23+
cout << search(arr, n, key, 0) << endl;
24+
return 0;
25+
}

0 commit comments

Comments
 (0)