Answers for "codeforces 102397e solution"

C++
0

codeforces 102397e solution

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000005;
int n,x,ans=maxn;
ll a[maxn],sum[maxn];
int main(){
	scanf("%d%d",&n,&x);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	if(sum[n]<x) return cout<<-1,0;
	int l=0,temp=maxn;
//	for(int i=1;i<=n;i++) cout<<sum[i]<<" ";
//	cout<<endl;
	for(int r=1;r<=n;r++){
		if(sum[r]-sum[l]>=x){
			while(l<r){
				if(sum[r]-sum[l+1]>=x) l++;
				else break; 
			}
			if(r==1+l) return cout<<1,0;
			temp=r-l;
			ans=min(ans,temp);
//			cout<<l<<" "<<r<<" "<<temp<<" "<<ans<<endl;
		}
	}
	printf("%d",ans);
	return 0;
} 
12345678910111213141516171819202122232425262728293031
Posted by: Guest on September-19-2021

Code answers related to "codeforces 102397e solution"

Browse Popular Code Answers by Language