Week - 7th, Assignment Sol - 1st
Week - 7th, Assignment Sol - 1st
int main() {
int a, b;
return 0;
}
2. Calculate the number of ways in which a person can climb n stairs if he can take
exactly 1, 2 or 3 steps at each level.
#include <bits/stdc++.h>
using namespace std;
int findNumberOfWays(int n) {
if(n < 0) return 0;
if(n == 0)return 1;
return findNumberOfWays(n-1) + findNumberOfWays(n-2) + findNumberOfWays(n-3);
}
int main() {
int n;
cin >> n;
cout << findNumberOfWays(n) << endl;
return 0;
}
3. Given a positive integer, return true if it is a power of 2.
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfTwo(int n) {
if(n == 1) {
return true;
}
if(n % 2 == 0) {
return isPowerOfTwo(n / 2);
}
return false;
}
int main() {
int n;
cin >> n;
if(isPowerOfTwo(n)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
4. Print all the elements of an array in reverse order.
#include <bits/stdc++.h>
using namespace std;
void printElementsInReverse(vector<int> &arr, int currIndex, int n) {
if(currIndex == n) {
return;
}
printElementsInReverse(arr, currIndex+1, n);
cout << arr[currIndex] << " ";
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; ++i) {
cin >> arr[i];
}
printElementsInReverse(arr, 0, n);
return 0;
}
5. Print index of a given element in an array.
#include <bits/stdc++.h>
using namespace std;
int indexOfKey(vector<int> &arr, int currIndex, int n, int key) {
if(currIndex == n) {
return -1;
}
if(arr[currIndex] == key) {
return currIndex;
}
return indexOfKey(arr, currIndex+1, n, key);
}
int main() {
int n, key;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; ++i) {
cin >> arr[i];
}
6. If not present, print -1. A function countAndSay is defined as
countAndSay(1) = “1”
countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1),
which is then converted into a different digit string.
So, if sample input is n = 4,
countAndSay(1) = 1
countAndSay(2) = “one 1” => 11
countAndSay(3) = “two 1” => 21
countAndSay(4) = “one 2 one 1” => 1211
#include <bits/stdc++.h>
using namespace std;
string countAndSay(int n) {
if(n == 1) {
return "1";
}
string ans = "";
string smallAns = countAndSay(n-1);
for(int i = 0; i < smallAns.size();) {
int count = 1;
int j = i+1;
while(j < smallAns.size() && smallAns[i] == smallAns[j]) {
j++;
count++;
}
ans = ans + to_string(count) + smallAns[i];
i = j;
}
return ans;
}
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
return 0;
}
7. Given an array of integers, print a sum triangle using recursion from it such that the
first level has all array elements. After that, at each level the number of elements is
one less than the previous level and elements at the level will be the sum of
consecutive two elements in the previous level. So, if sample input is [5, 4, 3, 2, 1],
sample output will be:
[5, 4, 3, 2, 1]
[9, 7, 5, 3]
[16, 12, 8]
[28, 20]
[48]
#include <bits/stdc++.h>
using namespace std;
vector<int> temp(n-1);
for(int i = 0; i < n; ++i) {
cout << arr[i] << " ";
if(i != 0) {
temp[i-1] = arr[i-1] + arr[i];
}
}
cout << endl;
sumTriangle(temp, n-1);
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the array elements: ";
for(int i = 0; i < n; ++i) {
cin >> arr[i];
}
return 0;
}
8. Write a recursive function to reverse a number.
#include <bits/stdc++.h>
using namespace std;
void reverseNum(int n, int &ans) {
if(n == 0) {
return;
}
int digit = n % 10;
ans = ans * 10 + digit;
reverseNum(n / 10, ans);
}
int main() {
int n;
cin >> n;
int ans = 0;
reverseNum(n, ans);
cout << ans << endl;
return 0;
}
9. Avoid preceding 0s in the reversed number. Print all the increasing sequences of
length k from first n natural numbers.
#include <iostream>
#include <vector>
using namespace std;
// Function to print all increasing sequences of length k from first n natural numbers
void printIncreasingSequences(int n, int k) {
vector<int> seq;
generateSequences(seq, 1, n, k);
}
int main() {
int n, k;
cout << "Enter the value of n: ";
cin >> n;
cout << "Enter the value of k: ";
cin >> k;
cout << "Increasing sequences of length " << k << " from first " << n << " natural numbers are:\n";
printIncreasingSequences(n, k);
return 0
}
10. Given an integer array containing unique numbers, return power set, containing all
the subsets of the set. [Leetcode 78]
#include <iostream>
#include <vector>
// Generate further subsets by including each element starting from the current index
for (int i = index; i < nums.size(); ++i) {
// Include the element nums[i] in the current subset
current.push_back(nums[i]);
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
vector<int> nums(n);
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
return 0;
}
11. Given an integer array which may contain duplicate numbers, return power set,
containing all the subsets of the set. [Leetcode 90]
#include <iostream>
#include <vector>
// Generate further subsets by including each element starting from the current index
for (int i = index; i < nums.size(); ++i) {
// Include the element nums[i] in the current subset
current.push_back(nums[i]);
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
vector<int> nums(n);
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
// Function to find the length of the longest common substring between two strings
int longestCommonSubstring(string s1, string s2) {
int n = s1.size();
int m = s2.size();
int result = 0; // To store the length of the longest common substring
return result;
}
int main() {
string s1, s2;
cout << "Enter the first string: ";
cin >> s1;
cout << "Enter the second string: ";
cin >> s2;
int length = longestCommonSubstring(s1, s2);
cout << "The length of the longest common substring is: " << length << endl;
return 0;
}
13. Program to find the factorial of a given number.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a non-negative integer: ";
cin >> number;
if (number < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
unsigned long long fact = factorial(number);
cout << "Factorial of " << number << " is: " << fact << endl;
}
return 0;
}
14. Program to convert a decimal number to binary
#include <iostream>
using namespace std;
int main() {
int decimalNumber;
cout << "Enter a decimal number: ";
cin >> decimalNumber;
return 0;
}
Note:- Please try to invest time doing the assignments which are necessary to build a
strongfoundation. Do not directly Copy Paste using Google or ChatGPT. Please use
your brain 😃.