dsa2 min read
Maximum Product Subarray — Track Min and Max O(n) [Google, Meta, Amazon]
Find the maximum product of a contiguous subarray. Track both min and max at each position (negative * negative = positive). O(n) time O(1) space.
Read →
webcoderspeed.com
1276 articles
Find the maximum product of a contiguous subarray. Track both min and max at each position (negative * negative = positive). O(n) time O(1) space.
Rotate an n×n matrix 90 degrees clockwise in-place. Trick: transpose (swap i,j with j,i) then reverse each row. O(n²) time O(1) space.
Find the lexicographically next permutation in-place using a two-pass scan from the right.
Find the one duplicate in [1..n] without modifying the array using Floyd's tortoise-and-hare cycle detection.
Sort an array of 0s, 1s, and 2s in-place in one pass using Dijkstra's Dutch National Flag algorithm.