Episode 7 — DSA with JavaScript / 7.3 — Arrays

7.3.b — Array Algorithms

<< 7.3 Overview


Two-pointer technique

The two-pointer technique uses two indices that move toward each other (or in the same direction) to solve problems in O(n) time.

Opposite direction:
  left →          ← right
  [1, 3, 5, 7, 9, 11]

Same direction:
  slow →
  fast →→→
  [0, 1, 0, 3, 0, 5]

Example: Two Sum (sorted array)

Given a sorted array, find two numbers that add up to a target.

function twoSum(arr, target) {
    let left = 0, right = arr.length - 1;
    while (left < right) {
        const sum = arr[left] + arr[right];
        if (sum === target) return [left, right];
        if (sum < target) left++;
        else right--;
    }
    return null;
}

// Dry-run: arr=[1,3,5,7,9], target=8
// left=0, right=4: 1+9=10 > 8 → right=3
// left=0, right=3: 1+7=8 = 8 → return [0, 3] ✓
pair<int,int> twoSum(const vector<int>& arr, int target) {
    int left = 0, right = arr.size() - 1;
    while (left < right) {
        int sum = arr[left] + arr[right];
        if (sum == target) return {left, right};
        if (sum < target) left++;
        else right--;
    }
    return {-1, -1};
}

Time: O(n) | Space: O(1)


Example: Remove duplicates in-place

function removeDuplicates(arr) {
    if (arr.length === 0) return 0;
    let slow = 0;
    for (let fast = 1; fast < arr.length; fast++) {
        if (arr[fast] !== arr[slow]) {
            slow++;
            arr[slow] = arr[fast];
        }
    }
    return slow + 1;
}

// Dry-run: [1, 1, 2, 2, 3]
// slow=0, fast=1: arr[1]=1 === arr[0]=1 → skip
// slow=0, fast=2: arr[2]=2 !== arr[0]=1 → slow=1, arr[1]=2
// slow=1, fast=3: arr[3]=2 === arr[1]=2 → skip
// slow=1, fast=4: arr[4]=3 !== arr[1]=2 → slow=2, arr[2]=3
// Return 3 → [1, 2, 3, ...]

Rotation algorithms

Rotate right by k positions

Input:  [1, 2, 3, 4, 5], k=2
Output: [4, 5, 1, 2, 3]

Method: Three reversals
Step 1: Reverse entire    → [5, 4, 3, 2, 1]
Step 2: Reverse first k   → [4, 5, 3, 2, 1]
Step 3: Reverse rest      → [4, 5, 1, 2, 3] ✓
function rotateRight(arr, k) {
    k = k % arr.length;
    if (k === 0) return arr;

    function reverse(start, end) {
        while (start < end) {
            [arr[start], arr[end]] = [arr[end], arr[start]];
            start++;
            end--;
        }
    }

    reverse(0, arr.length - 1);  // reverse all
    reverse(0, k - 1);           // reverse first k
    reverse(k, arr.length - 1);  // reverse rest
    return arr;
}
void rotateRight(vector<int>& arr, int k) {
    int n = arr.size();
    k = k % n;
    if (k == 0) return;
    reverse(arr.begin(), arr.end());
    reverse(arr.begin(), arr.begin() + k);
    reverse(arr.begin() + k, arr.end());
}

Time: O(n) | Space: O(1)


Kadane's algorithm — Maximum subarray sum

Find the contiguous subarray with the largest sum.

Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4]

         -2   1  -3   4  -1   2   1  -5   4
current: -2   1  -2   4   3   5   6   1   5
maxSoFar:-2   1   1   4   4   5   6   6   6

Answer: 6 (subarray [4, -1, 2, 1])
function maxSubarraySum(arr) {
    let currentMax = arr[0];
    let globalMax = arr[0];

    for (let i = 1; i < arr.length; i++) {
        currentMax = Math.max(arr[i], currentMax + arr[i]);
        globalMax = Math.max(globalMax, currentMax);
    }
    return globalMax;
}

console.log(maxSubarraySum([-2, 1, -3, 4, -1, 2, 1, -5, 4])); // 6
int maxSubarraySum(const vector<int>& arr) {
    int currentMax = arr[0], globalMax = arr[0];
    for (int i = 1; i < arr.size(); i++) {
        currentMax = max(arr[i], currentMax + arr[i]);
        globalMax = max(globalMax, currentMax);
    }
    return globalMax;
}

Time: O(n) | Space: O(1)

Dry-run table

IndexElementcurrentMaxglobalMax
0-2-2-2
11max(1, -2+1)=1max(-2, 1)=1
2-3max(-3, 1-3)=-2max(1, -2)=1
34max(4, -2+4)=4max(1, 4)=4
4-1max(-1, 4-1)=3max(4, 3)=4
52max(2, 3+2)=5max(4, 5)=5
61max(1, 5+1)=6max(5, 6)=6
7-5max(-5, 6-5)=1max(6, 1)=6
84max(4, 1+4)=5max(6, 5)=6

Dutch national flag (3-way partition)

Sort an array of 0s, 1s, and 2s in-place.

Input:  [2, 0, 1, 2, 0, 1, 0]

Use three pointers: low, mid, high
low=0, mid=0, high=6

mid=0: arr[0]=2 → swap(mid,high), high-- → [0, 0, 1, 2, 0, 1, 2], high=5
mid=0: arr[0]=0 → low++, mid++           → low=1, mid=1
mid=1: arr[1]=0 → low++, mid++           → low=2, mid=2
mid=2: arr[2]=1 → mid++                  → mid=3
mid=3: arr[3]=2 → swap(mid,high), high-- → [0, 0, 1, 1, 0, 2, 2], high=4
mid=3: arr[3]=1 → mid++                  → mid=4
mid=4: arr[4]=0 → swap(low,mid), low++, mid++ → [0, 0, 0, 1, 1, 2, 2]
mid=5 > high=4 → stop

Output: [0, 0, 0, 1, 1, 2, 2] ✓
function sortColors(arr) {
    let low = 0, mid = 0, high = arr.length - 1;
    while (mid <= high) {
        if (arr[mid] === 0) {
            [arr[low], arr[mid]] = [arr[mid], arr[low]];
            low++;
            mid++;
        } else if (arr[mid] === 1) {
            mid++;
        } else {
            [arr[mid], arr[high]] = [arr[high], arr[mid]];
            high--;
        }
    }
    return arr;
}
void sortColors(vector<int>& arr) {
    int low = 0, mid = 0, high = arr.size() - 1;
    while (mid <= high) {
        if (arr[mid] == 0) swap(arr[low++], arr[mid++]);
        else if (arr[mid] == 1) mid++;
        else swap(arr[mid], arr[high--]);
    }
}

Time: O(n) | Space: O(1)


Prefix sum array

A prefix sum array allows range sum queries in O(1) after O(n) preprocessing.

Array:      [2, 4, 6, 8, 10]
Prefix sum: [2, 6, 12, 20, 30]

Sum of elements from index 1 to 3:
= prefix[3] - prefix[0] = 20 - 2 = 18
Check: 4 + 6 + 8 = 18 ✓
function buildPrefixSum(arr) {
    const prefix = [arr[0]];
    for (let i = 1; i < arr.length; i++) {
        prefix.push(prefix[i - 1] + arr[i]);
    }
    return prefix;
}

function rangeSum(prefix, left, right) {
    if (left === 0) return prefix[right];
    return prefix[right] - prefix[left - 1];
}
vector<int> buildPrefixSum(const vector<int>& arr) {
    vector<int> prefix(arr.size());
    prefix[0] = arr[0];
    for (int i = 1; i < arr.size(); i++) {
        prefix[i] = prefix[i-1] + arr[i];
    }
    return prefix;
}

int rangeSum(const vector<int>& prefix, int left, int right) {
    if (left == 0) return prefix[right];
    return prefix[right] - prefix[left - 1];
}

Sliding window

Process a fixed-size window moving across the array.

Maximum sum of subarray of size k

Array: [1, 3, 2, 6, -1, 4, 1, 8, 2], k=3

Window [0..2]: sum = 1+3+2 = 6
Window [1..3]: sum = 6 - 1 + 6 = 11
Window [2..4]: sum = 11 - 3 + (-1) = 7
Window [3..5]: sum = 7 - 2 + 4 = 9
...
function maxSumSubarray(arr, k) {
    let windowSum = 0;
    for (let i = 0; i < k; i++) windowSum += arr[i];

    let maxSum = windowSum;
    for (let i = k; i < arr.length; i++) {
        windowSum += arr[i] - arr[i - k];
        maxSum = Math.max(maxSum, windowSum);
    }
    return maxSum;
}
int maxSumSubarray(const vector<int>& arr, int k) {
    int windowSum = 0;
    for (int i = 0; i < k; i++) windowSum += arr[i];
    int maxSum = windowSum;
    for (int i = k; i < arr.size(); i++) {
        windowSum += arr[i] - arr[i - k];
        maxSum = max(maxSum, windowSum);
    }
    return maxSum;
}

Time: O(n) | Space: O(1)


Scenario bank (array algorithms)

7.3b-001 — Two Sum (unsorted — use hash map) (variation 1)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-002 — Three Sum to zero (variation 2)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-003 — Container with most water (variation 3)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-004 — Trapping rain water (variation 4)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-005 — Product of array except self (variation 5)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-006 — Find all subarrays with given sum (variation 6)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-007 — Longest subarray with sum k (variation 7)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-008 — Maximum product subarray (variation 8)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-009 — Merge overlapping intervals (variation 9)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-010 — Next greater element (variation 10)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-011 — Stock buy and sell — max profit (variation 11)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-012 — Minimum in rotated sorted array (variation 12)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-013 — Find peak element (variation 13)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-014 — Subarray with equal 0s and 1s (variation 14)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-015 — Longest consecutive sequence (variation 15)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-016 — Majority element (Boyer-Moore) (variation 16)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-017 — Jump game (can reach last index?) (variation 17)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-018 — Minimum number of jumps (variation 18)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-019 — Set matrix zeroes (variation 19)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-020 — Rotate image (matrix) 90 degrees (variation 20)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-021 — Two Sum (unsorted — use hash map) (variation 21)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-022 — Three Sum to zero (variation 22)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-023 — Container with most water (variation 23)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-024 — Trapping rain water (variation 24)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-025 — Product of array except self (variation 25)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-026 — Find all subarrays with given sum (variation 26)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-027 — Longest subarray with sum k (variation 27)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-028 — Maximum product subarray (variation 28)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-029 — Merge overlapping intervals (variation 29)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-030 — Next greater element (variation 30)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-031 — Stock buy and sell — max profit (variation 31)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-032 — Minimum in rotated sorted array (variation 32)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-033 — Find peak element (variation 33)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-034 — Subarray with equal 0s and 1s (variation 34)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-035 — Longest consecutive sequence (variation 35)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-036 — Majority element (Boyer-Moore) (variation 36)

  • Level: Beginner
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-037 — Jump game (can reach last index?) (variation 37)

  • Level: Beginner
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-038 — Minimum number of jumps (variation 38)

  • Level: Beginner
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-039 — Set matrix zeroes (variation 39)

  • Level: Beginner
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-040 — Rotate image (matrix) 90 degrees (variation 40)

  • Level: Beginner
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-041 — Two Sum (unsorted — use hash map) (variation 41)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-042 — Three Sum to zero (variation 42)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-043 — Container with most water (variation 43)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-044 — Trapping rain water (variation 44)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-045 — Product of array except self (variation 45)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-046 — Find all subarrays with given sum (variation 46)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-047 — Longest subarray with sum k (variation 47)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-048 — Maximum product subarray (variation 48)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-049 — Merge overlapping intervals (variation 49)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-050 — Next greater element (variation 50)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-051 — Stock buy and sell — max profit (variation 51)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-052 — Minimum in rotated sorted array (variation 52)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-053 — Find peak element (variation 53)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-054 — Subarray with equal 0s and 1s (variation 54)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-055 — Longest consecutive sequence (variation 55)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-056 — Majority element (Boyer-Moore) (variation 56)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-057 — Jump game (can reach last index?) (variation 57)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-058 — Minimum number of jumps (variation 58)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-059 — Set matrix zeroes (variation 59)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-060 — Rotate image (matrix) 90 degrees (variation 60)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-061 — Two Sum (unsorted — use hash map) (variation 61)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-062 — Three Sum to zero (variation 62)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-063 — Container with most water (variation 63)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-064 — Trapping rain water (variation 64)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-065 — Product of array except self (variation 65)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-066 — Find all subarrays with given sum (variation 66)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-067 — Longest subarray with sum k (variation 67)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-068 — Maximum product subarray (variation 68)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-069 — Merge overlapping intervals (variation 69)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-070 — Next greater element (variation 70)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-071 — Stock buy and sell — max profit (variation 71)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-072 — Minimum in rotated sorted array (variation 72)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-073 — Find peak element (variation 73)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-074 — Subarray with equal 0s and 1s (variation 74)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-075 — Longest consecutive sequence (variation 75)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-076 — Majority element (Boyer-Moore) (variation 76)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-077 — Jump game (can reach last index?) (variation 77)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-078 — Minimum number of jumps (variation 78)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-079 — Set matrix zeroes (variation 79)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-080 — Rotate image (matrix) 90 degrees (variation 80)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-081 — Two Sum (unsorted — use hash map) (variation 81)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-082 — Three Sum to zero (variation 82)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-083 — Container with most water (variation 83)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-084 — Trapping rain water (variation 84)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-085 — Product of array except self (variation 85)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-086 — Find all subarrays with given sum (variation 86)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-087 — Longest subarray with sum k (variation 87)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-088 — Maximum product subarray (variation 88)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-089 — Merge overlapping intervals (variation 89)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-090 — Next greater element (variation 90)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-091 — Stock buy and sell — max profit (variation 91)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-092 — Minimum in rotated sorted array (variation 92)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-093 — Find peak element (variation 93)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-094 — Subarray with equal 0s and 1s (variation 94)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-095 — Longest consecutive sequence (variation 95)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-096 — Majority element (Boyer-Moore) (variation 96)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-097 — Jump game (can reach last index?) (variation 97)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-098 — Minimum number of jumps (variation 98)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-099 — Set matrix zeroes (variation 99)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-100 — Rotate image (matrix) 90 degrees (variation 100)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-101 — Two Sum (unsorted — use hash map) (variation 101)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-102 — Three Sum to zero (variation 102)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-103 — Container with most water (variation 103)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-104 — Trapping rain water (variation 104)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-105 — Product of array except self (variation 105)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-106 — Find all subarrays with given sum (variation 106)

  • Level: Intermediate
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-107 — Longest subarray with sum k (variation 107)

  • Level: Intermediate
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-108 — Maximum product subarray (variation 108)

  • Level: Intermediate
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-109 — Merge overlapping intervals (variation 109)

  • Level: Intermediate
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-110 — Next greater element (variation 110)

  • Level: Intermediate
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-111 — Stock buy and sell — max profit (variation 111)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-112 — Minimum in rotated sorted array (variation 112)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-113 — Find peak element (variation 113)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-114 — Subarray with equal 0s and 1s (variation 114)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-115 — Longest consecutive sequence (variation 115)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-116 — Majority element (Boyer-Moore) (variation 116)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-117 — Jump game (can reach last index?) (variation 117)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-118 — Minimum number of jumps (variation 118)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-119 — Set matrix zeroes (variation 119)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-120 — Rotate image (matrix) 90 degrees (variation 120)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-121 — Two Sum (unsorted — use hash map) (variation 121)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-122 — Three Sum to zero (variation 122)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-123 — Container with most water (variation 123)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-124 — Trapping rain water (variation 124)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-125 — Product of array except self (variation 125)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-126 — Find all subarrays with given sum (variation 126)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-127 — Longest subarray with sum k (variation 127)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-128 — Maximum product subarray (variation 128)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-129 — Merge overlapping intervals (variation 129)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-130 — Next greater element (variation 130)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-131 — Stock buy and sell — max profit (variation 131)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-132 — Minimum in rotated sorted array (variation 132)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-133 — Find peak element (variation 133)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-134 — Subarray with equal 0s and 1s (variation 134)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-135 — Longest consecutive sequence (variation 135)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-136 — Majority element (Boyer-Moore) (variation 136)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-137 — Jump game (can reach last index?) (variation 137)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-138 — Minimum number of jumps (variation 138)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-139 — Set matrix zeroes (variation 139)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-140 — Rotate image (matrix) 90 degrees (variation 140)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-141 — Two Sum (unsorted — use hash map) (variation 141)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-142 — Three Sum to zero (variation 142)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-143 — Container with most water (variation 143)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-144 — Trapping rain water (variation 144)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-145 — Product of array except self (variation 145)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-146 — Find all subarrays with given sum (variation 146)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-147 — Longest subarray with sum k (variation 147)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-148 — Maximum product subarray (variation 148)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-149 — Merge overlapping intervals (variation 149)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-150 — Next greater element (variation 150)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-151 — Stock buy and sell — max profit (variation 151)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-152 — Minimum in rotated sorted array (variation 152)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-153 — Find peak element (variation 153)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-154 — Subarray with equal 0s and 1s (variation 154)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-155 — Longest consecutive sequence (variation 155)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-156 — Majority element (Boyer-Moore) (variation 156)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-157 — Jump game (can reach last index?) (variation 157)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-158 — Minimum number of jumps (variation 158)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-159 — Set matrix zeroes (variation 159)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-160 — Rotate image (matrix) 90 degrees (variation 160)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-161 — Two Sum (unsorted — use hash map) (variation 161)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-162 — Three Sum to zero (variation 162)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-163 — Container with most water (variation 163)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-164 — Trapping rain water (variation 164)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-165 — Product of array except self (variation 165)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-166 — Find all subarrays with given sum (variation 166)

  • Level: Advanced
  • Pattern: Sliding window
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-167 — Longest subarray with sum k (variation 167)

  • Level: Advanced
  • Pattern: Prefix sum
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-168 — Maximum product subarray (variation 168)

  • Level: Advanced
  • Pattern: Hash map
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-169 — Merge overlapping intervals (variation 169)

  • Level: Advanced
  • Pattern: Kadane/DP
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.

7.3b-170 — Next greater element (variation 170)

  • Level: Advanced
  • Pattern: Two pointers
  • Time: O(n) or O(n log n) depending on approach.
  • Space: O(1) or O(n).
  • Key insight: Identify the invariant maintained across iterations.
  • Edge cases: empty array, single element, all same, sorted, reverse-sorted.
  • Code in: Both JavaScript and C++.