Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Recursion & Backtracking/RatInAMaze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <bits/stdc++.h>
using namespace std;

bool isSafe(int x, int y, int n, vector<vector<int>>& mapping, int arr[10][10]) {
return (x<0 || y<0 || x>=n || y>=n || mapping[x][y]==1) ? false : (arr[x][y]==1 ? true : false);
}

void findPaths(int arr[10][10], int n, int x, int y, vector<string>& solution, string output, vector<vector<int>>& mapping) {
if(x==n-1 && y==n-1) {
solution.push_back (output);
return;
}

mapping[x][y] = 1;
int newx, newy;

// Checking for UP location
newx = x-1;
newy = y;
if(isSafe(newx, newy, n, mapping, arr)) {
output.push_back ('U');
findPaths(arr, n, newx, newy, solution, output, mapping);
output.pop_back();
}

// Checking for DOWN location
newx = x+1;
newy = y;
if(isSafe(newx, newy, n, mapping, arr)) {
output.push_back ('D');
findPaths(arr, n, newx, newy, solution, output, mapping);
output.pop_back();
}

// Checking for LEFT location
newx = x;
newy = y-1;
if(isSafe(newx, newy, n, mapping, arr)) {
output.push_back ('L');
findPaths(arr, n, newx, newy, solution, output, mapping);
output.pop_back();
}

// Checking for RIGHT location
newx = x;
newy = y+1;
if(isSafe(newx, newy, n, mapping, arr)) {
output.push_back ('R');
findPaths(arr, n, newx, newy, solution, output, mapping);
output.pop_back();
}

mapping[x][y] = 0;
}

int main() {
int arr[10][10];
int n;

cout<<"Enter the value of n: ";
cin>>n;

cout<<"Enter the elements of matrix: ";
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin>>arr[i][j];
}
}

vector<vector<int>> mapping;
for(int i=0; i<n; i++) {
vector<int> temp (n,0);
mapping.push_back (temp);
}

vector<string> solution;
string output = "";

findPaths(arr, n, 0, 0, solution, output, mapping);

for(int i=0; i<solution.size(); i++) {
cout<<solution[i]<<endl;
}

return 0;
}
70 changes: 70 additions & 0 deletions Recursion & Backtracking/RecursionInArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <bits/stdc++.h>
using namespace std;

bool isSorted(int arr[], int size) {
if(size<=1)
return true;
if(arr[0] > arr[1])
return false;
else
return isSorted(arr+1, size-1);
}

int findSum(int arr[], int size) {
if(size==0)
return 0;
return arr[0] + findSum(arr+1, size-1);
}

bool linearSearch(int arr[], int size, int key) {
if(size==0)
return false;
if(arr[0]==key)
return true;
else
return linearSearch(arr+1, size-1, key);
}

bool binarySearch(int arr[], int start, int end, int key) {
int mid = start + (end-start)/2;
if(start > end)
return false;
if(arr[mid] == key)
return true;
if(arr[mid]>key)
return binarySearch(arr, start, mid-1, key);
else
return binarySearch(arr, mid+1, end, key);
}

int main() {
int arr[10] = {2, 5, 8, 10, 12, 12, 15, 18, 20, 25};
int size = 10;

/*
if(isSorted(arr, size))
cout<<"Array is sorted";
else
cout<<"Array is not sorted";
*/

//cout<<"Sum = "<<findSum(arr, size);

/*
int key = 80;
if(linearSearch(arr, size, key))
cout<<"Key is found in the array";
else
cout<<"Key is not found in the array";
*/

/*
int key = 15;
if(binarySearch(arr, 0, size-1, key))
cout<<"Key is found in the array";
else
cout<<"Key is not found in the array";
*/

return 0;
}
31 changes: 31 additions & 0 deletions Recursion & Backtracking/arraySum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;

int arraySum(int arr[], int n) {
if(n==1) {
return arr[0];
}

if(n==0) {
return 0;
}

//int ans = arr[0] + arr[1];
return arr[n-1] + arraySum(arr,n-1);
}

int main() {
int arr[100], n;

cout<<"Enter the size of array: ";
cin>>n;

cout<<"Enter the elements of the array: ";
for(int i=0; i<n; i++) {
cin>>arr[i];
}

cout<<"\nSum of the elements of the given array is : "<<arraySum(arr,n);

return 0;
}
44 changes: 44 additions & 0 deletions Recursion & Backtracking/binarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;

int binarySearch(int arr[], int key, int start, int end) {
if(start > end) {
return -1;
}

int mid = start + (end-start)/2;
if(arr[mid]==key) {
return mid;
}

if(arr[mid]>key) {
return binarySearch(arr, key, start, mid-1);
} else {
return binarySearch(arr, key, mid+1, end);
}
}

int main() {
int arr[100], n, key;

cout<<"Enter the size of array: ";
cin>>n;

cout<<"Enter the elements of the array: ";
for(int i=0; i<n; i++) {
cin>>arr[i];
}

cout<<"Enter the key to search for: ";
cin>>key;

int position = binarySearch(arr, key, 0, n-1);

if(position == -1) {
cout<<"\nKey is not found in the array.";
} else {
cout<<"\nKey is found in the array at position : "<<position;
}

return 0;
}
37 changes: 37 additions & 0 deletions Recursion & Backtracking/bubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

void bubbleSort(int arr[], int n) {
if(n <= 1) {
return;
}

for(int i=0; i<n-1; i++) {
if(arr[i] > arr[i+1]) {
swap(arr[i], arr[i+1]);
}
}

bubbleSort(arr, n-1);
}

int main() {
int arr[100], n;

cout<<"Enter the size of array: ";
cin>>n;

cout<<"Enter the elements of the array: ";
for(int i=0; i<n; i++) {
cin>>arr[i];
}

bubbleSort(arr, n);

cout<<"\nThe sorted array is: ";
for(int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}

return 0;
}
47 changes: 47 additions & 0 deletions Recursion & Backtracking/isSorted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <conio.h>
using namespace std;

bool isSorted_logic1(int arr[], int n) {
if(arr[n-1] >= arr[n-2] || n<=2) {
return true;
}

if(arr[n-1] < arr[n-2]) {
return false;
}

return isSorted_logic1(arr, n-1);
}

bool isSorted_logic2(int arr[], int n) {
if(n<=1) {
return true;
}

if(arr[0] > arr[1]) {
return false;
} else {
return isSorted_logic2(arr+1, n-1);
}
}

int main() {
int arr[100], n;

cout<<"Enter the size of array: ";
cin>>n;

cout<<"Enter the elements of the array: ";
for(int i=0; i<n; i++) {
cin>>arr[i];
}

if(isSorted_logic1(arr, n)) {
cout<<"\nSORTED ARRAY !!";
} else {
cout<<"\nUNSORTED ARRAY !!";
}

return 0;
}
40 changes: 40 additions & 0 deletions Recursion & Backtracking/letterCombination.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

void letterCombinations(string digits, string output, int index, vector<string>& combinations, string mapping[]) {
if(index >= digits.length()) {
combinations.push_back (output);
return;
}

int number = digits[index] - 48;
string value = mapping[number];

for(int i=0; i<value.length(); i++) {
output.push_back (value[i]);
letterCombinations(digits, output, index+1, combinations, mapping);
output.pop_back();
}
}

int main() {
string digits;
cout<<"Enter the string: ";
cin>>digits;

vector<string> combinations;
string output = "";
int index = 0;
string mapping[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

if(digits.length() > 0) {
letterCombinations(digits, output, index, combinations, mapping);
}

cout<<"All the possible combinations are:\n";
for(int i=0; i<combinations.size(); i++) {
cout<<combinations[i]<<endl;
}

return 0;
}
Loading