0% found this document useful (0 votes)
8 views5 pages

Recursion Strivers

Uploaded by

Thirishaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Recursion Strivers

Uploaded by

Thirishaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

double myPow(double x, int n) {


if(n==0){
return 1.0;
}
double result=1.0;
int exp=abs(n);
for(int i=0;i<exp;i++){
result*=x;
}
if(n<0){
return 1/result;
}
else{
return result;
}
}

-----------------------------------------------------------------------------------
--------------------
Reverse Stack Using Recursion
Example:
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]

void insertAtBottom(stack<int> &stack,int top){


if(stack.empty()){
stack.push(top);
}
else{
int temp=stack.top();
stack.pop();
insertAtBottom(stack,top);
stack.push(temp);
}
}

void reverseStack(stack<int> &stack) {


// Write your code here
if(!stack.empty()){
int top=stack.top();
stack.pop();
reverseStack(stack);
insertAtBottom(stack,top);
}

-----------------------------------------------------------------------------------
------------
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:

Input: nums = [1,2,3]


Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

#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;
}
}
}

vector<vector<int>> combSum(vector<int> &arr, int B)


{
// Write your code here.
vector<int>curr;
vector<vector<int>> result;
sort(arr.begin(),arr.end());
generate(arr,curr,0,B,result);
return result;
}

-----------------------------------------------------------------------------------
---------------------------------
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.

void generate(vector<int> &num,vector<int> &curr,int index,vector<int> &res){


int sum=0;
for(int x:curr){
sum+=x;
}
res.push_back(sum);
for(int i=index;i<num.size();i++){
curr.push_back(num[i]);
generate(num,curr,i+1,res);
curr.pop_back();
}
}

vector<int> subsetSum(vector<int> &num){


// Write your code here.
vector<int> curr;
vector<int> res;
sort(num.begin(),num.end());
generate(num,curr,0,res);
sort(res.begin(),res.end());
return res;
}

-----------------------------------------------------------------------------------
----------------
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.

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word =


"ABCCED"
Output: true

bool generate(vector<vector<char>> &board,string &word,int i,int j,int index){


if(word.length()==index){
return true;
}
if(i<0||j<0||i>=board.size()||j>=board[0].size()||board[i][j]!=word[index]){
return false;
}

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;
}

bool present(vector<vector<char>> &board, string &word, int n, int m)


{
// Write your code here.
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(board[i][j]==word[0]&generate(board,word,i,j,0)){
return true;
}
}
}
return false;
}

-----------------------------------------------------------------------------------
--------------------------

You might also like