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

using namespace std;

// 0-> unvisited
// 1-> processing
// 2-> exited
vector<int> visited;
bool found = false;
vector<int> ans;
vector<int> curStack;

void DFS(vector<int> graph[], int node){
if (visited[node] != 0) return;

visited[node] = 1;
curStack.push_back(node);

for (int neighbour: graph[node]){
if (visited[neighbour] == 1){
// Found a cycle
found = true;
ans.push_back(neighbour);
while (curStack.back() != neighbour){
ans.push_back(curStack.back());
curStack.pop_back();
}
ans.push_back(neighbour);
reverse(ans.begin(), ans.end());
return;
}

else if (visited[neighbour] == 0){
DFS(graph, neighbour);
}
if (found) return;
}

curStack.pop_back();
visited[node] = 2; // Done processing
}

void solve(vector<int> graph[], int n){
for (int i=0; i<n; i++){
if (!visited[i]){
DFS(graph, i);
if (found) break;
}
}
if (! found){
cout << "NO" << endl;
}
else{
cout << "YES" << endl;
cout << ans.size() << endl;
for (int ele: ans){
cout << ele + 1 << " ";
}
cout << "\n";
}
}

int main(){
int n, m;
cin >> n >> m;

vector<int> graph[n];
visited.resize(n+1);
fill(visited.begin(), visited.end(), false);

while (m--){
int u, v;
cin >> u >> v;
u--; v--;
graph[u].push_back(v);
}
solve(graph, n);


return 0;
}
52 changes: 52 additions & 0 deletions C++/FindMiddleElementLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>

using namespace std;

struct Node{
int val;
struct Node *next;
};

// typedef Node* nodeptr;


Node *head = NULL;

void insert(int data){
Node *newNode = new Node;
newNode->val = data;
newNode->next = head;
head = newNode;
}

struct Node* findMiddle(){
Node* slowptr = head;
Node* fastptr = head;

while(fastptr && fastptr->next){
slowptr = slowptr->next;
fastptr = fastptr->next->next;
}
return slowptr;
}

void display(){
Node *curNode = head;
while (curNode != NULL){
cout << curNode->val << " ";
curNode = curNode->next;
}
cout << "\n";
}

int main(){
insert(5);
insert(10);
insert(20);
insert(25);
insert(30);
display();
Node* mid = findMiddle();
cout << mid->val << endl;
return 0;
}
114 changes: 114 additions & 0 deletions C++/NQueens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <bits/stdc++.h>

using namespace std;

const int size = 4;
int board[size][size] = {0};

void printBoard(){
// Output the state of the board
for (int i=0; i<size; i++){
for (int j=0; j<size; j++){
cout << board[i][j] << " ";
}
cout << "\n";
}
}

bool CheckPossible(int col, int row){
// Check if there is a queen in the same row
for (int i=0; i<size; i++){
if (board[row][i] == 1) {
cout << "1" << endl;
return false;
}
}

// Check if there is a queen in the same column
for (int i=0; i<size; i++){
if (board[i][col] == 1) {
cout << "2" << endl;
return false;
}
}

// Check if there is a queen in the upper right diagonal
for (int i=row, j=col; i>=0 && j<size; i--, j++){
if (board[i][j] == 1){
cout << "3" << endl;
return false;
}
}

// Check if ther is a queen in the lower right diagonal
for (int i=row, j=col; i<size && j>=0; i++, j--){
if (board[i][j] == 1) {
cout << "4" << endl;
return false;
}
}

// Check if there is a qeen in the upper left diagonal
for (int i=row, j=col; i>=0 && j>=0; i--, j--){
if (board[i][j] == 1) {
cout << "5" << endl;
return false;
}
}

// Check if there is a qeen in the lower left diagonal
for (int i=row, j=col; i<size && j<size; i++, j++){
if (board[i][j] == 1){
cout << "6" << endl;
return false;
}
}

return true; // If there is no queen intersecting the placement of the current queen
}


bool NQueens(int col){
if (col == size){
return true;
}
// Travers through every row
for (int i=0; i<size; i++){
// Check if we can place the queen in the current square
if (CheckPossible(col, i)){

// Debugging
printBoard();
cout << endl;

// If it is possible to place the queen place the queen
board[i][col] = 1;
printBoard();
cout << endl;
// Check we can place another queen in the next column
if (NQueens(col+1)){
return true;
}
// If not remove the current queen and continue backtracking
else{
board[i][col] = 0;
}
}
}
// If we couldn't find any place for a queen in the current column
return false;
}



int main(){
if (NQueens(0)){
// If we find the solution we print the board
printBoard();
}
else{
// Otherwise
cout << "There exists no solution" << endl;
}
return 0;
}
23 changes: 23 additions & 0 deletions C++/TowersOfHanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>

using namespace std;

void TowersOfHanoi(int Stand_from, int Stand_mid, int Stand_to, int diskNum){
// Base case: Only one disk
if (diskNum == 1){
cout << "Move disk " << diskNum << " from stand " << Stand_from << " to stand " << Stand_to << endl;
return;
}
// Move the current disk to the spare stand
TowersOfHanoi(Stand_from, Stand_to, Stand_mid, diskNum-1);

cout << "Move disk " << diskNum << " from stand " << Stand_from << " to stand " << Stand_to << endl;
// Move the disk from the spare stand to the required stand
TowersOfHanoi(Stand_mid, Stand_from, Stand_to, diskNum-1);
}


int main(){
TowersOfHanoi(1, 2, 3, 3);
return 0;
}