Arrays Mastery Guide

Welcome to the complete hub for Array. Here youโ€™ll find guides, templates, and curated problems for interviews and mastery.


๐Ÿงฎ

Sliding Window Technique

Sliding Window is a fundamental technique used for solving problems involving contiguous subarrays or substrings.
๐Ÿ”Ž

Two Pointer Technique

Two indices that move independently, reduce time complexity from O(nยฒ) to O(n)

๐Ÿ“‹ Practice Problems

โšก View All Arrays Problems โ†’

โญ 1. Array Concepts You Must Master

๐Ÿ”น Basic Operations

๐Ÿ”น Core Patterns

Arrays revolve around 10 major patterns:

1. Sliding Window

2. Two Pointers

3. Prefix Sum

4. Binary Search on Sorted Array

5. Binary Search on Answer

6. Kadaneโ€™s Algorithm

7. Sorting + Greedy

8. Intervals

9. Matrix as Array of Arrays

10. Hashmap + Array Combo

We will cover each with template + example.

โญ Pattern โ†’ Template โ†’ Example

๐Ÿ”ถ Pattern 1: Sliding Window

Sliding Window is used when we deal with contiguous subarrays or substrings.

๐Ÿ“Œ Template (Variable-size window)

int left = 0;
for (int right = 0; right < n; right++) {
    // expand window using arr[right]

    while (window_invalid) {
        // shrink window
        left++;
    }

    // track best window
}

๐Ÿ“˜ Example

  1. Longest substring without repeating characters

๐Ÿ”ถ Pattern 2: Two Pointers

Used when array is sorted, or when youโ€™re searching for pairs.

๐Ÿ“Œ Template

int left = 0, right = n - 1;

while (left < right) {
    int sum = arr[left] + arr[right];
    if (sum == target) { ... }
    else if (sum < target) left++;
    else right--;
}

๐Ÿ“˜ Example

Two Sum (sorted)

3-sum

Container With Most Water

๐Ÿ”ถ Pattern 3: Prefix Sum

Instant sum queries from index l to r.

๐Ÿ“Œ Template

vector<int> pref(n+1, 0);
for (int i = 0; i < n; i++) pref[i+1] = pref[i] + arr[i];

// sum of l..r
int sum = pref[r+1] - pref[l];

๐Ÿ“˜ Example

Subarray sum equals K

Range sum queries

๐Ÿ”ถ Pattern 4: Kadaneโ€™s Algorithm

Max subarray sum in O(n).

๐Ÿ“Œ Template

int max_ending_here = 0, best = INT_MIN;

for (int x : arr) {
    max_ending_here = max(x, max_ending_here + x);
    best = max(best, max_ending_here);
}

๐Ÿ”ถ Pattern 5: Sorting + Greedy

Used in:

Meeting rooms

Task scheduling

Minimum arrows to burst balloons

Used on sorted arrays.

๐Ÿ“Œ Standard Template

int l = 0, r = n - 1;
while (l <= r) {
    int mid = l + (r - l) / 2;
    if (arr[mid] == target) return mid;
    else if (arr[mid] < target) l = mid + 1;
    else r = mid - 1;
}

๐Ÿ”ถ Pattern 7: Binary Search on Answer

Used when the array is not sorted but the answer lies in a monotonic search space.

Examples:

Koko eating bananas

Minimum pages allocation

Aggressive cows

๐Ÿ”ถ Pattern 8: Intervals (Important!)

Many array problems are actually interval problems.

Steps:

  1. Sort by start

  2. Merge or process based on end

๐Ÿ”ถ Pattern 9: Matrix as Array of Arrays

2D array concepts:

๐Ÿ”ถ Pattern 10: Hashmap + Array Combo

Most-used pattern in arrays.

Examples:


โ† Back to DSA ๐Ÿ  Home