From 5d27369d7f1ec27e9fb56dbca8e2dbf420fde8c9 Mon Sep 17 00:00:00 2001 From: Yogesh-7523 Date: Sat, 1 Oct 2022 23:15:57 +0530 Subject: [PATCH] Some Important Questions Added --- 1221. Split a String in Balanced Strings.cpp | 12 +++ Inorder Traversal of Binary Tree.cpp | 58 +++++++++++++ N Queen Problem.cpp | 77 +++++++++++++++++ Power of 2.cpp | 18 ++++ ..._element_in_a_sorted_and_rotated_Array.cpp | 82 +++++++++++++++++++ Sudoku Solver.cpp | 77 +++++++++++++++++ balance-parenthesis.cpp | 28 +++++++ 7 files changed, 352 insertions(+) create mode 100644 1221. Split a String in Balanced Strings.cpp create mode 100644 Inorder Traversal of Binary Tree.cpp create mode 100644 N Queen Problem.cpp create mode 100644 Power of 2.cpp create mode 100644 Search_an_element_in_a_sorted_and_rotated_Array.cpp create mode 100644 Sudoku Solver.cpp create mode 100644 balance-parenthesis.cpp diff --git a/1221. Split a String in Balanced Strings.cpp b/1221. Split a String in Balanced Strings.cpp new file mode 100644 index 0000000..259461c --- /dev/null +++ b/1221. Split a String in Balanced Strings.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int balancedStringSplit(string s) { + int res = 0, cnt = 0; + for (const auto& c : s) { + cnt += c == 'L' ? 1 : -1; + if (cnt == 0) ++res; + } + return res; + +} +}; \ No newline at end of file diff --git a/Inorder Traversal of Binary Tree.cpp b/Inorder Traversal of Binary Tree.cpp new file mode 100644 index 0000000..303f1d7 --- /dev/null +++ b/Inorder Traversal of Binary Tree.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +struct node { + int data; + struct node * left, * right; +}; + +vector < int > inOrderTrav(node * curr) { + vector < int > inOrder; + stack < node * > s; + while (true) { + if (curr != NULL) { + s.push(curr); + curr = curr -> left; + } else { + if (s.empty()) break; + curr = s.top(); + inOrder.push_back(curr -> data); + s.pop(); + curr = curr -> right; + } + } + return inOrder; +} + +struct node * newNode(int data) { + struct node * node = (struct node * ) malloc(sizeof(struct node)); + node -> data = data; + node -> left = NULL; + node -> right = NULL; + + return (node); +} + +int main() { + + struct node * root = newNode(1); + root -> left = newNode(2); + root -> right = newNode(3); + root -> left -> left = newNode(4); + root -> left -> right = newNode(5); + root -> left -> right -> left = newNode(8); + root -> right -> left = newNode(6); + root -> right -> right = newNode(7); + root -> right -> right -> left = newNode(9); + root -> right -> right -> right = newNode(10); + + vector < int > inOrder; + inOrder = inOrderTrav(root); + + cout << "The inOrder Traversal is : "; + for (int i = 0; i < inOrder.size(); i++) { + cout << inOrder[i] << " "; + } + return 0; +} diff --git a/N Queen Problem.cpp b/N Queen Problem.cpp new file mode 100644 index 0000000..262fd86 --- /dev/null +++ b/N Queen Problem.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; +class Solution { + public: + bool isSafe1(int row, int col, vector < string > board, int n) { + // check upper element + int duprow = row; + int dupcol = col; + + while (row >= 0 && col >= 0) { + if (board[row][col] == 'Q') + return false; + row--; + col--; + } + + col = dupcol; + row = duprow; + while (col >= 0) { + if (board[row][col] == 'Q') + return false; + col--; + } + + row = duprow; + col = dupcol; + while (row < n && col >= 0) { + if (board[row][col] == 'Q') + return false; + row++; + col--; + } + return true; + } + + public: + void solve(int col, vector < string > & board, vector < vector < string >> & ans, int n) { + if (col == n) { + ans.push_back(board); + return; + } + for (int row = 0; row < n; row++) { + if (isSafe1(row, col, board, n)) { + board[row][col] = 'Q'; + solve(col + 1, board, ans, n); + board[row][col] = '.'; + } + } + } + + public: + vector < vector < string >> solveNQueens(int n) { + vector < vector < string >> ans; + vector < string > board(n); + string s(n, '.'); + for (int i = 0; i < n; i++) { + board[i] = s; + } + solve(0, board, ans, n); + return ans; + } +}; +int main() { + int n = 4; // we are taking 4*4 grid and 4 queens + Solution obj; + vector < vector < string >> ans = obj.solveNQueens(n); + for (int i = 0; i < ans.size(); i++) { + cout << "Arrangement " << i + 1 << "\n"; + for (int j = 0; j < ans[0].size(); j++) { + cout << ans[i][j]; + cout << endl; + } + cout << endl; + } + return 0; +} diff --git a/Power of 2.cpp b/Power of 2.cpp new file mode 100644 index 0000000..5ed4997 --- /dev/null +++ b/Power of 2.cpp @@ -0,0 +1,18 @@ +// Author: ADARSH +// Date Modified: 01/10/2022 + +#include +using namespace std; + +bool isPowerOf2(int n) { + return (n && !(n & (n - 1))); +} + +int main(int argc, char const *argv[]) +{ + int n; + cout << "Enter a number: "; + cin >> n; + cout << ((isPowerOf2(n)) ? "It's a power of 2." : "It's not a power of 2."); + return 0; +} diff --git a/Search_an_element_in_a_sorted_and_rotated_Array.cpp b/Search_an_element_in_a_sorted_and_rotated_Array.cpp new file mode 100644 index 0000000..2912dc9 --- /dev/null +++ b/Search_an_element_in_a_sorted_and_rotated_Array.cpp @@ -0,0 +1,82 @@ +// C++ Program to search an element +// in a sorted and pivoted array + +#include +using namespace std; + +// Standard Binary Search function +int binarySearch(int arr[], int low, int high, int key) +{ + if (high < low) + return -1; + + int mid = (low + high) / 2; + if (key == arr[mid]) + return mid; + + if (key > arr[mid]) + return binarySearch(arr, (mid + 1), high, key); + + return binarySearch(arr, low, (mid - 1), key); +} + +// Function to get pivot. For array 3, 4, 5, 6, 1, 2 +// it returns 3 (index of 6) +int findPivot(int arr[], int low, int high) +{ + // Base cases + if (high < low) + return -1; + if (high == low) + return low; + + // low + (high - low)/2; + int mid = (low + high) / 2; + if (mid < high && arr[mid] > arr[mid + 1]) + return mid; + + if (mid > low && arr[mid] < arr[mid - 1]) + return (mid - 1); + + if (arr[low] >= arr[mid]) + return findPivot(arr, low, mid - 1); + + return findPivot(arr, mid + 1, high); +} + +// Searches an element key in a pivoted +// sorted array arr[] of size n +int pivotedBinarySearch(int arr[], int n, int key) +{ + int pivot = findPivot(arr, 0, n - 1); + + // If we didn't find a pivot, + // then array is not rotated at all + if (pivot == -1) + return binarySearch(arr, 0, n - 1, key); + + // If we found a pivot, then first compare with pivot + // and then search in two subarrays around pivot + if (arr[pivot] == key) + return pivot; + + if (arr[0] <= key) + return binarySearch(arr, 0, pivot - 1, key); + + return binarySearch(arr, pivot + 1, n - 1, key); +} + +// Driver program to check above functions +int main() +{ + // Let us search 3 in below array + int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 }; + int n = sizeof(arr1) / sizeof(arr1[0]); + int key = 3; + + // Function calling + cout << "Index of the element is : " + << pivotedBinarySearch(arr1, n, key); + + return 0; +} diff --git a/Sudoku Solver.cpp b/Sudoku Solver.cpp new file mode 100644 index 0000000..262fd86 --- /dev/null +++ b/Sudoku Solver.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; +class Solution { + public: + bool isSafe1(int row, int col, vector < string > board, int n) { + // check upper element + int duprow = row; + int dupcol = col; + + while (row >= 0 && col >= 0) { + if (board[row][col] == 'Q') + return false; + row--; + col--; + } + + col = dupcol; + row = duprow; + while (col >= 0) { + if (board[row][col] == 'Q') + return false; + col--; + } + + row = duprow; + col = dupcol; + while (row < n && col >= 0) { + if (board[row][col] == 'Q') + return false; + row++; + col--; + } + return true; + } + + public: + void solve(int col, vector < string > & board, vector < vector < string >> & ans, int n) { + if (col == n) { + ans.push_back(board); + return; + } + for (int row = 0; row < n; row++) { + if (isSafe1(row, col, board, n)) { + board[row][col] = 'Q'; + solve(col + 1, board, ans, n); + board[row][col] = '.'; + } + } + } + + public: + vector < vector < string >> solveNQueens(int n) { + vector < vector < string >> ans; + vector < string > board(n); + string s(n, '.'); + for (int i = 0; i < n; i++) { + board[i] = s; + } + solve(0, board, ans, n); + return ans; + } +}; +int main() { + int n = 4; // we are taking 4*4 grid and 4 queens + Solution obj; + vector < vector < string >> ans = obj.solveNQueens(n); + for (int i = 0; i < ans.size(); i++) { + cout << "Arrangement " << i + 1 << "\n"; + for (int j = 0; j < ans[0].size(); j++) { + cout << ans[i][j]; + cout << endl; + } + cout << endl; + } + return 0; +} diff --git a/balance-parenthesis.cpp b/balance-parenthesis.cpp new file mode 100644 index 0000000..e7c4b94 --- /dev/null +++ b/balance-parenthesis.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +bool isValid(string s) { + stackst; + for(auto it: s) { + if(it=='(' || it=='{' || it == '[') st.push(it); + else { + if(st.size() == 0) return false; + char ch = st.top(); + st.pop(); + if((it == ')' and ch == '(') or (it == ']' and ch == '[') or (it == '}' and ch == '{')) continue; + else return false; + } + } + return st.empty(); +} +int main() +{ + string s; + cout<<"Enter the string to check"<>s; + if(isValid(s)) + cout<<"True"<