Skip to content

Commit d9b8785

Browse files
committed
Algorithms
1 parent a260c38 commit d9b8785

File tree

5 files changed

+99
-70
lines changed

5 files changed

+99
-70
lines changed

Diff for: Arrays&Strings/NQueenProblem.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
bool isSafe(int board[][10],int i,int j,int n){
4+
//check for the column
5+
for(int x=0;x<i;x++){
6+
if(board[x][j]==1)
7+
return false;
8+
}
9+
10+
//Check for top left diagonal
11+
int x=i,y=j;
12+
while(x>=0 && y>=0){
13+
if(board[x][y]==1){
14+
return false;
15+
}
16+
x--;
17+
y--;
18+
}
19+
20+
//Check for top right diagonal
21+
x=i,y=j;
22+
while(x>=0 && y<n){
23+
if(board[x][y]==1){
24+
return false;
25+
}
26+
x--;
27+
y++;
28+
}
29+
30+
//if position is safe to place the queen
31+
return true;
32+
33+
}
34+
bool solveNQueen(int board[][10],int i,int n){
35+
//Base Case
36+
if(i==n){
37+
//Print the board
38+
for(int i=0;i<n;i++){
39+
for(int j=0;j<n;j++){
40+
if(board[i][j]){
41+
cout << "Q ";
42+
}
43+
else
44+
cout << ". ";
45+
}
46+
cout << endl;
47+
}
48+
return true;
49+
}
50+
//Recursive Case
51+
for(int j=0;j<n;j++){
52+
//Lets try to place the queen at jth column
53+
if(isSafe(board,i,j,n)){
54+
board[i][j] = 1;
55+
bool nextQueen = solveNQueen(board,i+1,n);
56+
if(nextQueen==true){
57+
return true;
58+
}
59+
board[i][j] = 0;
60+
}
61+
}
62+
return false;
63+
}
64+
int main(int argc, char const *argv[])
65+
{
66+
int n;
67+
cout << "Enter the value of n\n";
68+
cin >> n;
69+
int board[10][10] = {0};
70+
solveNQueen(board,0,n);
71+
return 0;
72+
}

Diff for: Arrays&Strings/a.out

3.97 KB
Binary file not shown.

Diff for: Arrays&Strings/permutationsOfString.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
void permutations(string s,int i){
4+
if(i==s.length()){
5+
cout << s << endl;
6+
return ;
7+
}
8+
for(int j=i;j<s.length();j++){
9+
swap(s[i],s[j]);
10+
permutations(s,i+1);
11+
swap(s[i],s[j]);
12+
}
13+
return;
14+
}
15+
int main(int argc, char const *argv[])
16+
{
17+
string s;
18+
cin >> s;
19+
cout << "permutations\n";
20+
permutations(s,0);
21+
// cout << "string " << s << endl;
22+
return 0;
23+
}

Diff for: Recursion/NQueenProblem.cpp

-64
This file was deleted.

Diff for: SieveOfEratosthenes.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ std::vector<int> factorizationlog(int n){
6767
std::vector<int> res;
6868
std::map<int,int> m;
6969
std::map<int,int>::iterator it;
70-
while(n != 1){
71-
res.push_back(minPrime[n]);
72-
m[minPrime[n]]++;
73-
n /= minPrime[n];
70+
while(n != 1){
71+
res.push_back(minPrime[n]);
72+
m[minPrime[n]]++;
73+
n /= minPrime[n];
7474
}
7575
cout << "map is \n";
7676
for(it=m.begin();it!=m.end();it++){
@@ -79,8 +79,6 @@ std::vector<int> factorizationlog(int n){
7979
cout << endl;
8080
return res;
8181
}
82-
83-
8482
int main(int argc, char const *argv[])
8583
{
8684
int n;

0 commit comments

Comments
 (0)