So I was trying to solve this CSES problem, but I couldn’t, so I looked at the editorial for a bit of help. I read the idea behind the solution and tried making a solution using sets instead of maps. Here is my solution,
#include <bits/stdc++.h>
using namespace std;
long long n,tar;
long long ans=0;
int main(){
cin >> n>>tar; //Get the input for the problem
long long arr[n+1];//Prefix array
set<long long> track={0};//keeps track if the sum we need is there
arr[0]=0;
for(int i=1;i<=n;i++){
long long j;
cin >>j;
arr[i]=arr[i-1]+j;
if(track.count(arr[i]-tar)) ans++;//If the set has the opposite answer, then add
track.insert(arr[i]);//Insert this for future cases
}
cout << ans << "\n";
return 0;
}
The idea behind the solution is the same, but I used sets. The solution works for most test cases except a few. Can you guys tell me why? Thanks!!