diff --git a/Binary-Search/Books Allocation.cpp b/Binary-Search/Books Allocation.cpp new file mode 100755 index 0000000..0f2ab85 --- /dev/null +++ b/Binary-Search/Books Allocation.cpp @@ -0,0 +1,42 @@ +#define ll long long +#include +using namespace std; + +// Book allocation problems +// Why binary search? Dont just use it ! +// Brute force solution will be to check for each book +// note that books are to be allocated in continuous form else it would have been an n subset problem with minimum sum any approaches? +// Interviewer might can gove you a different problem which seems to be solved this way but actually could have been different +// STRATEGY-> +// Naive solution +// Notice the function graph +// boom! binary search cna be a solution + +bool is(ll x, vector & book,int m){ + ll cnt=1,curr=0; + for(int i=0;i &book, int m) { + int n=book.size(); + if((m==0&&n!=0) || m>n) return -1; + + ll lo=0,hi=0,res=0; + for(int i=0;i>1; + if(is(mid,book,m)) res=mid,hi=mid-1; + else lo=mid+1; + } + + return res; +} diff --git a/Binary-Search/Good Base.cpp b/Binary-Search/Good Base.cpp new file mode 100755 index 0000000..56bf23d --- /dev/null +++ b/Binary-Search/Good Base.cpp @@ -0,0 +1,43 @@ +#define ll long long +#include +using namespace std; +// still we not know when to use binary search! +string solve(string A) { + ll n = stoll(A); + ll res=n-1,temp,mul; + temp=(sqrt(4*n-3)-1)/2; + + if(n!=3 && temp*temp+temp+1==n) res=temp; + temp=0; + + for(int i=3;i<64;i++){ // at most 1e6+1e4+1e2... + ll j=1; temp=0; + // we can use binary search for this !!!! + while(temp= 2; --i) { + long long left = 2, right = pow(num, 1.0 / (i-1)) + 1; + while (left < right) { + long long mid = left + (right - left) / 2, sum = 0; + for (int j = 0; j < i; ++j) { + sum = sum * mid + 1; + } + if (sum == num) return to_string(mid); + else if (sum < num) left = mid + 1; + else right = mid; + } + } + return to_string(num - 1); +} +*/ \ No newline at end of file diff --git a/Binary-Search/Matrix Median.cpp b/Binary-Search/Matrix Median.cpp new file mode 100755 index 0000000..5a0bed2 --- /dev/null +++ b/Binary-Search/Matrix Median.cpp @@ -0,0 +1,28 @@ +// Given a rowWise sorted array find matrix median of the array +// Naive -> (N*M) log(N*M) Aux->space O(N*M) +// MEGRING SORTED ARRAYS-> (N*M) LOG(M) Aux->space O(N*M) +// BSearch-> (32*N*LOG(M)) Aux->space O(32)/ Dont say O(32) say constant +// in bsearch logn steps are there but each step is having O(1) space so even after the search extra space will be used only once +// but in Merging Sorted arrays recursive func is there so at any time space might be o(1)*logM steps + +#include +using namespace std; + +int cnt(int x, vector> &A){ + int cnt=0; + for(auto v:A) cnt+=(lower_bound(v.begin(),v.end(),x)-v.begin()); + return cnt; +} + +int findMedian(vector > &A) { + int hi=INT_MIN,lo=INT_MAX,res=INT_MIN,r=A.size(),c=A[0].size(); + + for(auto v:A) for(auto x:v) hi=max(hi,x), lo=min(lo,x); + while(lo<=hi){ + int mid= (lo+hi)>>1; + if(cnt(mid,A)*2 +using namespace std; +#define ll long long +#define vi vector +#define pb push_back +#define pii pair +#define fi first +#define se second +#define re cin>> +#define pr cout<< +#define all(x) x.begin(),x.end() +#define rep(i,n) for(int i=0;i solve(vector &A, vector &B) { + int n=A.size(); + vector times(n+1),order(n); + stack st; + for(int i=0;i> pp; + for(int i=0;i>()); + + ll sum=0; + for(int i=0;idp(A+1,0); + dp[1]=dp[0]=1; + for(int i=2;i<=A;i++) for(int j=0;j +#define ll int64_t +#define pii pair +#define mp make_pair +#define pb push_back +#define vi vector +#define fastio cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); + +using namespace std; +const ll mod =1e9+7; +const int N =2e5+1; + + +// for counting-> +int main() { + int d[3]={1,2,3}; + int s[3]={1,2,1}; + + cout<<"For diff->"; + int same=0,diff=0; + for(int i=1;i<=4;i++) + for(int j=1;j<=4;j++) + for(int k=1;k<=4;k++){ + if(d[0]==i|| d[1]==j || d[2]==k) continue; + if(i==j || j==k) continue; + if(i==k) same++; + else diff++; + } + cout<"; + same=0,diff=0; + for(int i=1;i<=4;i++) + for(int j=1;j<=4;j++) + for(int k=1;k<=4;k++){ + if(s[0]==i || s[1]==j|| s[2]==k) continue; + if(i==j || j==k) continue; + if(i==k) same++; + else diff++; + } + + cout<> isPad; +vector dp; + +void cr(string &A){ + isPad=vector>(A.size()+1,vector(A.size()+1)); + dp=vector(A.size()+1,-1); +} + +void generatePads(string &A){ + int n=A.size(); + for(int i=0;i +using namespace std; + +int kmp(string &pattern, string & text){ + int len=0; + vectorlps(pattern.size(),0); + for(int i=1;i & ar){ + // cerr<<"\nReduced to Vector:";for(auto s:ar) cerr< vec; + int n=ar.size(); + for(int i=0;i &ar,bool &at){ + // cerr<<"\nPassed Vector:";for(auto s:ar) cerr<r) return ar[1]+ar[0].substr(l); + return ar[0]+ar[1].substr(r); + } + int ii=0,jj=1,kk=2,mx=0,n=ar.size(); + for(int i=0;imx) {mx=x;ii=i,jj=j,kk=k;}} + else { if(x>=mx){mx=x;ii=i,jj=j,kk=k;}} + } + // cerr<<"\nMax reduction->"< justPassItDude; + for(int i=0;i &ar){ + sort(ar.begin(),ar.end()); + vector res; + for(int i=0;i &A) { + removeDups(A); + bool withEquality=0; + withEquality=1; + string res1 = getRes(A,withEquality); + string res2 = getRes(A,withEquality); + return min(res1.size(),res2.size()); +} + +int main() { + int n; + cin>>n; + vector vec(n); + for(int i=0;i>vec[i]; + cerr<<"\n"<