diff --git a/MaxMin using divide and conquer.cpp b/MaxMin using divide and conquer.cpp new file mode 100644 index 0000000..3ca6e78 --- /dev/null +++ b/MaxMin using divide and conquer.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; +void in(int a[],int n){ + for(int i=0;i>a[i]; +} +void print(int a[],int n){ + for(int i=0;ib) + return a; + return b; +} +int getMin(int a,int b){ + if(a a,int n){ + for(int y=0;y MaxMin(int a[],int i,int n){ +// show(a,i,n); + if(i==n){ + vector h; + h.push_back(a[i]); + h.push_back(a[i]); + return h; + } + int mid=(i+n)/2; + vector jj; + vector m=MaxMin(a,i,mid); + vector k=MaxMin(a,mid+1,n); + jj.push_back(getMax(m[0],k[0])); + jj.push_back(getMin(m[1],k[1])); + return jj ; + +} +int main(void){ + int n; + cout<<"Size of the array-"; + cin>>n; + int a[n]; + cout<<"ENTER ELEMENTS-"; + in(a,n); + vector o=MaxMin(a,0,n-1); + cout<<"Max "< +#define list vector +using namespace std; +class nqueen{ + vector sol; + int n; + bool **b; + public: + nqueen(int v){ + n=v; + b=(bool**)malloc(n*sizeof(bool*)); + for(int i=0;i=0 and k=0 and k=0 and l>=0;k--,l--){ + if(b[k][l]) + return false; + } + for(int k=i,l=j;k=0;k++,l--){ + if(b[k][l]) + return false; + } + return true; + } + bool rec(int r,int c){ + if(c==n){ + list v; + for(int i=0;i>n; + nqueen q=nqueen(n); + q.soln(); +} diff --git a/parenthesis generator.cpp b/parenthesis generator.cpp new file mode 100644 index 0000000..28b9f82 --- /dev/null +++ b/parenthesis generator.cpp @@ -0,0 +1,54 @@ +//You are given a number n +//you have to print all valid combinations that can be made using n pairs of brackets + +#include +using namespace std; +vector AllParenthesis(int n) ; +// N is the number of pairs of parentheses +// Return list of all combinations of balanced parantheses +class Solution +{ + public: + //Helping recursive function to generate parenthesis + void ss(vector &a,string s,int c,int o,int n) + { + + if(o==0 and c==0){ + a.push_back(s); + return; + } + if(o>0){ + ss(a,s+"(",c,o-1,n); + } + + if(o AllParenthesis(int n) + { + vector a; + ss(a,"",n,n,n); + return a; + //returning the vector containing permutations of braces + } +}; + +// { Driver Code Starts. + + +int main() +{ + + int n; + cin>>n; + Solution ob; + vector result = ob.AllParenthesis(n); + sort(result.begin(),result.end()); + for (int i = 0; i < result.size(); ++i) + cout< +#define list vector +using namespace std; + + + // } Driver Code Ends +// User function template for C++ + +class Solution{ + public: + bool issafe(vector &m, int i,int j,int n){ + return (i=0 &&j>=0 && m[i][j]==1 ); + } + bool ss(vector &m,int i,int j,int n,string s,vector&res){ + if(i==n-1 and j==n-1 and m[i][j]==1){ + // s+="1"; + res.push_back(s); + return true; + } + if(issafe(m,i,j,n)) + + + { m[i][j]=0; + ss(m,i+1,j,n,s+'D',res); + ss(m,i-1,j,n,s+'U',res); + ss(m,i,j+1,n,s+'R',res); + ss(m,i,j-1,n,s+'L',res); + m[i][j]=1; + } + return false; + } + vector findPath(vector &m, int n) { + // Your code goes here + string s=""; + vector v; + ss(m,0,0,n,s,v); + + sort(v.begin(),v.end()); + // reverse(s.begin(),s.end()); + + // v.push_back(s); + return v; + } +}; + + + + +// { Driver Code Starts. + +int main() { + + int n; + cin >> n; + vector m(n, vector (n,0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cin >> m[i][j]; + } + } + Solution obj; + vector result = obj.findPath(m, n); + if (result.size() == 0) + cout << -1; + else + for (int i = 0; i < result.size(); i++) cout << result[i] << " "; + cout << endl; + + return 0; +} // } Driver Code Ends