10

What is a contiguous subarray? I have tried searching online and have found pages about solving the largest contiguous subarray problem but none with a definition or explanation of what contiguous in this actually is.

Ex: Wikipedia: Maximm subarray problem has no explanation of why the given "subarray" is contiguous or what contiguous means in the context.

Raphael
  • 73,212
  • 30
  • 182
  • 400
ansonl
  • 201
  • 1
  • 2
  • 5

2 Answers2

9

This is just the ordinary dictionary definition of "contiguous": all adjacent in space. A subarray is defined by any subset of the indices of the original array; a contiguous subarray is defined by an interval of the indices: a first and last element and everything between them.

David Richerby
  • 82,470
  • 26
  • 145
  • 239
-1

EDIT: I get what people are saying. The actual definition of contiguous subarray is any sub series of elements in a given array that are contiguous ie their indices are continuous.

So given [1,2,3,4,5,6]:

[1,2,3], [3,4], [3,4,5,6] are all valid contiguous subarrays. Any algorithm can be used to generate the subarrays.

Personal note: What was confusing for me was most explanations either use or reference a specific problem or condition set to generate the subarrays. Most don't specifically state there's no direct relationship between the problem and the definition of contiguous subarrays.

For me it's the same as asking:

Q: "What does + do?"

A: "4+4=8"

Me: "OK, so I can only use + with 4, got it."

This is how my brain worked to grok the definition, so hopefully it helps someone else out.

ORIGIONAL ANSWER:

OK from what I've worked out the term "contiguous subarray" is a misnomer.

Subarray = "any part or section of an array" Contiguous = "data that is moved or stored in a solid uninterrupted block."

But AFAIK "Contiguous" still needs to be qualified by further conditions.

For example this Facebook/Meta test question:

Contiguous Subarrays

You are given an array a of N integers. For each index i, you are required to determine the number of contiguous subarrays that fulfills the following conditions:

The value at index i must be the maximum element in the contiguous subarrays, and These contiguous subarrays must either start from or end on index i.

Or this LeetCode Example:

Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.

So just asking for "contiguous subarray" without the conditions of what makes a subarrary contiguous is invalid.

Geordie
  • 99
  • 2