LeetCode 560 | Subarray Sum Equals K
Автор: CodingWithYash
Загружено: 2025-10-26
Просмотров: 1193
Описание:
Problem Statement:
Given an integer array and a number k, we have to find the number of subarrays whose sum equals exactly k.
Approach 1: Brute Force – O(n²)
✅ We use two nested loops to generate all subarrays
✅ For each subarray, we calculate the sum
✅ If sum == k → increase count
Approach 2: Optimized using Prefix Sum + HashMap – O(n)
✅ Maintain a running sum (prefixSum)
✅ Use a HashMap to store frequency of prefixSums
✅ If (prefixSum - k) exists in map → we found a valid subarray
✅ Add its frequency to the answer
--Tags--
#leetcode #leetcodechallenge #codingwithyash #datastructures #interviewquestions #placementprep #dsa #dsaforbeginners #javaprogramming
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: