Quick Overview

Given an integer array, count the contiguous subsegments in which every pair of adjacent elements differs by at most 1. It tests turning a pairwise condition into a linear-time counting argument and keeping counts correct when they exceed 32-bit range.

Count Stable Subsegments Where Adjacent Elements Differ by at Most 1

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, a subsegment is a contiguous, non-empty slice of `nums`. A subsegment is called stable if, for every pair of adjacent elements `nums[i]` and `nums[i+1]` inside it, `|nums[i] - nums[i+1]| <= 1`. Return the total number of stable subsegments of `nums`. A subsegment of length 1 is always stable, since it has no adjacent pairs. ### Function Signature ```python def count_stable_subsegments(nums: list[int]) -> int: ``` ### Rules - Subsegments are counted by position. Two slices with different start or end indices count separately, even when they hold the same values. - Return the count as a single integer. ### Constraints - `1 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1,2,3,7,8] Output: 9 ``` The stable subsegments are the 5 single elements, the pairs [1,2], [2,3] and [7,8], and [1,2,3]. The jump from 3 to 7 makes every slice that crosses it unstable. ```text Input: nums = [5,5,5,5] Output: 10 ``` All adjacent differences are 0, so every one of the 4 + 3 + 2 + 1 = 10 slices of the array is stable. ```text Input: nums = [-1,0,-2,-1,5] Output: 7 ``` Only the pairs [-1,0] and [-2,-1] are stable among the longer slices, because 0 to -2 and -1 to 5 both differ by more than 1. That gives 5 single elements plus 2 pairs.

Overview: Given an integer array, count the contiguous subsegments in which every pair of adjacent elements differs by at most 1. It tests turning a pairwise condition into a linear-time counting argument and keeping counts correct when they exceed 32-bit range.

You are given an integer array `nums`. A subsegment is a contiguous, non-empty slice of `nums`. A subsegment is called stable if, for every pair of adjacent elements `nums[i]` and `nums[i+1]` inside it, `|nums[i] - nums[i+1]| <= 1`. A subsegment of length 1 is always stable, since it has no adjacent pairs. Return the total number of stable subsegments of `nums`. Subsegments are counted by position: two slices with a different start index or a different end index count separately, even when they hold the same values. Return the count as a single integer. The count can exceed 2^31-1. When `nums` has 10^5 elements and every slice is stable, it reaches 10^5 * (10^5 + 1) / 2 = 5,000,050,000. Java returns `long` and C++ returns `long long`. ### Example 1 ```text Input: nums = [1, 2, 3, 7, 8] Output: 9 ``` The stable subsegments are the 5 single elements, the pairs [1, 2], [2, 3] and [7, 8], and [1, 2, 3]. The jump from 3 to 7 makes every slice that crosses it unstable. ### Example 2 ```text Input: nums = [-1, 0, -2, -1, 5] Output: 7 ``` Among the longer slices, only the pairs [-1, 0] and [-2, -1] are stable, because 0 to -2 and -1 to 5 both differ by more than 1. That gives 5 single elements plus 2 pairs. ### Constraints - `1 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` - The answer is at most 5,000,050,000, which does not fit in a 32-bit signed integer.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The answer is at most 5,000,050,000, which exceeds 2^31-1, so Java returns long and C++ returns long long.

Examples

Input: ([1, 2, 3, 7, 8],)

Expected Output: 9

Explanation: Source example 1: runs of length 3 and 2 give 6 + 3 = 9.

Input: ([5, 5, 5, 5],)

Expected Output: 10

Explanation: Source example 2: all four values are equal, so all 10 slices are stable.

Hints

  1. One adjacent pair that differs by more than 1 makes every slice containing both of its elements unstable, so such pairs split the array into separate pieces.
  2. Work out how many stable slices a fully stable block of length k contains, and check it against an all-equal array such as [5, 5, 5, 5], which has 10.
  3. For a long array the count can exceed 2^31-1, so pick an integer type for it with that in mind.

Count Stable Subsegments of an Array

easyCitadelSoftware Engineer

You are given an integer array nums. A subsegment is a contiguous, non-empty slice of nums. A subsegment is called stable if, for every pair of adjacent elements nums[i] and nums[i+1] inside it, |nums[i] - nums[i+1]| <= 1. A subsegment of length 1 is always stable, since it has no adjacent pairs.

Return the total number of stable subsegments of nums.

Subsegments are counted by position: two slices with a different start index or a different end index count separately, even when they hold the same values. Return the count as a single integer.

The count can exceed 2^31-1. When nums has 10^5 elements and every slice is stable, it reaches 10^5 * (10^5 + 1) / 2 = 5,000,050,000. Java returns long and C++ returns long long.

Example 1

Input: nums = [1, 2, 3, 7, 8]
Output: 9

The stable subsegments are the 5 single elements, the pairs [1, 2], [2, 3] and [7, 8], and [1, 2, 3]. The jump from 3 to 7 makes every slice that crosses it unstable.

Example 2

Input: nums = [-1, 0, -2, -1, 5]
Output: 7

Among the longer slices, only the pairs [-1, 0] and [-2, -1] are stable, because 0 to -2 and -1 to 5 both differ by more than 1. That gives 5 single elements plus 2 pairs.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The answer is at most 5,000,050,000, which does not fit in a 32-bit signed integer.

Examples

Example 1
Input
([1, 2, 3, 7, 8],)
Output
9
Notes
Source example 1: runs of length 3 and 2 give 6 + 3 = 9.
Example 2
Input
([5, 5, 5, 5],)
Output
10
Notes
Source example 2: all four values are equal, so all 10 slices are stable.

Constraints

  • •1 <= nums.length <= 10^5
  • •-10^9 <= nums[i] <= 10^9
  • •The answer is at most 5,000,050,000, which exceeds 2^31-1, so Java returns long and C++ returns long long.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def count_stable_subsegments(nums: list[int]) -> int:
Sample call
count_stable_subsegments([1, 2, 3, 7, 8])
Language
Case 1
Input
([1, 2, 3, 7, 8],)
Expected
9
Case 2
Input
([5, 5, 5, 5],)
Expected
10

Browse More Questions

Show the approach

Approach

Group the stable subsegments by their end index. For each index i, let run(i) be the length of the longest stable subsegment that ends at i. If nums[j..i] is stable, then every shorter slice nums[j'..i] with j <= j' <= i is stable too, because it contains a subset of the same adjacent pairs. So the stable subsegments ending at i are exactly the run(i) slices that start at i - run(i) + 1 through i.

The run length follows a simple rule: run(0) = 1; for i > 0, run(i) = run(i-1) + 1 when |nums[i] - nums[i-1]| <= 1, and run(i) = 1 otherwise, since one pair at (i-1, i) with a larger gap rules out every slice of length 2 or more that ends at i. Summing run(i) over all i counts every stable subsegment exactly once, by its end index.

The code makes one pass, keeping the current run length and a running total. Invariant: after processing index i, run equals run(i) and total equals the number of stable subsegments that end at or before i.

Edge cases: a single element gives 1. An array where no adjacent pair is stable gives n. An array where every pair is stable gives n(n+1)/2, which reaches 5,000,050,000 at n = 10^5, so the total is a 64-bit value (long in Java, long long in C++). JavaScript numbers represent it exactly because it is below 2^53. Adjacent differences reach 2 * 10^9, and the Java and C++ references compute them in 64-bit arithmetic.

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