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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ using namespace std;
// return 0;
// }
//print prime factorization

//spf:smallest prime factor
int main(){
int n;cin>>n;
int spf[n+1];
Expand Down
Binary file modified Aaliyah Beg/Basics of Programming/Getting Started/RotateANumber
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ int main() {
string n;
cin>>n>>k;
int k_new=abs(k)%n.length();
if(k_new<0){
cout<<n.substr(k_new-1)+n.substr(0,k_new-1);
if(k<0){
cout<<n.substr(k_new)+n.substr(0,k_new);
}
else if(k_new>0){
cout<<n.substr(n.length()- k_new + 1)+n.substr(0,n.length()- k_new + 1);
else if(k>0){
cout<<n.substr(n.length()- k_new )+n.substr(0,n.length()- k_new );
}
else{
cout<<n;
Expand Down
5 changes: 5 additions & 0 deletions Aaliyah Beg/Recursion and backtracking/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"iostream": "cpp"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include<bits/stdc++.h>
using namespace std;
int fac(int n){
//base case
if(n==1) return 1;
return n*fac(n-1);
}
int main(){
int n;cin>>n;
cout<<fac(n);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<bits/stdc++.h>
using namespace std;
int powerlinear(int x,int n){
//base case
if(n==0) return 1;
return x* powerlinear(x,n-1);
}
int main(){
int x;cin>>x;
int n;cin>>n;
cout<<powerlinear(x,n);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<bits/stdc++.h>
using namespace std;
int powerlogrithmic(int x,int n){
//base case
if(n==0) return 1;
//recursive case
if(n%2==0)
return x*x*powerlogrithmic(x,n/2-1)*powerlogrithmic(x,n/2-1);
else
return x*powerlogrithmic(x,n/2)*powerlogrithmic(x,n/2);
}
int main(){
int x;cin>>x;
int n;cin>>n;
cout<<powerlogrithmic(x,n);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<bits/stdc++.h>
using namespace std;
void printdecreasing(int n){
//base case
if(n==0) return;
cout<<n<<"\n";
printdecreasing(n-1);
return;
}
int main(){
int n;cin>>n;
printdecreasing(n);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<bits/stdc++.h>
using namespace std;
void toh(int n,int a,int b,int c){
//base case
if(n==0) return;
//recursive case
toh(n-1,a,c,b);
cout<<n<<"["<<a<<"->"<<b<<"] \n";
toh(n-1,c,b,a);
return;

}
int main(){
int n;cin>>n;
int a;cin>>a;
int b;cin>>b;
int c;cin>>c;
toh(n,a,b,c);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<bits/stdc++.h>
using namespace std;
void printincreasing(int n){
//base case
if(n==0) return;

printincreasing(n-1);
cout<<n<<"\n";
return;
}
int main(){
int n;cin>>n;
printincreasing(n);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<bits/stdc++.h>
using namespace std;
void printincreasingdecreasing(int n){
//base case
if(n==0) return;
cout<<n<<"\n";
printincreasingdecreasing(n-1);
cout<<n<<"\n";
return;
}
int main(){
int n;cin>>n;
printincreasingdecreasing(n);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include<bits/stdc++.h>
using namespace std;
void printzigzag(int n){

}
int main(){
int n;cin>>n;
printzigzag(n);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
vector<string> floodfill(int a[][1000],bool v[][1000],int n,int m,int i,int j){

//base case
if( j==-1 || i==-1 || i==n || j==m || a[i][j]==1 || v[i][j]==true){
vector<string>base_ans;
return base_ans;
}
if(i==n-1 && j==m-1){
vector<string>base_ans;
base_ans.pb("");
return base_ans;
}
//recursive case
vector<string>bada_ans;
v[i][j]=true;
vector<string>chota_ans1=floodfill(a,v,n,m,i-1,j);

for(auto x:chota_ans1){
bada_ans.pb('u'+x);
}
vector<string>chota_ans2=floodfill(a,v,n,m,i+1,j);

for(auto x:chota_ans2){
bada_ans.pb('d'+x);
}
vector<string>chota_ans3=floodfill(a,v,n,m,i,j-1);

for(auto x:chota_ans3){
bada_ans.pb('l'+x);
}
vector<string>chota_ans4=floodfill(a,v,n,m,i,j+1);

for(auto x:chota_ans4){
bada_ans.pb('r'+x);
}
v[i][j]=false;

return bada_ans;
}
int main(){
int n,m;
cin>>n>>m;
int a[1000][1000];
bool v[1000][1000];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
v[i][j]=false;
}
}
// int b=n-1;
// int c=m-1;
vector<string>ans=floodfill(a,v,n,m,0,0);

for(auto x:ans){
cout<<x<<"\n";
}
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
void floodfill(int a[][1000],bool v[][1000],int n,int m,int i,int j,string psf){
//base case
if( j==-1 || i==-1 || i==n || j==m || a[i][j]==1 || v[i][j]==true){
return;
}
if(i==n-1 && j==m-1){
cout<<psf<<"\n";
return;
}
//recursive case

v[i][j]=true;
floodfill(a,v,n,m,i-1,j,psf+'u');
floodfill(a,v,n,m,i+1,j,psf+'d');
floodfill(a,v,n,m,i,j-1,psf+'l');
floodfill(a,v,n,m,i,j+1,psf+'r');
v[i][j]=false;
return;

}
int main(){
int n,m;
cin>>n>>m;
int a[1000][1000];
bool v[1000][1000];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
v[i][j]=false;
}
}
// int b=n-1;
// int c=m-1;

string psf;
floodfill(a,v,n,m,0,0,psf);
return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
void knightstour(int a[1000][1000],int n,int r,int c,int step){
//base case
if(r<0 || c<0 || r>=n || c>=n || a[r][c]!=0){
return ;
}
if(step==n*n) {
a[r][c]=step;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n-------------------------- \n";
a[r][c]=0;
return;
}
a[r][c]=step;
knightstour(a,n,r-2,c+1,step+1);
knightstour(a,n,r-1,c+2,step+1);
knightstour(a,n,r+1,c+2,step+1);
knightstour(a,n,r+2,c+1,step+1);
knightstour(a,n,r+2,c-1,step+1);
knightstour(a,n,r+1,c-2,step+1);
knightstour(a,n,r-1,c-2,step+1);
knightstour(a,n,r-2,c-1,step+1);
a[r][c]=0;
return;


}
int main(){
int n;cin>>n;
int r;cin>>r;
int c;cin>>c;
int a[1000][1000];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=0;
}
}
knightstour(a,n,r,c,1);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
bool check(int a[1000][1000],int i,int j,int n){
for(int x=i-1;x>-1;x--){

if(a[x][j]==1){
return false;
}
}
for(int x=1;x<=i and x<=j;x++){
if(a[i-x][j-x]==1){
return false;
}
}
for(int x=1;x<=i and x<n-j;x++){
if(a[i-x][j+x]==1){
return false;
}
}
return true;
}

void Nqueens(int a[1000][1000],int n,int i){
//base case
if(i==n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
return;
}
for(int j=0;j<n;j++){
if(check(a,i,j,n)){
a[i][j]=1;
Nqueens(a,n,i+1);
a[i][j]=0;
}
}
return;
}
// recursion ka tree visulaize karte hain usmein
//left mein pre written cheez hoti hai when we are going down
//phir recursive call
//phir right mein post written cheez hoti hai when we are going up
int main(){
int n;cin>>n;
int ans[1000][1000];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
ans[i][j]=0;
}
}
Nqueens(ans,n,0);
return 0;
}
Binary file not shown.
Loading