How do you count subarrays whose sum equals k?
Simple meaning
Keep a prefix-sum frequency map starting with 0:1.
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready DSA interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
Keep a prefix-sum frequency map starting with 0:1.
Open the full page for Why, Steps, Example and Key takeaway.
Put all numbers in a set, then only start a streak from values that have no predecessor x-1, and walk x+1, x+2, ...
Open the full page for Why, Steps, Example and Key takeaway.
Hash all sums of A[i]+B[j], then for each C[k]+D[l] add the count of the negated sum.
Open the full page for Why, Steps, Example and Key takeaway.
Same as Two Sum: one pass with value-to-index map, looking up target-x before inserting x so you never reuse the same index.
Open the full page for Why, Steps, Example and Key takeaway.
Sort, then for each index i run two pointers on the right side looking for -nums[i], skipping duplicates at all three positions.
Open the full page for Why, Steps, Example and Key takeaway.
Start at both ends and move the pointer at the shorter line inward, because width shrinks so you must try a taller side.
Open the full page for Why, Steps, Example and Key takeaway.
Walk t with a pointer into s
Open the full page for Why, Steps, Example and Key takeaway.
Squares of a sorted array are largest at the ends.
Open the full page for Why, Steps, Example and Key takeaway.
Grow a right pointer and store last-seen indices in a map
Open the full page for Why, Steps, Example and Key takeaway.
Expand right to grow the sum, then shrink left while the sum stays at least target, tracking the shortest window.
Open the full page for Why, Steps, Example and Key takeaway.
Maintain a window of length p with character counts
Open the full page for Why, Steps, Example and Key takeaway.
Sliding window: the window is valid while window_size minus the count of the most frequent character is at most k.
Open the full page for Why, Steps, Example and Key takeaway.
This is a window where the number of zeros is at most k
Open the full page for Why, Steps, Example and Key takeaway.
Scan left to right and keep a stack of indices with decreasing temperatures
Open the full page for Why, Steps, Example and Key takeaway.
Store values on one stack and running minima on a second stack (or pairs of value and min).
Open the full page for Why, Steps, Example and Key takeaway.
Iterate asteroids and use a stack of survivors.
Open the full page for Why, Steps, Example and Key takeaway.
Use a stack of (previous_string, multiplier) pairs: on '[', push the current buffer and k, on ']', pop and repeat the inner buffer.
Open the full page for Why, Steps, Example and Key takeaway.
Keep a buffer, head index, count (or head and tail), and capacity.
Open the full page for Why, Steps, Example and Key takeaway.
Maintain a frequency map and a queue of candidate characters (or indices).
Open the full page for Why, Steps, Example and Key takeaway.
Multi-source BFS: enqueue every rotten orange at time 0, then spread to adjacent fresh oranges level by level.
Open the full page for Why, Steps, Example and Key takeaway.