Skip to content

Chapter 2 of 7

Array Techniques and Problem Patterns

Several powerful techniques transform seemingly complex array problems into elegant linear-time solutions. The Two Pointers technique uses two indices moving toward each other or in the same direction, achieving O(n) time on sorted arrays. Classic applications include finding pairs summing to a target (two_sum_sorted), solving the 3Sum problem, and computing container areas with most water by moving the shorter pointer inward.

The Sliding Window technique maintains a contiguous subarray and slides it across the input, adding elements on one end while removing from the other. Fixed-size windows efficiently compute maximum sums of subarrays, while variable-size windows solve problems like minimum window substring and longest substring without repeating characters. The combination of sliding windows with monotonic queues enables solving sliding window maximum in O(n) amortized time.

Prefix Sum arrays enable O(1) range sum queries after O(n) preprocessing, with the technique extending naturally to 2D for sub-rectangle queries. Other specialized array techniques include Kadane's algorithm for maximum subarray sum in O(n), the Dutch National Flag algorithm for three-way partitioning in a single pass, and the Difference Array for efficient range updates. The trap rain water problem and product of array except self problem showcase how two-pointer techniques can replace extra space with elegant pointer manipulation.

All chapters
  1. 1Searching and Sorting Foundations
  2. 2Array Techniques and Problem Patterns
  3. 3Trees and Hierarchical Structures
  4. 4Graph Algorithms
  5. 5Dynamic Programming
  6. 6Advanced Data Structures
  7. 7String Algorithms and Specialized Techniques

Drill it

Reading is not remembering. These come from the Algorithms Code deck:

Q

What is Binary Search and what is its time complexity?<br><br>Write the implementation in Python.

Binary Search finds a target in a sorted array by repeatedly halving the search space.Time: O(log n) &nbsp;|&nbsp; Space: O(1)def binary_search(arr, target):...

Q

What is Bubble Sort and what is its time complexity?<br><br>Write the implementation in Python.

Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.Time: O(n²) &nbsp;|&nbsp; Space: O(1)def bubble_sort(arr): n = len(arr) for...

Q

What is Merge Sort and what is its time complexity?<br><br>Write the implementation in Python.

Merge Sort is a divide-and-conquer algorithm that splits the array in half, recursively sorts each half, then merges them.Time: O(n log n) &nbsp;|&nbsp; Space:...

Q

What is Quick Sort and what is its time complexity?<br><br>Write the implementation in Python.

Quick Sort picks a pivot, partitions elements around it, then recursively sorts each partition.Time: O(n log n) avg, O(n²) worst &nbsp;|&nbsp; Space: O(log n)de...