Posts

Showing posts from December, 2025

Sub-arrays

Image
  1.  Find maximum sum of a subarray Brute - time: O(n^2) go through each subarray and thus find the maximum sum --------------------- Optimal - time: O(n) use Kadane Algorithm at each element either extend old subarray or start a new one. Handles negatives as well as positives ----------------------------------------------------------------------------------------------------------------------------- 2. Find maximum sum of subarray of size K only positives - use sliding window of fix size K, and move both pointers simultaneously thus simulating fix window traversing array   ----------------------------------------------------------------------------------------------------------------------------- 3. Find maximum sum of distinct subarray of size K Brute -  time: O(nK)    space: O(n) Use hashing , after each expansion and shrinking of sub array, check if this subarray contains duplicates or not. ----------------------------- Optimal - time: O(n)  ...