Skip to content

Instantly share code, notes, and snippets.

@Rockbet
Last active May 7, 2019 01:29
Show Gist options
  • Save Rockbet/f78484afa40c956c4414cdf9f0eb8acd to your computer and use it in GitHub Desktop.
Save Rockbet/f78484afa40c956c4414cdf9f0eb8acd to your computer and use it in GitHub Desktop.
// Noic - Ideia 4
// Exercício 2
// Complexidade: O(N)
// Por Leonardo Paes
#include <bits/stdc++.h>
using namespace std;
int v[3000000];
int main(){
int n, x, resposta = 0, soma;
cin >> n >> x;
for(int i = 1; i <= n; i++){
cin >> v[i];
v[n+i]=v[i];
}
soma=v[1];
for(int l=1, r=1; l<=n;){
if(r-l>n){
soma-=v[l];
l++;
}
else if(soma<x){
r++;
soma+=v[r];
}
else if(soma>x){
soma-=v[l];
l++;
}
else if(soma == x){
resposta++;
r++;
soma+=v[r];
soma-=v[l];
l++;
}
}
cout << resposta << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment