Array patterns are recurring problem-solving techniques used to solve a wide variety of array problems efficiently.

Instead of memorizing individual solutions, understanding these patterns helps you recognize how to approach a problem and design an optimal solution.


1. Traversal Pattern

Concept

The traversal pattern involves visiting each element of the array once or in a fixed manner.


When to Use

  • Finding maximum or minimum elements
  • Printing all elements
  • Counting frequency
  • Simple condition checks

Explanation Example

Traverse the array from the first element to the last, updating the required result as you move forward.


Time Complexity

O(n)


2. Sliding Window Pattern

Concept

The sliding window pattern processes a fixed-size or variable-size window that slides over the array, avoiding repeated computation.


When to Use

  • Subarray sum problems
  • Maximum or minimum in a window
  • Longest or shortest subarray problems

Explanation Example

Instead of recalculating the sum for every subarray, update the window by adding the next element and removing the previous one.


Time Complexity

O(n)


3. Two Pointer Pattern

Concept

The two pointer pattern uses two pointers that move through the array, either from both ends or in the same direction.


When to Use

  • Sorted arrays
  • Pair sum problems
  • Removing duplicates
  • Reversing arrays

Explanation Example

One pointer starts at the beginning and another at the end. Based on conditions, one of the pointers is moved inward.


Time Complexity

O(n)


4. Prefix Sum Pattern

Concept

The prefix sum pattern precomputes cumulative sums so that range queries can be answered efficiently.


When to Use

  • Range sum queries
  • Subarray sum problems
  • Performance optimization scenarios

Explanation Example

Store cumulative sums in an auxiliary array where each index represents the sum of elements up to that index.


Time Complexity

Preprocessing: O(n)
Query: O(1)


5. Kadane’s Algorithm (Maximum Subarray)

Concept

Kadane’s algorithm is a Dynamic Programming technique used to find the maximum sum of a contiguous subarray.


When to Use

  • Maximum sum subarray problems
  • Problems involving contiguous segments

Explanation Example

Maintain a running sum and reset it whenever it becomes worse than starting fresh from the current element.


Time Complexity

O(n)


6. Frequency Count Pattern

Concept

This pattern uses extra space, such as an array or hashmap, to count the frequency of elements.


When to Use

  • Detecting duplicates
  • Anagram checking
  • Majority element problems

Explanation Example

Traverse the array and update a frequency record for each element encountered.


Time Complexity

O(n)


7. Sorting-Based Pattern

Concept

Sorting the array first can simplify the logic of many problems.


When to Use

  • Pair or triplet problems
  • Merging intervals
  • Duplicate handling

Explanation Example

After sorting, elements that satisfy conditions often appear next to each other, reducing problem complexity.


Time Complexity

O(n log n)


8. Binary Search on Arrays

Concept

Binary search repeatedly divides the search space into half to efficiently locate elements or answers.


When to Use

  • Sorted arrays
  • Search problems
  • Optimization problems such as binary search on answer

Explanation Example

Compare the middle element with the target and eliminate half of the array in each step.


Time Complexity

O(log n)


9. Subarray Pattern

Concept

This pattern deals with problems involving continuous segments of an array.


When to Use

  • Subarray sum problems
  • Longest or shortest subarray problems
  • Zero-sum subarray problems

Key Insight

Sliding window or prefix sum techniques are often applied to solve these problems efficiently.


10. In-Place Modification Pattern

Concept

The in-place modification pattern updates the array without using extra space.


When to Use

  • Removing elements
  • Rearranging arrays
  • Memory-constrained problems

Explanation Example

Use an index to overwrite unwanted elements while traversing the array.


Time and Space Complexity

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


Summary of Array Patterns

Traversal Pattern
Use case: Basic operations

Sliding Window Pattern
Use case: Subarrays

Two Pointer Pattern
Use case: Sorted arrays

Prefix Sum Pattern
Use case: Range queries

Kadane’s Algorithm
Use case: Maximum subarray

Frequency Count Pattern
Use case: Duplicates and counting

Sorting-Based Pattern
Use case: Simplifying logic

Binary Search Pattern
Use case: Fast searching

Subarray Pattern
Use case: Contiguous segments

In-Place Modification Pattern
Use case: Space optimization


Key Learning Advice

  • Identify array constraints
  • Check whether the array is sorted
  • Look for subarray or pair conditions
  • Try to reduce nested loops
  • Think in terms of patterns, not individual problems

Conclusion

Mastering array patterns significantly improves your problem-solving ability.

Once you recognize the underlying pattern, designing an efficient solution becomes much easier and faster.