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
12 changes: 12 additions & 0 deletions 1221. Split a String in Balanced Strings.cpp
Original file line number Diff line number Diff line change
@@ -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;

}
};
58 changes: 58 additions & 0 deletions Inorder Traversal of Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bits/stdc++.h>

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;
}
77 changes: 77 additions & 0 deletions N Queen Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>

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;
}
18 changes: 18 additions & 0 deletions Power of 2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Author: ADARSH
// Date Modified: 01/10/2022

#include <bits/stdc++.h>
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;
}
82 changes: 82 additions & 0 deletions Search_an_element_in_a_sorted_and_rotated_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// C++ Program to search an element
// in a sorted and pivoted array

#include <bits/stdc++.h>
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;
}
77 changes: 77 additions & 0 deletions Sudoku Solver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>

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;
}
28 changes: 28 additions & 0 deletions balance-parenthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<bits/stdc++.h>
using namespace std;

bool isValid(string s) {
stack<char>st;
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"<<endl;
cin>>s;
if(isValid(s))
cout<<"True"<<endl;
else
cout<<"False"<<endl;
return 0;
}