HACKWITHINFY ROUND 1 QUESTION

I think you have unnecessarily complicated it, or otherwise, I have misunderstood the problem.

If you want to check if K operations can make the array non positive, consider subtracting B from everything K times. And then you want to check if you can reduce K more elements by (A-B) to make everything non positive. It should be simply O( N \log a[i]_{max} ) .

4 Likes

yep, you are correct
i kinda felt that there is a much simpler solution than binarysearching for x_i(number of times a is removed) but was too lazy to think about it
thanks a lot :smiley: :smiley:

can you please provide sudo code for your approach?

int count = 0;
sort(int a[],int n){
int temp,i,j;
for(i=0;i<n-1;i++){
for(j=i;j<n;j++)
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
subtract(int a[],int n,int b, int c){
count++;
a[n-1]=a[n-1]-b;
int i;
for(i=0;i<n-1;i++){
a[i]=a[i]-c;
}
}
int main(){
int i,j,n,k=0,b,c;
printf(“Enter size of array :”);
scanf("%d",&n);
int a[n];
printf(“Enter b which is big value”);
scanf("%d",&b);
printf(“Enter c which is small value”);
scanf("%d",&c);
for(i=0;i<n;i++){
printf(“Enter elements :”);
scanf("%d",&a[i]);
}
while(k==0){
k=1;
for(i=0;i<n;i++){
if(a[i]>-1){
k=0;
}
}
sort(a,n);
subtract(a,n,b,c);
}
printf("%d",count-1);
}

yashraj077 i think,this can be helpful to you.

Here’s the implementation of smarth gupta’s idea

CODE

#include<bits/stdc++.h>

using namespace std;


#define FOR(i,a,b) for(long i=a;i<b;i++)
#define pb push_back
#define ll long long
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) 
 


//================================================//

int main(){
fastio;

ll n,a,b; 
cin>>n>>a>>b;

ll A[n];

ll maxi=-9;

FOR(i,0,n){cin>>A[i];maxi=max(maxi,A[i]);}


ll l=0,r=maxi,mid;

ll ansi=1e17;

while(l<=r){


	mid=(l+r)/2;


	ll B[n];

bool flag=true;
ll bb=mid*b;
ll op=mid;
ll aa=(a-b);
FOR(i,0,n){ B[i]=A[i]; B[i]-=bb;}

FOR(i,0,n){ 

	while(B[i]>0&&op>0){
	ll mul=B[i]/aa; 
	ll rem=B[i]%aa;
	if(rem!=0){mul++;}
	if(mul<=op)
	{ B[i]-=aa*mul;
	op-=mul;}else{break;}
	} 

if(B[i]>0){flag=false; break;} 
}

if(flag){  r=mid-1;  ansi=min(ansi,mid); }
else{l=mid+1;  }

}

 
cout<<ansi<<"\n";


return 0;}
2 Likes

O(N log a[i] max)

Code
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin>>n;
    vector<ll> g(n);
    for(auto&u:g) cin>>u;
    ll a,b;
    cin>>a>>b;
    ll l=0,r=1e9;
    ll dif=a-b;
    ll ans=r;
    while(l<=r)
    {
      ll m=l+(r-l)/2;
      vector<ll> v=g;
      for(ll i=0;i<n;i++) v[i]-=(m*b);
      ll rem=m;
      bool ok=true;
      for(ll i=0;i<n;i++)
      {
        if(v[i]>0 and rem>0)
        {
          ll req=(v[i]+dif-1)/dif;
          ll used=min(rem,req);
          v[i]-=(used*dif);
          rem-=used;
          if(v[i]>0)
          {
            ok=false;
            break;
          }
        }
      }
      if(ok)
      {
        ans=m;
        r=m-1;
      }
      else l=m+1;
    }
    cout<<ans<<endl;
  return 0;
}

I think this is what u meant guptosity sir.

Regards
Mr Trash

1 Like

@tamo11 Have a look at this one please and if possible can you give me some direction for the solution. Thanks in advance!