Grokking the Engineering Manager Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Continuous Subarrays (medium)
On this page

Problem Statement

Examples

Try it yourself

Problem Statement

You are given a 0-indexed array of integers called nums. A subarray of num is called continuous if the following condition is met:

  • consider 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 within nums.

A subarray is defined as any contiguous, non-empty sequence of elements within an array.

Examples

Example 1:

  • Input: nums = [1, 2, 2, 13, 1]
  • Expected Output: 8
  • Justification: The continuous subarrays are [1], [2], [2], [13], [1], [1, 2], [2, 2], and [1, 2, 2].

Example 2:

  • Input: nums = [3, 3, 4, 2]
  • Expected Output: 10
  • Justification: The continuous subarrays are [3], [3], [4], [2], [3, 3], [3, 4], [4, 2], [3, 3, 4], [3, 4, 2], and [3, 3, 4, 2].

Example 3:

  • Input: nums = [5, 17, 35, 26, 5]
  • Expected Output: 5
  • Justification: The continuous subarrays are [5], [17], [35], [26], and [5].

Constraints:

  • 1 <= nums.length <= 10<sup>5</sup>
  • 1 <= nums[i] <= 10<sup>9</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself