Recursion Strivers
Recursion Strivers
50. Pow(x, n)
Attempted
Medium
Topics
Companies
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
-----------------------------------------------------------------------------------
--------------------
Reverse Stack Using Recursion
Example:
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
-----------------------------------------------------------------------------------
------------
78. Subsets
Medium
Topics
Companies
Given an integer array nums of unique elements, return all possible
subsets
(the power set).
The solution set must not contain duplicate subsets. Return the solution in any
order.
Example 1:
#include<bits/stdc++.h>
using namespace std;
void solve(int i, string s, string &f) {
if (i == s.length()) {
cout << f << " ";
return;
}
//picking
f = f + s[i];
solve(i + 1, s, f);
//poping out while backtracking
f.pop_back();
solve(i + 1, s, f);
}
int main() {
string s = "abc";
string f = "";
cout<<"All possible subsequences are: "<<endl;
solve(0, s, f);
}
-----------------------------------------------------------------------------------
----------------------------
Combination Sum
Your task is to return all unique combinations in the array whose sum equals 'B'.
Let the array 'ARR' be [1, 2, 3] and 'B' = 5. Then all possible valid combinations
are-
(1, 1, 1, 1, 1)
(1, 1, 1, 2)
(1, 1, 3)
(1, 2, 2)
(2, 3)
//https://chat.openai.com/share/1e2a878a-33ee-48d7-833a-742a377cfeb2
void generate(vector<int> &arr,vector<int> &curr,int index,int
B,vector<vector<int>> &result){
if(B==0){
result.push_back(curr);
return;
}
for(int i=index;i<arr.size();i++){
if(arr[i]<=B){
curr.push_back(arr[i]);
generate(arr,curr,i,B-arr[i],result);
curr.pop_back();
}
else{
break;
}
}
}
-----------------------------------------------------------------------------------
---------------------------------
Subset Sums
Given a list arr of n integers, return sums of all subsets in it. Output sums can
be printed in any order.
Example 1:
Input:
n = 2
arr[] = {2, 3}
Output:
0 2 3 5
Explanation:
When no elements is taken then Sum = 0.
When only 2 is taken then Sum = 2.
When only 3 is taken then Sum = 3.
When element 2 and 3 are taken then
Sum = 2+3 = 5.
-----------------------------------------------------------------------------------
----------------
79. Word Search
Medium
Topics
Companies
Given an m x n grid of characters board and a string word, return true if word
exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where
adjacent cells are horizontally or vertically neighboring. The same letter cell may
not be used more than once.
char temp=board[i][j];
board[i][j]= '.' ;
bool found=generate(board,word,i+1,j,index+1)||
generate(board,word,i-1,j,index+1)||
generate(board,word,i,j+1,index+1)||
generate(board,word,i,j-1,index+1);
board[i][j]=temp;
return found;
}
-----------------------------------------------------------------------------------
--------------------------