Basic Algorithms Course for Beginners : CodeNCode(1 Aug 2020 : New problem added)

also a video on spoj FCVSPOW spoj problem

I think the time has come for this problem to be solved

1 Aug 2020 : New practice Problem added
E001 : Koko Eating bananas | Leetcode | Binary Search

1 Like

@askswaraj Why 1e5 instead of 1e6? How to decide the limits? Wouldn’t 1e6 would be appropriate since max value of f(t) can be upto 1e18 according to the problem and there is t^3 too…

Thank u bhaiya…I always follow ur courses…Nd it
hlps me a alot…espically in number theory :smiley: :ok_hand:

Can anyone explain why it’s max range is upto only ceil(sqrt(k))

bhaiya , in ternary string question , i am getting difficulty to understand isValid function

Monk’s Encounter with Polynomial :

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
ll f(ll x,ll a,ll b,ll c){
    return (a*x*x) + (b*x) + c;
}

ll findX(ll a,ll b,ll c,ll k){
    ll L = 0;
    ll R = 100000;// or can be ceil(sqrt(k))
    ll ans = 0;
    while(L<=R){
        ll mid = L + (R-L)/2;
        if(f(mid,a,b,c)>=k){
            ans =  mid;
            R = mid-1;
        }
        else{
           L = mid +1;
        }
    }

    return ans;
}
void solve(){
    ll a,b,c,k;
    cin>>a>>b>>c>>k;
    cout<<findX(a,b,c,k)<<"\n";
}
int main(){
    ll t=1;
    cin>>t;
    while(t>0){
        solve();
        t--;
    }

}

For Ternary string code what is wrong with my approach


#include <bits/stdc++.h>
using namespace std;
string s;
bool valid(int k)
{

int ar[4]= {0};
for(int i=0;i<k;i++)
{

    ar[s[i]-'0']++;

}
for(int i=k-1;i<s.size();i++)
{

        if(ar[1]&&ar[2]&&ar[3])
        {

            return true;

        }
        ar[s[i]-'0']++;
        ar[s[i-k+1]-'0']--;

}
return false;

}

int solve()
{

cin>>s;
int l=3;
int h=s.size();
while(l<=h)
{

    int mid=(l+h)/2;
    if(valid(mid)&&(!valid(mid-1))) return mid;
    if(valid(mid)==false) l=mid+1;
    else h=mid-1;

}

}
int main()
{

int t;
cin>>t;
while(t--){
    int ans=solve();
    cout<<ans<<endl;

}
return 0;

}