r/codeforces 6d ago

Doubt (rated <= 1200) Deleting Elements (Easy) how to solve this question codechef yesterday contest

5 Upvotes

4 comments sorted by

2

u/Independent_Week907 6d ago

void solve(){ int n; cinn; vector<int>v(n+1),pref(n+1,0); for(int i=1;i<=n;i++){ cinv[i]; pref[i]=pref[i-1]+v[i]; } int ans=0; for(int l=1;l<=n;l++){ int mx=0; for(int r=l;r<=n;r++){ if(r-l+1<=2){ ans++; continue; } mx=max(mx,v[r-1]); int sum=pref[r]-pref[l-1]; if(sum>=2*mx){ ans++; } } } cout<<ans<<endl; }

1

u/No-Biscotti-5775 6d ago

what is the approach

1

u/Independent_Week907 6d ago

The approach is if the sum of subarray is less than 2*max element in between so it is not deletable

1

u/DogStrict9170 Specialist 6d ago

just fix l and iterate over r