Submission details
Task:Subarray Sums II
Sender:francden
Submission time:2025-11-24 00:54:50 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int findAllSubarraysWithGivenSum(std::vector<int>&, int, int)':
input/code.cpp:6:9: error: class template argument deduction failed:
    6 |     map mpp;
      |         ^~~
input/code.cpp:6:9: error: no matching function for call to 'map()'
In file included from /usr/include/c++/11/map:61,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:81,
                 from input/code.cpp:2:
/usr/include/c++/11/bits/stl_map.h:290:9: note: candidate: 'template<class _Key, class _Tp, class _Compare, class _Alloc, class _InputIterator> map(_InputIterator, _InputIterator, const _Compare&, const _Alloc&)-> std::map<_Key, _Tp, _Compare, _Alloc>'
  290 |         map(_InputIterator __first, _InputIterator __last,
      |         ^~~
/usr/include/c++/11/bits/stl_map.h:290:9: note:   template argument deduction/substitution failed:
input/code.cpp:6:9: note:   candidate expects 4 arguments, 0 provided
    6 |     map mpp;
      |         ^~~
In file incl...

Code

#include <bits/stdc++.h>
using namespace std;

int findAllSubarraysWithGivenSum(vector < int > & arr, int k,int n) {
    map mpp;
    int preSum = 0, cnt = 0;

    mpp[0] = 1; // Setting 0 in the map.
    for (int i = 0; i < n; i++) {
        // add current element to prefix Sum:
        preSum += arr[i];

        // Calculate x-k:
        int remove = preSum - k;

        // Add the number of subarrays to be removed:
        cnt += mpp[remove];

        // Update the count of prefix sum
        // in the map.
        mpp[preSum] += 1;
    }
    return cnt;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long x;
    cin >> n >> x;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    int cnt = findAllSubarraysWithGivenSum(a, x,n);
    cout <<  cnt << "\n";
    return 0;
}