Skip to content

Commit 57f81ca

Browse files
Added on 29th Aug
1 parent 2b0cb60 commit 57f81ca

6 files changed

+195
-0
lines changed

CS_10_Print1ToNUsingRecursion.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Backtracking solution to print 1 to N using recursion
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
vector<int> printNos(int x) {
6+
// Base case: Stop recursion when x becomes 0
7+
if (x == 0) {
8+
return vector<int>(); // at the end of the recursion, we return an empty vector and then this gets filled up as the recursion goes back up to its previous calls, programming is truly magnificent, isn't it?
9+
}
10+
// Recursive call
11+
vector<int> ans = printNos(x - 1); // calling function before adding to vector will print in ascending order, because at the end of the recurive call the variable x would be 1, then we return to a function with x = 2, then x = 3, and so on
12+
// Add the current number to the vector
13+
ans.push_back(x);
14+
return ans;
15+
}
16+
17+
int main()
18+
{
19+
vector<int> ans = printNos(5);
20+
for (auto i : ans)
21+
{
22+
cout << i << " ";
23+
}
24+
25+
return 0;
26+
}

CS_11_PrintNTo1UsingRecursion.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Normal solution to print N to 1 using recursion
5+
void doTheMagic(int x, vector<int> &ans) {
6+
if (x == 0) {
7+
return;
8+
}
9+
10+
ans.push_back(x);
11+
doTheMagic(x - 1, ans);
12+
}
13+
14+
vector<int> printNos(int x) {
15+
vector<int> ans;
16+
doTheMagic(x, ans);
17+
return ans;
18+
}
19+
20+
// Backtracking solution to print N to 1 using recursion
21+
// vector<int> printNos(int x, int n) {
22+
// // Base case: Stop recursion when x becomes 0
23+
// if (x > n) {
24+
// return vector<int>();
25+
// }
26+
// // Recursive call
27+
// vector<int> ans = printNos(x + 1, n); // calling function before adding to vector will print in reverse order, because at the end of the recurive call the variable x would be 5, then we return to a function with x = 4, then x = 3, and so on
28+
// // Add the current number to the vector
29+
// ans.push_back(x);
30+
// return ans;
31+
// }
32+
33+
int main()
34+
{
35+
// vector<int> ans = printNos(1, 5); // backtracking solution, here we need the 2nd parameter because we are adding 1 to x, so we need to know when to stop unlike the 1-N problem where we are subtracting 1 from x, so we can stop when x becomes 0
36+
vector<int> ans = printNos(5); // normal solution
37+
for (auto i : ans)
38+
{
39+
cout << i << " ";
40+
}
41+
42+
return 0;
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
bool f(int i, string &str)
5+
{
6+
if (i >= str.size() / 2)
7+
return true;
8+
if (str[i] != str[str.size() - i - 1])
9+
return false;
10+
return f(i + 1, str);
11+
}
12+
13+
bool isPalindrome(string &str) { return f(0, str); }
14+
15+
int main()
16+
{
17+
string str = "abba";
18+
if (isPalindrome(str))
19+
cout << "Yes" << endl;
20+
else
21+
cout << "No" << endl;
22+
return 0;
23+
}
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+
vector<int> generateFibonacciNumbers(int n)
5+
{
6+
if (n == 1)
7+
return {0};
8+
if (n == 2)
9+
return {0, 1};
10+
11+
vector<int> small = generateFibonacciNumbers(n - 1);
12+
small.push_back(small[n - 2] + small[n - 3]);
13+
return small;
14+
}
15+
16+
int main()
17+
{
18+
vector<int> ans = generateFibonacciNumbers(10);
19+
for (auto i : ans)
20+
{
21+
cout << i << " ";
22+
}
23+
24+
return 0;
25+
}

CS_14_CountFrequencyInARange.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> countFrequency(int n, int x, vector<int> &nums)
5+
{
6+
vector<int> ans(n, 0);
7+
for (int i = 0; i < n; i++)
8+
{
9+
ans[nums[i] - 1]++;
10+
}
11+
return ans;
12+
}
13+
14+
int main()
15+
{
16+
int n, x;
17+
cout << "Enter the number of elements in the array: ";
18+
cin >> n;
19+
cout << "Enter the range of elements in the array: ";
20+
cin >> x;
21+
vector<int> nums(n);
22+
cout << "Enter the elements of the array: ";
23+
for (int i = 0; i < n; i++)
24+
{
25+
cin >> nums[i];
26+
}
27+
vector<int> ans = countFrequency(n, x, nums); // this returns the frequency of each element in the array from 1 to n
28+
for (int i = 0; i < n; i++)
29+
{
30+
cout << i + 1 << " -> " << ans[i] << endl;
31+
}
32+
return 0;
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> getFrequencies(vector<int> &v)
5+
{
6+
int n = v.size();
7+
map<int, int> mp;
8+
for (int i = 0; i < n; i++)
9+
{
10+
mp[v[i]]++;
11+
}
12+
int max = INT_MIN, min = INT_MAX;
13+
int x = 0, y = 0;
14+
for (auto it : mp)
15+
{
16+
if (it.second > max)
17+
{
18+
max = it.second;
19+
x = it.first;
20+
}
21+
if (it.second < min)
22+
{
23+
min = it.second;
24+
y = it.first;
25+
}
26+
}
27+
return {x, y};
28+
}
29+
30+
int main()
31+
{
32+
int n;
33+
cout << "Enter the number of elements in the array: ";
34+
cin >> n;
35+
vector<int> v(n);
36+
cout << "Enter the elements of the array: ";
37+
for (int i = 0; i < n; i++)
38+
{
39+
cin >> v[i];
40+
}
41+
vector<int> ans = getFrequencies(v);
42+
cout << "The element with the highest frequency is: " << ans[0] << endl;
43+
cout << "The element with the lowest frequency is: " << ans[1] << endl;
44+
return 0;
45+
}

0 commit comments

Comments
 (0)