-1

Can anyone help me understand what this piece of code does? His logic, what is the output etc

// currsum exceeds given sum by currsum
//  - sum. Find number of subarrays having
// this sum and exclude those subarrays
// from currsum by increasing count by
// same amount.
if (prevSum.find(currsum - sum) != prevSum.end())
    res += (prevSum[currsum - sum]);

The entire code: "Given an unsorted array of integers, find the number of subarrays having sum exactly equal to a given number k."

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


int cntSubarrays(vector<int>arr,int sum){
    //complete this method
    unordered_map<int, int> prevSum;
    int n = arr.size();
    int res = 0;
 
    // Sum of elements so far.
    int currsum = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Add current element to sum so far.
        currsum += arr[i];
 
        // If currsum is equal to desired sum,
        // then a new subarray is found. So
        // increase count of subarrays.
        if (currsum == sum)
            res++;
 
        // currsum exceeds given sum by currsum
        //  - sum. Find number of subarrays having
        // this sum and exclude those subarrays
        // from currsum by increasing count by
        // same amount.
        if (prevSum.find(currsum - sum) != prevSum.end())
            res += (prevSum[currsum - sum]);
 
        // Add currsum value to count of
        // different values of sum.
        prevSum[currsum]++;
    }
 
    return res;
}
Emil Rowland
  • 538
  • 5
  • 25
  • 1
    `#include` & `using namespace std;` <-- don't do this. The first is not a standard library (and will include a number of unnecessary libraries) and the second will put the whole `std` namespace into the global namespace, which could cause all kinds of problems. – JHBonarius Aug 26 '22 at 07:57
  • Also please clarify what you want to hear: are you not understanding the code, or the algorithm? – JHBonarius Aug 26 '22 at 08:03
  • It was answered here: Those two lines you ask is the typical way to look if a certain key is in the map and get the value associated with that key. You have to check if there is a (key, value) pair in the map before accessing it with operator[], because if there is none then map[key] will insert a pair for that key with default value of the value type which is usually not what we want. I didn't understand the logic of the code, what exactly it was doing with the variables. Thank you for the tips! – Alana Fernandes Aug 29 '22 at 16:19
  • Yes, I can see that. Please don't repeat the whole anwer in the comments... instead, In that case accept it as the correct answer please. That way stack overflow wil close the question as being answered, and reward the person giving you that anwer as a thank you. That's how the site works – JHBonarius Aug 30 '22 at 05:11

1 Answers1

1

Those two lines you ask is the typical way to look if a certain key is in the map and get the value associated with that key. You have to check if there is a (key, value) pair in the map before accessing it with operator[], because if there is none then map[key] will insert a pair for that key with default value of the value type which is usually not what we want.

It is described here

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41