LeetCode 560: Subarray Sum Equals K - C++/Java According to LeetCode, the following question is one of the most frequent interview questions asked by companies such as Facebook and Google. Here, I'm posting C++/Java codes, if you'd like to review, please do so.
LeetCode 560

Given an array of integers and an integer target (K), you need to find the
total number of continuous subarrays whose sum equals to target.
Example 1:
Input:nums = [1,1,1], target = 2 Output: 2
Constraints:
The length of the array is in range [1, 20,000].
The range of numbers in the array is [-1000, 1000] and the range of the integer target is [-1e7,
1e7].

C++
class Solution {
public:
    int subarraySum(vector&lt;int&gt; &amp;nums, int target) {
        map&lt;int, int&gt; prefix_sum;
        int sum = 0, subarrays = 0;
        prefix_sum[0]++;
        for (int index = 0; index &lt; nums.size(); index++) {
            sum += nums[index];
            subarrays += prefix_sum[sum - target];
            prefix_sum[sum]++;
        }

        return subarrays;
    }
};

Java
class Solution {
    public int subarraySum(int[] nums, int target) {
        int sum = 0, subarrays = 0;
        Map&lt;Integer, Integer&gt; prefixSum = new HashMap&lt;&gt;();
        prefixSum.put(0, 1);

        for (int index = 0; index != nums.length; index++) {
            sum += nums[index];

            if (prefixSum.get(sum - target) != null)
                subarrays += prefixSum.get(sum - target);

            prefixSum.put(sum, -~prefixSum.getOrDefault(sum, 0));
        }

        return subarrays;
    }
}

Reference

LeetCode 560
LeetCode 560 Discussion Board
I have some suggestion for the Java version
Always try to pass the size of the maximum size to the Collection / Map constructor when known
The map has a default size of 16 elements, if you have more elements, the map will have to resize its internal cache. By setting the size, you can prevent the resize and make your code faster.
In this case, you can set the maximum size since it's based on the size of the array + 1.
Map&lt;Integer, Integer&gt; prefixSum = new HashMap&lt;&gt;(nums.length + 1);

Extract the expression to variables when used multiple times
In your code, when you check if the key is present, you can extract the value to a variable to reuse it when present.
Before
if (prefixSum.get(sum - target) != null)
   subarrays += prefixSum.get(sum - target);

After
Integer currentValue = prefixSum.get(sum - target);
if (currentValue != null)
   subarrays += currentValue;

This will be better, since this will prevent the rechecking and the rehashing in the map.
Always add curly braces to loop &amp; if
In my opinion, it's a bad practice to have a block of code not surrounded by curly braces; I saw so many bugs in my career related to that, if you forget to add the braces when adding code, you break the logic / semantic of the code.
Before
if (prefixSum.get(sum - target) != null)
   subarrays += prefixSum.get(sum - target);

After
if (prefixSum.get(sum - target) != null) {
   subarrays += prefixSum.get(sum - target);
}

Extract some of the logic to methods.
In this case, I suggest to extract the map creation to a method; this will allow to group the logic and make the main code shorter.
public int subarraySum(int[] nums, int target) {
   Map&lt;Integer, Integer&gt; prefixSum = buildMap(lengthOfNums + 1);
   //[...]
}

private Map&lt;Integer, Integer&gt; buildMap(int defaultSize) {
   Map&lt;Integer, Integer&gt; prefixSum = new HashMap&lt;&gt;(defaultSize);
   prefixSum.put(0, 1);
   return prefixSum;
}