From 4f78c66abb7618e8b9c0061ebd204c69bf44a889 Mon Sep 17 00:00:00 2001 From: Girish Garg Date: Wed, 30 Sep 2020 23:08:12 +0530 Subject: [PATCH] Added recursion --- README.md | 1 + .../power or exponentiation.cpp | 25 ++++ Recursion/1. Basics/firstOcc/firstOcc.cpp | 40 +++++++ .../1. Basics/inc dec order/print rec.cpp | 44 +++++++ .../bubble sort rec/bubble sort rec.cpp | 49 ++++++++ .../inv count/inv count.cpp | 58 ++++++++++ .../quick sort/quick sort.cpp | 51 +++++++++ .../2048 problem recursion.cpp | 39 +++++++ .../friends pairing problem.cpp | 36 ++++++ .../Ladder problem/Ladder problem.cpp | 25 ++++ .../Ladder problem/Ladder problem2.cpp | 27 +++++ .../binaryStringswithoutcon1s.cpp | 41 +++++++ .../place the tiles/place the tiles2.cpp | 22 ++++ .../replace pi/replace pi.cpp | 36 ++++++ .../string to integer recursion.cpp | 19 ++++ .../tower of hanoi/tower of hanoi.cpp | 17 +++ .../1. more intuitive.cpp | 34 ++++++ .../2. using bitmasking.cpp | 26 +++++ .../intuitive practice.cpp | 31 +++++ .../using rec 2.cpp | 45 ++++++++ .../using rec.cpp | 38 +++++++ .../balanced brackets.cpp | 30 +++++ .../4. Subset Based/3. 01 Knapsack/input.txt | 3 + .../4. Subset Based/3. 01 Knapsack/output.txt | 1 + .../3. 01 Knapsack/practice.cpp | 47 ++++++++ .../using bitmasking practice.cpp | 49 ++++++++ .../3. 01 Knapsack/using bitmasking.cpp | 48 ++++++++ .../3. 01 Knapsack/using rec.cpp | 62 ++++++++++ .../4. Keypad Problem/4. Keypad Problem.cpp | 60 ++++++++++ .../4. Keypad Problem/pratice.cpp | 38 +++++++ .../5. ACODE String Generation/ACODE.cpp | 53 +++++++++ .../6. Sum it up/sum it up.cpp | 50 ++++++++ .../1. Rat in a maze/Rat in a maze.cpp | 67 +++++++++++ .../Permutations of a string.cpp | 28 +++++ .../Using set STL.cpp | 41 +++++++ .../5. Backtracking/3. N queens/N queens.cpp | 91 +++++++++++++++ .../3. N queens/bitmasking approach.cpp | 27 +++++ .../3. N queens/bitset approach.cpp | 29 +++++ .../4. Sudoku Solver/Sudoku Solver.cpp | 107 ++++++++++++++++++ 39 files changed, 1535 insertions(+) create mode 100644 Recursion/1. Basics/Modular exponentiation/power or exponentiation.cpp create mode 100644 Recursion/1. Basics/firstOcc/firstOcc.cpp create mode 100644 Recursion/1. Basics/inc dec order/print rec.cpp create mode 100644 Recursion/2. Implementation based/bubble sort rec/bubble sort rec.cpp create mode 100644 Recursion/2. Implementation based/inv count/inv count.cpp create mode 100644 Recursion/2. Implementation based/quick sort/quick sort.cpp create mode 100644 Recursion/3. Quick Thinking/2048 problem recursion/2048 problem recursion.cpp create mode 100644 Recursion/3. Quick Thinking/Friends pairing problem/friends pairing problem.cpp create mode 100644 Recursion/3. Quick Thinking/Ladder problem/Ladder problem.cpp create mode 100644 Recursion/3. Quick Thinking/Ladder problem/Ladder problem2.cpp create mode 100644 Recursion/3. Quick Thinking/binaryStringswithoutcon1s/binaryStringswithoutcon1s.cpp create mode 100644 Recursion/3. Quick Thinking/place the tiles/place the tiles2.cpp create mode 100644 Recursion/3. Quick Thinking/replace pi/replace pi.cpp create mode 100644 Recursion/3. Quick Thinking/stringToInt/string to integer recursion.cpp create mode 100644 Recursion/3. Quick Thinking/tower of hanoi/tower of hanoi.cpp create mode 100644 Recursion/4. Subset Based/1. subset or subsequences gen recursion/1. more intuitive.cpp create mode 100644 Recursion/4. Subset Based/1. subset or subsequences gen recursion/2. using bitmasking.cpp create mode 100644 Recursion/4. Subset Based/1. subset or subsequences gen recursion/intuitive practice.cpp create mode 100644 Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec 2.cpp create mode 100644 Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec.cpp create mode 100644 Recursion/4. Subset Based/2. balanced brackets/balanced brackets.cpp create mode 100644 Recursion/4. Subset Based/3. 01 Knapsack/input.txt create mode 100644 Recursion/4. Subset Based/3. 01 Knapsack/output.txt create mode 100644 Recursion/4. Subset Based/3. 01 Knapsack/practice.cpp create mode 100644 Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking practice.cpp create mode 100644 Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking.cpp create mode 100644 Recursion/4. Subset Based/3. 01 Knapsack/using rec.cpp create mode 100644 Recursion/4. Subset Based/4. Keypad Problem/4. Keypad Problem.cpp create mode 100644 Recursion/4. Subset Based/4. Keypad Problem/pratice.cpp create mode 100644 Recursion/4. Subset Based/5. ACODE String Generation/ACODE.cpp create mode 100644 Recursion/4. Subset Based/6. Sum it up/sum it up.cpp create mode 100644 Recursion/5. Backtracking/1. Rat in a maze/Rat in a maze.cpp create mode 100644 Recursion/5. Backtracking/2. Permutations of a string/Permutations of a string.cpp create mode 100644 Recursion/5. Backtracking/2. Permutations of a string/Using set STL.cpp create mode 100644 Recursion/5. Backtracking/3. N queens/N queens.cpp create mode 100644 Recursion/5. Backtracking/3. N queens/bitmasking approach.cpp create mode 100644 Recursion/5. Backtracking/3. N queens/bitset approach.cpp create mode 100644 Recursion/5. Backtracking/4. Sudoku Solver/Sudoku Solver.cpp diff --git a/README.md b/README.md index 60a7097..737347b 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,6 @@ Sections - Getting started with C++ - Programming Fundamentals - Programming Fundamentals-II +- Recursion diff --git a/Recursion/1. Basics/Modular exponentiation/power or exponentiation.cpp b/Recursion/1. Basics/Modular exponentiation/power or exponentiation.cpp new file mode 100644 index 0000000..895c16e --- /dev/null +++ b/Recursion/1. Basics/Modular exponentiation/power or exponentiation.cpp @@ -0,0 +1,25 @@ +// exponentiation using recursion + +#include +using namespace std; + +int power(int a, int b){ + // Base case + if(b==0) return 1; + return a*power(a,b-1); +} + +int powerFast(int a, int b){ + // Base case + if(b==0) return 1; + if(b&1) return a*power(a, b/2)*power(a, b/2); + else return power(a, b/2)*power(a, b/2); +} + +int main(){ + int a, b; cin>>a>>b; + cout< +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +int firstOcc(int arr[], int s, int e, int key){ + // Base case + if(s==e) return -1; + if(arr[s]==key) return s; + + return firstOcc(arr, s+1, e, key); +} + +void allOcc(int arr[], int s, int e, int key){ + // Base case + if(s==e) return; + else if(arr[s]==key){ + cout< +using namespace std; + +void printDec(int n){ + cout< +using namespace std; + +bool compare(int a, int b){ + return a +#include +#define MAX 100000 +using namespace std; +int inv=0; + +void merge(vector &a, vector &b, vector &toreturn){ //You are given two sorted arrays A and B. You want to merge these arrays and return a sorted vector will elements from both arrays. + int apointer = 0, bpointer = 0; //point to the first element in the vectors + //That hasn't been taken yet + while(apointer a,b; + for(int i=s; i<=mid; i++) a.push_back(arr[i]); + for(int i=mid+1; i<=e; i++) b.push_back(arr[i]); + + vector temp; + merge(a,b, temp); + for(int i=0; i>n; + int arr[n]; + for(int i=0; i>arr[i]; + ms(arr, 0, n-1); + for(int i=0; i +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +void print(int n){ + + switch(n){ + case 0: cout<<"zero"; break; + case 1: cout<<"one"; break; + case 2: cout<<"two"; break; + case 3: cout<<"three"; break; + case 4: cout<<"four"; break; + case 5: cout<<"five"; break; + case 6: cout<<"six"; break; + case 7: cout<<"seven"; break; + case 8: cout<<"eight"; break; + case 9: cout<<"nine"; break; + default: cout<<"error"; + } +} + +void fun(int n){ + // Base case + if(n==0) return; + + fun(n/10); + print(n%10); cout<<" "; +} + +int main(){ + int n=204478; + fun(n); +} + + diff --git a/Recursion/3. Quick Thinking/Friends pairing problem/friends pairing problem.cpp b/Recursion/3. Quick Thinking/Friends pairing problem/friends pairing problem.cpp new file mode 100644 index 0000000..a9bb243 --- /dev/null +++ b/Recursion/3. Quick Thinking/Friends pairing problem/friends pairing problem.cpp @@ -0,0 +1,36 @@ +// Friends pairing problem + +// Given N, frinds who want to go to a party on bikes. +// Each person can go single, or as a couple. Find the number of ways in which N friends can go to the party. + +#include +#define MAX 100000 +using namespace std; + +int noOfWays(int n){ + // Base Case + if(n==1 || n==0) return 1; + if(n==2) return 2; + + return noOfWays(n-1)+(n-1)*noOfWays(n-2); +} + +int topDown(int n, int dp[]){ + // Lookup + if(dp[n]!=-1){ + return dp[n]; + } + + return dp[n]=topDown(n-1, dp) + (n-1)*topDown(n-2, dp); +} + +int main(){ + int dp[MAX]; + std::fill_n(dp, MAX, -1); + dp[0]=1; dp[1]=1; dp[2]=2; + int n; cin>>n; + cout< +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +int main(){ + int dp[MAX]{0}; + dp[1]=1; dp[2]=2; dp[3]=4; + + for(int i=4; i>n; + cout< +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +int count(int n){ + // Base Case + if(n==1 || n==0) return 1; + if(n<0) return 0; + + return count(n-1)+count(n-2)+count(n-3); +} + +int main(){ + + int n; cin>>n; + cout< +#define MAX 100000 +using namespace std; + +int count(int n){ + if(n==1) return 2; + if(n==2) return 3; + + int a=2, b=3; + for(int i=2; i>n; + cout< +using namespace std; + +int noOfWays(int n){ + // Base Case + if(n==1 || n==2 || n==3) return 1; + if(n==4) return 2; + + return noOfWays(n-1)+noOfWays(n-4); + +} + +int main(){ + int n; cin>>n; + cout< +using namespace std; + +void replace(char a[], int s, int e){ + // Base case + if(s>=e) return; + + int i=s; + for(i=s; ii+1; j--){ + a[j+2] = a[j]; + } + a[i]='3'; a[i+1]='.'; a[i+2]='1'; a[i+3]='4'; + + replace(a, i+4, e+4); + break; + } + } + + +} + +int main(){ + char a[100]="xpighpilmpipi"; + + int n=0; while(a[n]!='\0') n++; + + replace(a,0, n); + cout< +using namespace std; + +int stringToInt(char a[], int n){ + // Base Case + if(n<0) return 0; + int digit = a[n]-'0'; + return (stringToInt(a, n-1)*10)+digit; + +} + +int main(){ + char a[]="1234"; + int n = strlen(a); + int ans = stringToInt(a, n-1); + cout< +using namespace std; + +int move(int n){ + if(n==1) return 1; + + return 2*move(n-1)+1; +} + +int main(){ + int n; cin>>n; + cout< +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +void generate_subset(char *in, char *out, int i, int j){ + // Base Case + if(in[i]=='\0'){ + out[j] = '\0'; + cout< +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +int main(){ + string s; cin>>s; + + int n = s.size(); + + for(int i=0; i<(1< +#define ll long long +#define pb push_back +#define MAX 100005 +#define mod 1000000007 // 1e9+7 +#define inf 1e18 +using namespace std; + +void generate_subset(char in[], char out[], int i, int j) { + + if (in[i] == '\0') { + out[j] = '\0'; + cout << out << endl; + return; + } + + // including in[i] + out[j] = in[i]; + generate_subset(in, out, i + 1, j + 1); + + + // Excluding in[i] + generate_subset(in, out, i + 1, j); +} + +int main() { + char in[] = "abc"; + char out[10] = ""; + + generate_subset(in, out, 0, 0); +} diff --git a/Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec 2.cpp b/Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec 2.cpp new file mode 100644 index 0000000..ba9b74b --- /dev/null +++ b/Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec 2.cpp @@ -0,0 +1,45 @@ +// The idea is to fix a prefix, generate all subsets beginning with current prefix. +/// After all subsets with a prefix are generated, replace last character with one of the remaining characters +// CPP program to generate power set +#include +using namespace std; + +// str : Stores input string +// curr : Stores current subset +// index : Index in current subset, curr +void powerSet(string str, int index = -1, + string curr = "") +{ + int n = str.length(); + + // base case + if (index == n) + return; + + // First print current subset + cout << curr << "\n"; + + // Try appending remaining characters + // to current subset + for (int i = index + 1; i < n; i++) { + + curr += str[i]; + powerSet(str, i, curr); + + // Once all subsets beginning with + // initial "curr" are printed, remove + // last character to consider a different + // prefix of subsets. + curr.erase(curr.size() - 1); + } + return; +} + +// Driver code +int main() +{ + string str = "abc"; + powerSet(str); + return 0; +} + diff --git a/Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec.cpp b/Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec.cpp new file mode 100644 index 0000000..15cc427 --- /dev/null +++ b/Recursion/4. Subset Based/1. subset or subsequences gen recursion/using rec.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +//void generateSubsets(string first, string st, int s, int e){ +// // Base Case +// if(s==e){ +// cout<>str; + + int n = str.size(); + + generateSubsets(str, 0, ""); + +} + + diff --git a/Recursion/4. Subset Based/2. balanced brackets/balanced brackets.cpp b/Recursion/4. Subset Based/2. balanced brackets/balanced brackets.cpp new file mode 100644 index 0000000..4d7f6bf --- /dev/null +++ b/Recursion/4. Subset Based/2. balanced brackets/balanced brackets.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +void brackets(string s, int count_o, int count_c, int n){ + // Base Case + if(count_c==n){ + cout<>n; + string s; + brackets(s, 0, 0, n); +} + + diff --git a/Recursion/4. Subset Based/3. 01 Knapsack/input.txt b/Recursion/4. Subset Based/3. 01 Knapsack/input.txt new file mode 100644 index 0000000..f374e2a --- /dev/null +++ b/Recursion/4. Subset Based/3. 01 Knapsack/input.txt @@ -0,0 +1,3 @@ +4 7 +40 20 35 100 +1 2 3 4 \ No newline at end of file diff --git a/Recursion/4. Subset Based/3. 01 Knapsack/output.txt b/Recursion/4. Subset Based/3. 01 Knapsack/output.txt new file mode 100644 index 0000000..a762560 --- /dev/null +++ b/Recursion/4. Subset Based/3. 01 Knapsack/output.txt @@ -0,0 +1 @@ +160 diff --git a/Recursion/4. Subset Based/3. 01 Knapsack/practice.cpp b/Recursion/4. Subset Based/3. 01 Knapsack/practice.cpp new file mode 100644 index 0000000..7e77eec --- /dev/null +++ b/Recursion/4. Subset Based/3. 01 Knapsack/practice.cpp @@ -0,0 +1,47 @@ +#include +#define ll long long +#define pb push_back +#define MAX 100005 +#define mod 1000000007 // 1e9+7 +#define inf 1e18 +using namespace std; + +int price[MAX], weight[MAX], n, capacity, maxP = 0; + +void subset(int i, int cw, int cp) { + + //Base Case + if (i == n) { + if (cw <= capacity) { + maxP = max(maxP, cp); + //cout << maxP << endl; + } + return; + } + + // Including price and weight[i] + subset(i + 1, cw + weight[i], cp + price[i]); + + //Excluding price and weight[i] + subset(i + 1, cw, cp); + +} + +int main() { + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin >> n >> capacity; + + for (int i = 0; i < n; i++) cin >> price[i]; + for (int i = 0; i < n; i++) cin >> weight[i]; + + subset(0, 0, 0); + cout << maxP << endl; +} diff --git a/Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking practice.cpp b/Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking practice.cpp new file mode 100644 index 0000000..696b07c --- /dev/null +++ b/Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking practice.cpp @@ -0,0 +1,49 @@ +// Using bitmasking + +#include +#define ll long long +#define pb push_back +#define MAX 100005 +#define mod 1000000007 // 1e9+7 +#define inf 1e18 +using namespace std; + +int price[MAX], weight[MAX], n, capacity, maxP; + +void solve() { + + for (int mask = 1; mask < 1 << n; mask++) { + int cp = 0, cwt = 0; + for (int i = 0; i < n; i++) { + if (mask & 1 << i) { + cwt += weight[i]; + cp += price[i]; + } + } + + if (cwt <= capacity) { + maxP = max(cp, maxP); + } + } + +} + +int main() { + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin >> n >> capacity; + + for (int i = 0; i < n; i++) cin >> price[i]; + for (int i = 0; i < n; i++) cin >> weight[i]; + + + solve(); + cout << maxP << endl; +} diff --git a/Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking.cpp b/Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking.cpp new file mode 100644 index 0000000..d5e8266 --- /dev/null +++ b/Recursion/4. Subset Based/3. 01 Knapsack/using bitmasking.cpp @@ -0,0 +1,48 @@ +// Time O(n*2^n) + +#include +#include +#include +#include +#define ll long long +#define MAX 1005 +using namespace std; + +void solve(){ + + int n; cin>>n; + int capacity, maxPrice=INT_MIN; cin>>capacity; + + int price[MAX]={0}, weight[MAX]={0}; + + for(int i=0; i>price[i]; + for(int i=0; i>weight[i]; + + for(int mask=0; mask<(1<>t; + while(t--){ + solve(); + } +} + + diff --git a/Recursion/4. Subset Based/3. 01 Knapsack/using rec.cpp b/Recursion/4. Subset Based/3. 01 Knapsack/using rec.cpp new file mode 100644 index 0000000..f024c3f --- /dev/null +++ b/Recursion/4. Subset Based/3. 01 Knapsack/using rec.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#define ll long long +#define MAX 1005 +using namespace std; + +int capacity; +int n, maxSum = INT_MIN; +int price[MAX] = {0}, weight[MAX] = {0}; + +void solve(int *in, int *out, int i = 0, int j = 0, int cwt = 0, int cp = 0) { + + // Base Case + if (i == n) { + if (cwt <= capacity) { + maxSum = max(maxSum, cp); + //cout << maxSum << endl; + } + return; + } + + // Including in[i] + out[j] = in[i]; + if ( (cwt + in[i]) <= capacity) + solve(in, out, i + 1, j + 1, cwt + in[i], cp + price[i]); + + // Excluding in[i] + solve(in, out, i + 1, j, cwt, cp); + +} + + +int main() { + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif + + int t; + cin >> t; + while (t--) { + maxSum = INT_MIN; + cin >> n; + cin >> capacity; + int out[MAX] = {0}; + + for (int i = 0; i < n; i++) cin >> price[i]; + for (int i = 0; i < n; i++) cin >> weight[i]; + + // weight will be my input array + solve(weight, out); + + if (maxSum != INT_MIN) cout << maxSum << endl; + else cout << "0" << endl; + + } +} + + diff --git a/Recursion/4. Subset Based/4. Keypad Problem/4. Keypad Problem.cpp b/Recursion/4. Subset Based/4. Keypad Problem/4. Keypad Problem.cpp new file mode 100644 index 0000000..b2a3184 --- /dev/null +++ b/Recursion/4. Subset Based/4. Keypad Problem/4. Keypad Problem.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +char keypad[][5] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wx", "yz"}; +int counter=0; + + +void subsets(char *in, char *out, int i=0, int j=0){ + // Base Case + // If i reaches end + if(in[i]=='\0'){ + out[j] = '\0'; + cout<>in; + + char out[MAX]; + + subsets(in, out); + cout< +#define ll long long +#define pb push_back +#define MAX 100005 +#define mod 1000000007 // 1e9+7 +#define inf 1e18 +using namespace std; + +char keypad[][5] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wx", "yz"}; +int counter = 0; + +void subsets(char in[], char out[], int i = 0, int j = 0) { + + // Base case + if (in[i] == '\0') { + out[j] = '\0'; + cout << out << endl; + return; + } + int key = in[i] - '0'; + // I will choose key-1 + for (int k = 0; keypad[key - 1][k] != '\0'; k++) { + + //Including keypad[key-1][k] + out[j] = keypad[key - 1][k]; + subsets(in, out, i + 1, j + 1); + } + +} + +int main() { + char in[MAX] = "23"; + + char out[MAX]; + + subsets(in, out); + cout << endl << counter; +} \ No newline at end of file diff --git a/Recursion/4. Subset Based/5. ACODE String Generation/ACODE.cpp b/Recursion/4. Subset Based/5. ACODE String Generation/ACODE.cpp new file mode 100644 index 0000000..d7c422a --- /dev/null +++ b/Recursion/4. Subset Based/5. ACODE String Generation/ACODE.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#define ll long long +#define MAX 100000 +using namespace std; + +void subsets(char *in, char *out, int i=0, int j=0){ + // Base Case + if(in[i]=='\0'){ + out[j] = '\0'; + cout<>in; + char out[MAX]; + + subsets(in, out); +} + + diff --git a/Recursion/4. Subset Based/6. Sum it up/sum it up.cpp b/Recursion/4. Subset Based/6. Sum it up/sum it up.cpp new file mode 100644 index 0000000..3dad81c --- /dev/null +++ b/Recursion/4. Subset Based/6. Sum it up/sum it up.cpp @@ -0,0 +1,50 @@ +#include +#define ll long long +#define MAX 100000 +using namespace std; + +ll n, target; + +bool solve(int in[], int out[], int i=0, int j=0, int sum=0){ + + // Base Case + if(sum==target){ + // Print the output + for(int x=0; xtarget){ + return false; + } + + // Rec case + + // Inlcuding in[i] + out[j] = in[i]; + solve(in, out, i+1, j+1, sum+in[i]); + + // We will also try the case where we exclude in[i] + // excluding in[i] + solve(in, out, i+1, j, sum); +} + +int main(){ + cin>>n; + int arr[MAX]={0}; + for(int i=0; i>arr[i]; + } sort(arr, arr+n); + cin>>target; + + int out[MAX]={0}; + + solve(arr, out); + +} + + diff --git a/Recursion/5. Backtracking/1. Rat in a maze/Rat in a maze.cpp b/Recursion/5. Backtracking/1. Rat in a maze/Rat in a maze.cpp new file mode 100644 index 0000000..f9091e2 --- /dev/null +++ b/Recursion/5. Backtracking/1. Rat in a maze/Rat in a maze.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; +bool ratInMaze(char maze[][1005], int sol[][1005], int i, int j, int m, int n) +{ + //Base case + if (i == m && j == n) + { + sol[i][j] = 1; + for (int row = 0; row <= m; row++) + { + for (int col = 0; col <= n; col++) + { + cout << sol[row][col] << " "; + } + cout << endl; + } + cout << endl; + return true; + } + + //Rat should be inside the maze + if (i > m || j > n) + return false; + + //Should not encounter a blocked cell + if (maze[i][j] == 'X') + return false; + + //Recursive case + //Assume solution exists through current cell + sol[i][j] = 1; + + bool rightSuccess = ratInMaze(maze, sol, i, j + 1, m, n); + if (rightSuccess) { + return true ; + } + bool downSuccess = ratInMaze(maze, sol, i + 1, j, m, n); + if (downSuccess) { + return true ; + } + + //Backtrack + sol[i][j] = 0; + + return false; + +} +int main() +{ + char maze[1005][1005]; + int sol[1005][1005] = {0}; + int m ; //No of rows + int n ; //No of cols + cin >> m >> n; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + cin >> maze[i][j]; + } + } + + bool ans = ratInMaze(maze, sol, 0, 0, m - 1, n - 1); + if (!ans) + cout << "-1"; + + + return 0; +} \ No newline at end of file diff --git a/Recursion/5. Backtracking/2. Permutations of a string/Permutations of a string.cpp b/Recursion/5. Backtracking/2. Permutations of a string/Permutations of a string.cpp new file mode 100644 index 0000000..8243d45 --- /dev/null +++ b/Recursion/5. Backtracking/2. Permutations of a string/Permutations of a string.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +void permutation(char *s, int i=0){ + + // Base Case + if(s[i]=='\0'){ + cout<>s; + + permutation(s); +} + + diff --git a/Recursion/5. Backtracking/2. Permutations of a string/Using set STL.cpp b/Recursion/5. Backtracking/2. Permutations of a string/Using set STL.cpp new file mode 100644 index 0000000..c65e3d5 --- /dev/null +++ b/Recursion/5. Backtracking/2. Permutations of a string/Using set STL.cpp @@ -0,0 +1,41 @@ +// Permutatins using set STL + +#include +#include +#include +using namespace std; +set s; + +void permutation(char a[], int i=0){ + + // Base Case + if(a[i]=='\0'){ +// cout<>a; + + permutation(a, 0); + + // Loop over the set + for(auto str:s){ + cout< +#define ll long long +#define MAX 100000 +using namespace std; +int counter = 0; + +bool isSafe(int board[][20], int i, int j, int n) { + + // check for column + for (int row = 0; row < i; row++) { + if (board[row][j]) { + return 0; + } + } + + // Check for left diagonal + int x = i; + int y = j; + + while (x >= 0 && y >= 0) { + if (board[x][y]) return 0; + x--; y--; + } + + // Check for right diagonal + x = i; + y = j; + + while (x >= 0 && y < n) { + if (board[x][y]) return 0; + x--; y++; + } + + // Therefore, the position is now safe + return 1; +} + +// i: starting row +bool solve(int board[][20], int i, int n) { + // Base Case + if (i == n) { + // print board +// for(int row=0; row>n; + + int board[20][20] = {0}; + + solve(board, 0, n); + cout << counter; +} + + diff --git a/Recursion/5. Backtracking/3. N queens/bitmasking approach.cpp b/Recursion/5. Backtracking/3. N queens/bitmasking approach.cpp new file mode 100644 index 0000000..21e3225 --- /dev/null +++ b/Recursion/5. Backtracking/3. N queens/bitmasking approach.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +int n; +int ans = 0, DONE; + + +void solve(int rowMask, int ld, int rd) { + if (rowMask == DONE) { + ans++; + return; + } + + int safe = DONE & (~(rowMask | ld | rd)); + while (safe) { + int p = safe & (-safe); + safe = safe - p; + solve(rowMask | p, (ld | p) <<< 1, (rd | p) >> 1); + } +} + +int main() { + cin >> n; + DONE = ((1 << n) - 1); + solve(0, 0, 0); + cout << ans << endl; +} \ No newline at end of file diff --git a/Recursion/5. Backtracking/3. N queens/bitset approach.cpp b/Recursion/5. Backtracking/3. N queens/bitset approach.cpp new file mode 100644 index 0000000..b50de94 --- /dev/null +++ b/Recursion/5. Backtracking/3. N queens/bitset approach.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +bitset<30> col, d1, d2; + +void solve(int r, int n, int &ans) { + if (r == n) { + + + ans++; + return; + } + + for (int c = 0; c < n; c++) { // Trying to place Queen at every place in the current row + if (!col[c] && !d1[r - c + n - 1] && !d2[r + c]) { // If safe + col[c] = d1[r - c + n - 1] = d2[r + c] = 1; //Try placing Queen there + solve(r + 1, n, ans); + col[c] = d1[r - c + n - 1] = d2[r + c] = 0; // Backtracking + } + } +} + +int main() { + int n = 5; + int ans = 0; + //int board[20][20] = {0}; + solve(0, n, ans); + cout << ans; +} \ No newline at end of file diff --git a/Recursion/5. Backtracking/4. Sudoku Solver/Sudoku Solver.cpp b/Recursion/5. Backtracking/4. Sudoku Solver/Sudoku Solver.cpp new file mode 100644 index 0000000..4a9c58c --- /dev/null +++ b/Recursion/5. Backtracking/4. Sudoku Solver/Sudoku Solver.cpp @@ -0,0 +1,107 @@ +#include +#define ll long long +#define MAX 100000 +using namespace std; +int n = 9; + +bool isSafe(int board[][9], int i, int j, int num) { + +// // Check in row +// for(int col=0; col>n; + + int board[9][9] = { + {5, 3, 0, 0, 7, 0, 0, 0, 0 }, + {6, 0, 0, 1, 9, 5, 0, 0, 0 }, + {0, 9, 8, 0, 0, 0, 0, 6, 0 }, + {8, 0, 0, 0, 6, 0, 0, 0, 3 }, + {4, 0, 0, 8, 0, 3, 0, 0, 1 }, + {7, 0, 0, 0, 2, 0, 0, 0, 6 }, + {0, 6, 0, 0, 0, 0, 2, 8, 0 }, + {0, 0, 0, 4, 1, 9, 0, 0, 5 }, + {0, 0, 0, 0, 8, 0, 0, 7, 9 } + }; + + // for(int i=0; i>board[i][j]; + // } + // } + + solve(board, 0, 0, n); +} + +