2762. Continuous Subarrays

Continuous Subarrays (LeetCode 2762) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.

How to solve Continuous Subarrays

Continuous Subarrays is a Variable Size Sliding Window problem, part of Sliding Window. Master variable-size sliding window for condition-based problems like longest substring without repeating characters.

Work towards the solution in these steps:

  1. Try using the sliding window technique.
  2. Use a set or map to keep track of the maximum and minimum of subarrays.

The reference solution runs in O(n) time and O(n) space.

The Continuous Subarrays solution and editorial walks through the full approach with reference code.

Problem statement

You are given a 0-indexed integer array nums. A subarray of nums is called continuous if:

- Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2.

Return the total number of continuous subarrays.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [5,4,2,4] Output: 8 Explanation: Continuous subarray of size 1: [5], [4], [2], [4]. Continuous subarray of size 2: [5,4], [4,2], [2,4]. Continuous subarray of size 3: [4,2,4]. There are no subarrys of size 4. Total continuous subarrays = 4 + 3 + 1 = 8. It can be shown that there are no more continuous subarrays.

Example 2:

Input: nums = [1,2,3] Output: 6 Explanation: Continuous subarray of size 1: [1], [2], [3]. Continuous subarray of size 2: [1,2], [2,3]. Continuous subarray of size 3: [1,2,3]. Total continuous subarrays = 3 + 2 + 1 = 6.

Constraints:

- 1 <= nums.length <= 10^5

- 1 <= nums[i] <= 10^9

Topics and companies

Topics: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue.

How to practise Continuous Subarrays on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.

Open Continuous Subarrays in the code editor, or read the Continuous Subarrays editorial for a worked solution with its approach and complexity analysis.

Where Continuous Subarrays sits in the DSA pattern sheet

Problems related to Continuous Subarrays

Other problems that use the same Variable Size (Condition-Based) and Sliding Window techniques:

Continuous Subarrays FAQ

What is the LeetCode number of Continuous Subarrays?

Continuous Subarrays is LeetCode problem 2762, rated Medium.

What pattern is used to solve Continuous Subarrays?

Continuous Subarrays is a Variable Size Sliding Window problem. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.

What is the time complexity of Continuous Subarrays?

The reference solution runs in O(n) time and O(n) space.

Preparing this workspace.