#invalid initialization of reference of type 'stdvectorint&' from expression of type 'int'

9 messages · Page 1 of 1 (latest)

surreal flareBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

jagged ocean
#

!f

surreal flareBOT
#

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

int countSubarraysWithSumK(vector<int>& a, int K) {
  int n = a.size();
  unordered_map<int, int> hash;
  int count = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
    if (sum == K) {
      count++;
    }
    if (hash.find(sum - K) != hash.end()) {
      count += hash[sum - K];
    }
    hash[sum]++;
  }
  return count;
}

int main() {
  int n, K;
  cin >> n;
  cin >> K;
  int a[n];

  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  cout << countSubarraysWithSumK(n, K) << endl;
  return 0;
}

Here is my code and when I am trying to compile i get this error message . Can someone help me? ( This is the only error that I get)

Amerikanos
jagged ocean
#

The error tells you everything

#

countSubarraysWithSumK's first parameter is a reference to a vector of integers, you're trying to pass a single integer

#

int a[n]; is a variable length array (VLA), which is bad. You should use an std::vector for this.

steel canopy
#

thank you very much!

surreal flareBOT
#

@steel canopy

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.

steel canopy
#

!solved