g0501_0600.s0560_subarray_sum_equals_k.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
**Time Complexity (Big O Time):**
The program uses a single pass through the input array `nums`, maintaining a running sum `tempSum`. Within the loop, it checks if `tempSum - k` exists in the `sumCount` map, and if it does, it increments the result variable `ret`. This loop iterates through the entire `nums` array once.
1. The loop iterates through the entire `nums` array, which has 'n' elements, where 'n' is the length of the input array.
2. Inside the loop, the operations involving the `sumCount` map, such as checking for a key's existence and updating its value, have an average time complexity of O(1) due to the use of a hash map.
Therefore, the overall time complexity of the program is O(n), where 'n' is the length of the input array `nums`. The dominant factor is the loop that iterates through the array.
**Space Complexity (Big O Space):**
The space complexity of the program is determined by the additional data structures used, including the `sumCount` map and a few integer variables.
1. The `sumCount` map stores at most 'n' unique sums from the input array `nums`, where 'n' is the length of the array. Therefore, the space used by the map is O(n) in the worst case.
2. The other integer variables (`tempSum` and `ret`) used to keep track of the current sum and the result require constant space, O(1).
Hence, the overall space complexity of the program is O(n), where 'n' is the length of the input array `nums`. The dominant factor in the space complexity is the `sumCount` map.
In summary, the provided program has a time complexity of O(n) and a space complexity of O(n), where 'n' is the length of the input array `nums`. It efficiently counts the number of subarrays with a sum equal to the target 'k' using a map to store intermediate sums.