From 51061e53146d09844c2f1763d21f6c7e2af03b76 Mon Sep 17 00:00:00 2001 From: Akash Gautam <76935019+geekblower@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:28:20 +0530 Subject: [PATCH 1/2] Recursion & Backtracking problems in CPP --- Recursion & Backtracking/RatInAMaze.cpp | 86 ++++++++++++++++++ Recursion & Backtracking/RecursionInArray.cpp | 70 +++++++++++++++ Recursion & Backtracking/arraySum.cpp | 31 +++++++ Recursion & Backtracking/binarySearch.cpp | 44 +++++++++ Recursion & Backtracking/bubbleSort.cpp | 37 ++++++++ Recursion & Backtracking/isSorted.cpp | 47 ++++++++++ .../letterCombination.cpp | 40 +++++++++ Recursion & Backtracking/linearSearch.cpp | 39 ++++++++ Recursion & Backtracking/mergeSort.cpp | 89 +++++++++++++++++++ Recursion & Backtracking/permutation.cpp | 46 ++++++++++ Recursion & Backtracking/pivotElement.cpp | 13 +++ Recursion & Backtracking/powerSet.cpp | 49 ++++++++++ Recursion & Backtracking/powerofN.cpp | 41 +++++++++ Recursion & Backtracking/quickSort.cpp | 59 ++++++++++++ Recursion & Backtracking/revString.cpp | 44 +++++++++ Recursion & Backtracking/sqrt.cpp | 33 +++++++ Recursion & Backtracking/subsequences.cpp | 39 ++++++++ 17 files changed, 807 insertions(+) create mode 100644 Recursion & Backtracking/RatInAMaze.cpp create mode 100644 Recursion & Backtracking/RecursionInArray.cpp create mode 100644 Recursion & Backtracking/arraySum.cpp create mode 100644 Recursion & Backtracking/binarySearch.cpp create mode 100644 Recursion & Backtracking/bubbleSort.cpp create mode 100644 Recursion & Backtracking/isSorted.cpp create mode 100644 Recursion & Backtracking/letterCombination.cpp create mode 100644 Recursion & Backtracking/linearSearch.cpp create mode 100644 Recursion & Backtracking/mergeSort.cpp create mode 100644 Recursion & Backtracking/permutation.cpp create mode 100644 Recursion & Backtracking/pivotElement.cpp create mode 100644 Recursion & Backtracking/powerSet.cpp create mode 100644 Recursion & Backtracking/powerofN.cpp create mode 100644 Recursion & Backtracking/quickSort.cpp create mode 100644 Recursion & Backtracking/revString.cpp create mode 100644 Recursion & Backtracking/sqrt.cpp create mode 100644 Recursion & Backtracking/subsequences.cpp diff --git a/Recursion & Backtracking/RatInAMaze.cpp b/Recursion & Backtracking/RatInAMaze.cpp new file mode 100644 index 0000000..c3b4da9 --- /dev/null +++ b/Recursion & Backtracking/RatInAMaze.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +bool isSafe(int x, int y, int n, vector>& 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& solution, string output, vector>& 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>arr[i][j]; + } + } + + vector> mapping; + for(int i=0; i temp (n,0); + mapping.push_back (temp); + } + + vector solution; + string output = ""; + + findPaths(arr, n, 0, 0, solution, output, mapping); + + for(int i=0; i +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 = "< +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>arr[i]; + } + + cout<<"\nSum of the elements of the given array is : "< +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>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 : "< +using namespace std; + +void bubbleSort(int arr[], int n) { + if(n <= 1) { + return; + } + + for(int i=0; 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>arr[i]; + } + + bubbleSort(arr, n); + + cout<<"\nThe sorted array is: "; + for(int i=0; i +#include +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>arr[i]; + } + + if(isSorted_logic1(arr, n)) { + cout<<"\nSORTED ARRAY !!"; + } else { + cout<<"\nUNSORTED ARRAY !!"; + } + + return 0; +} \ No newline at end of file diff --git a/Recursion & Backtracking/letterCombination.cpp b/Recursion & Backtracking/letterCombination.cpp new file mode 100644 index 0000000..3ba018c --- /dev/null +++ b/Recursion & Backtracking/letterCombination.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +void letterCombinations(string digits, string output, int index, vector& 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>digits; + + vector 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 +using namespace std; + +int linearSearch(int arr[], int n, int key) { + if(arr[n-1]==key) { + return n-1; + } + + if(n==1) { + return -1; + } + + return linearSearch(arr, n-1, key); +} + +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>arr[i]; + } + + cout<<"Enter the key to search for: "; + cin>>key; + + int position = linearSearch(arr, n, key); + + if(position == -1) { + cout<<"\nKey is not found in the array."; + } else { + cout<<"\nKey is found in the array at position : "< +using namespace std; + +void mergeArrays(int arr[], int start, int end) { + int mid = start + (end-start)/2; + + int len1 = mid - start + 1; + int len2 = end - mid; + + int *firstArray = new int[len1]; + int *secondArray = new int[len2]; + + // Coping both arrays + int index = start; + for(int i=0; i secondArray[j]) { + arr[index++] = secondArray[j++]; + } else { + arr[index++] = firstArray[i++]; + arr[index++] = secondArray[j++]; + } + } + + while(i= end) { + return; + } + + int mid = start + (end-start)/2; + + // Sorting left array + mergeSort(arr, start, mid); + + // Sorting right array + mergeSort(arr, mid+1, end); + + // Merging both sorted arrays + mergeArrays(arr, start, end); +} + +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>arr[i]; + } + + mergeSort(arr, 0, n-1); + + cout<<"\nThe sorted array is: "; + for(int i=0; i +using namespace std; + +void findPermutation(vector& arr, int index, vector>& solution) { + if(index >= arr.size()) { + solution.push_back (arr); + return; + } + + for(int i=index; i arr; + vector> solution; + int index = 0; + + cout<<"Enter the size of array: "; + cin>>n; + + cout<<"Enter the elements of the array: "; + for(int i=0; i>x; + arr.push_back (x); + } + + findPermutation(arr, index, solution); + sort(solution.begin(), solution.end()); + + cout<<"\n\nThe permutation of entered array:\n"; + for(int i=0; i "; + for(int j=0; j +using namespace std; + +int getPivot(int arr[], int size) { + +} + +int main() { + int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int size = 10; + cout<<"Pivot element : "< +using namespace std; + +void powerSet(vector arr, vector output, int index, vector>& solution) { + if(index >= arr.size()) { + solution.push_back (output); + return; + } + + // Excluding the element + powerSet(arr, output, index+1, solution); + + // Including the element + output.push_back (arr[index]); + powerSet(arr, output, index+1, solution); +} + +int main() { + int n; + cout<<"Enter the size of array: "; + cin>>n; + + vector arr; + cout<<"Enter the elements of array: "; + for(int i=0; i>x; + arr.push_back (x); + } + + vector> solution; + vector output; + int index = 0; + + powerSet(arr, output, index, solution); + + cout<<"\n\nAll the power sets are:\n"; + for(int i=0; i +using namespace std; + +long long calcPower_1(int n, int p) { + if(p==0) { + return 1; + } + + return n * calcPower_1(n, p-1); +} + +// More optimized approach +long long calcPower_2(int n, int p) { + if(p==0) { + return 1; + } + + if(p==1) { + return n; + } + + int ans = calcPower_2(n, p/2); + + return (p&1) ? (n * ans * ans) : (ans * ans); +} + +int main() { + int num, power; + + cout<<"Enter the number: "; + cin>>num; + + cout<<"Enter the power: "; + cin>>power; + + //int ans = calcPower_1(num, power); + int ans = calcPower_2(num, power); + + cout<<"\n"< +using namespace std; + +int partitionArray(int arr[], int start, int end) { + int pivot=arr[start], count=0; + + for(int i=start; i<=end; i++) { + if(arr[i] < pivot) { + count++; + } + } + + int index = start+count; + swap(arr[start], arr[index]); + + int i=start, j=end; + + while(iindex) { + if(arr[i]>pivot && arr[j]pivot){ + j--; + } + } +} + +void quickSort(int arr[], int start, int end) { + if(start>=end) { + return; + } + + int p = partitionArray(arr, start, end); + + quickSort(arr, start, p); + quickSort(arr, p+1, end); +} + +int main() { + int n; + cout<<"Enter the size of array: "; + cin>>n; + + int *arr = new int[n]; + cout<<"Enter the elements of array: "; + for(int i=0; i>arr[i]; + } + + quickSort(arr, 0, n-1); + + cout<<"\nThe sorted array is: "; + for(int i=0; i +using namespace std; + +string ans; +void revString_1(string str, int size) { + if(size==0) { + return; + } + + ans.push_back (str[size-1]); + + revString_1(str, size-1); +} + +void revString_2(string &str, int start, int end) { + if(start>end) { + return; + } + + swap(str[start], str[end]); + revString_2(str, start+1, end-1); +} + +void revString_3(string &str, int index) { + if(index > str.length()/2-1) { + return; + } + + swap(str[index], str[str.length()-1-index]); + revString_3(str, index+1); +} + +int main() { + string str; + cout<<"Enter the string: "; + cin>>str; + + //revString_1(str, str.length()); + //revString_2(str, 0, str.length()-1); + revString_3(str, 0); + + cout<<"\nReverse of the given string is: "< +using namespace std; + +int sqRoot(int n, int start, int end) { + // Base Case + if(start>end) + return 0; + + int mid = start + (end-start)/2; + long long int square = mid*mid; + + // True Condition + if(square == n) + return mid; + + int ans; + if(square < n) { + // Lowest Possible SQRT + ans = mid; + int temp = sqRoot(n, mid+1, end); + if(temp) + ans = temp; + } else + ans = sqRoot(n, start, mid-1); + return ans; +} + +int main() { + int num; + cin>>num; + cout<<"Square root : "< +using namespace std; + +void allSubsequences(string str, string output, int index, vector& solution) { + if(index >= str.length()) { + if(output != "") { + solution.push_back (output); + } + return; + } + + // Excluding the character + allSubsequences(str, output, index+1, solution); + + // Including the character + output.push_back (str[index]); + allSubsequences(str, output, index+1, solution); +} + +int main() { + string str; + cout<<"Enter the string: "; + cin>>str; + + vector solution; + string output; + int index = 0; + + allSubsequences(str, output, index, solution); + + sort(solution.begin(), solution.end()); + + cout<<"\n\nAll the subsequences are:\n"; + for(int i=0; i Date: Sat, 8 Oct 2022 20:30:24 +0530 Subject: [PATCH 2/2] CPP problems related to Rotated Arrays --- Rotated Array/CheckRotatedSorted.cpp | 35 ++++++++++ Rotated Array/PeakInMountainArray.cpp | 39 +++++++++++ Rotated Array/PivotElement.cpp | 38 +++++++++++ Rotated Array/RotateArray.cpp | 37 +++++++++++ Rotated Array/SearchInRotatedSortedArray.cpp | 70 ++++++++++++++++++++ 5 files changed, 219 insertions(+) create mode 100644 Rotated Array/CheckRotatedSorted.cpp create mode 100644 Rotated Array/PeakInMountainArray.cpp create mode 100644 Rotated Array/PivotElement.cpp create mode 100644 Rotated Array/RotateArray.cpp create mode 100644 Rotated Array/SearchInRotatedSortedArray.cpp diff --git a/Rotated Array/CheckRotatedSorted.cpp b/Rotated Array/CheckRotatedSorted.cpp new file mode 100644 index 0000000..a852250 --- /dev/null +++ b/Rotated Array/CheckRotatedSorted.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +void check(int arr[], int n) { + int count=0; + for(int i=0; i arr[i+1]) + count++; + } + + if(arr[n-1] > arr[0]) { + count++; + } + + if(count <= 1) { + cout<<"True"; + } else { + cout<<"False"; + } +} + +int main() { + int arr[100], n, k; + cout<<"\nEnter the size of array: "; + cin>>n; + + cout<<"\nEnter the elements of array: "; + for(int i=0; i>arr[i]; + } + + check(arr, n); + + return 0; +} \ No newline at end of file diff --git a/Rotated Array/PeakInMountainArray.cpp b/Rotated Array/PeakInMountainArray.cpp new file mode 100644 index 0000000..086ba51 --- /dev/null +++ b/Rotated Array/PeakInMountainArray.cpp @@ -0,0 +1,39 @@ +// Write a program to find the index of the peak element in a mountain array + +#include +using namespace std; + +int FindPeak(int arr[], int n) { + int s = 0, e = n-1; + int m = s + (e-s)/2; + + while(s>n; + + cout<<"\nEnter the elements of array: "; + for(int i=0; i>arr[i]; + } + + cout<<"\n\nIndex of peak element is: "< +using namespace std; + +int FindPivot(int arr[], int size) { + int start=0, end=size-1; + int mid = (start + ((end-start)/2)); + + while(start < end) { + if(arr[mid] >= arr[0]) { + start = mid + 1; + } else { + end = mid; + } + + mid = (start + ((end-start)/2)); + } + + return start; +} + +int main() { + int arr[100], n, p; + + cout<<"Enter the size of the array: "; + cin>>n; + + cout<<"Enter the elements of the array: "; + for(int i=0; i>arr[i]; + } + + p = FindPivot(arr, n); + + cout<<"\nThe pivot element in the entered array is: "< +using namespace std; + +void rotate(int arr[], int n, int key) { + int temp[100]; + for(int i=0; i>n; + + cout<<"\nEnter the elements of array: "; + for(int i=0; i>arr[i]; + } + + cout<<"\nEnter the key to rotate array: "; + cin>>k; + + rotate(arr, n, k); + + cout<<"\n\nThe rotated array is: "; + for(int i=0; i +using namespace std; + +int FindPivot(int arr[], int size) { + int start=0, end=size-1; + int mid = (start + ((end-start)/2)); + + while(start < end) { + if(arr[mid] >= arr[0]) { + start = mid + 1; + } else { + end = mid; + } + + mid = (start + ((end-start)/2)); + } + + return start; +} + +int BinarySearch(int arr[], int start, int end, int k) { + int mid = (start + (end - start)/2); + + while(start <= end) { + if(arr[mid] == k) { + return mid; + } + + if(arr[mid] < k) { + start = mid + 1; + } + else { + end = mid - 1; + } + + mid = (start + (end - start)/2);c--; + } + + return -1; +} + +int main() { + int arr[100], n, i, k, p; + + cout<<"Enter the size of the array: "; + cin>>n; + + cout<<"Enter the elements of the array: "; + for(int i=0; i>arr[i]; + } + + cout<<"Enter the element to search: "; + cin>>k; + + p = FindPivot(arr, n); + + if((k <= arr[n-1]) && (k >= arr[p])) { + i = BinarySearch(arr, p, n-1, k); + } else { + i = BinarySearch(arr, 0, p-1, k); + } + + if(i == -1) + cout<<"\nThe element "<