### iron_nicko's blog

By iron_nicko, history, 2 months ago,

Hey, I know the contest was postponed, but I was trying to solve this question:

Question:

Consider the integer sequence A[], where the elements are first N natural numbers in order.

You are now given two integers, L and S. Determine whether there exists a subarray with length L and sum S after removing at most one element from A.

A subarray of an array is a non-empty sequence obtained by removing zero or more elements from the front of the array, and zero or more elements from the back of the array.
1 <= N <= 10^9
1 <= L <= N - 1


I came up with something like this:

My Code

Test Cases with the answers:

3
5 3 11 # YES
5 3 5 # NO
5 3 6 # YES


My code seems to pass the initial test cases, but I'd like to know if this will work for all cases. Please let me know if this is correct.

Updated Code
• +3

 » 2 months ago, # | ← Rev. 6 →   0 this may help Spoiler#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include #define int long long #define all(a) a.begin(),a.end() #define endl "\n" #define fill(a,b) memset(a, b, sizeof(a)) #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) using namespace std; int func(int l,int r){ int x,y; l--; x=l*(l+1)/2; y=r*(r+1)/2; return y-x; } signed main (){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; cin>>t; while(t--){ int i,j,x=0,y=0,p=0,n,q=-1,u=0,v=0,k,c=0,m,s; int len; cin>>n>>len>>s; if(len>n){ cout<<"NO"<s){ y=ans-s; if(y>=l&&y<=r){ p1=1; break; } j=mid-1; } } i=1; j=n; len--; while(i<=j){//subarray of length len without deletion int mid=i+(j-i)/2; l=mid; r=mid+len-1; int ans=func(l,r); if(ans==s){ p1=1; break; } if(anss){ j=mid-1; } } if(p1==1){ cout<<"YES"<
•  » » 2 months ago, # ^ |   0 SpoilerIt helped but wouldn't mind others code/explanation as well. Thanks!
 » 2 months ago, # |   0 Auto comment: topic has been updated by iron_nicko (previous revision, new revision, compare).
 » 2 months ago, # |   0 Auto comment: topic has been updated by iron_nicko (previous revision, new revision, compare).
 » 2 months ago, # |   0 Can you help me with the fourth problem 'compatible subsequence' of this contest. here