Algorithms
Contiguous Subarrays
Data Structures
Programming
Computer Science

Algorithm that generates all contiguous subarrays

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Contiguous subarrays are subsets of an array that maintain the original sequence of elements and do not skip any items between the start and the end. Generating all contiguous subarrays of an array is a common problem in computer science and can be applied to solve numerous problems including finding maximum sums, products, or analyzing patterns within data.

In this article, we will explore how to generate all contiguous subarrays efficiently, with a focus on technical explanations and examples.

Understanding the Concept

Consider an array `A` of length `n`. A contiguous subarray includes elements from the array such that elements follow one another in the defined order without any gaps.

For example, if `A = [1, 2, 3]`, the contiguous subarrays would be:

  • [1]
  • [2]
  • [3]
  • [1, 2]
  • [2, 3]
  • [1, 2, 3]

These are all contiguous because they preserve the order and inclusion of direct succession in the original array.

The Algorithm

To generate all contiguous subarrays of an array `A` of size `n`, one can use a straightforward approach that involves nested iterations:

  1. Outer Loop: Iterate over each element of the array as a starting point for subarrays.
  2. Inner Loop: For each starting point, iterate to build subarrays by including one more element at a time until reaching the end.

This algorithm effectively runs in `O(n^2)` time complexity due to its nested loops, where `n` is the size of the array.

Algorithm Implementation

Here's a Python implementation of the algorithm:

  • The outer loop sets the starting index `start` from `0` to `n-1`.
  • The inner loop sets the ending index `end` from the current `start` to `n-1`.
  • The subarray is generated using array slicing `array[start:end+1]`, ensuring contiguity.
  • The outer loop runs `n` times.
  • The inner loop runs in an average of `(n-i)` times for each iteration of the outer loop, summing up to roughly `n^2/2`.
  • In the worst case, half of the total number of subarrays of various lengths is stored.
  • Boundary Conditions: Handle empty arrays separately. The algorithm naturally results in an empty list when passed an empty input.
  • Memory Constraints: For very large values of `n`, consider approaches that process subarrays without storing them all, if feasible.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.