FastPrepFastPrep
Problem Brief

Count Descending Subarrays

FULLTIMEOA

Given an integer array nums, count how many contiguous subarrays of length at least 2 are strictly descending by exactly 1 at every step.

A subarray nums[l..r] is valid if, for every index i with l <= i < r, we have nums[i + 1] = nums[i] - 1.

Function Description

Complete the function countDescendingSubarrays in the editor below.

countDescendingSubarrays has the following parameter:

  1. int[] nums: the input array

Returns

long: the number of valid descending subarrays.

1Example 1

Input
nums = [7, 6, 5, 5, 4]
Output
4
Explanation

The valid subarrays are [7, 6], [6, 5], [7, 6, 5], and [5, 4].

2Example 2

Input
nums = [4, 3, 2, 1]
Output
6
Explanation

Every subarray of length at least 2 is valid, so the answer is 3 + 2 + 1 = 6.

Constraints

Limits and guarantees your solution can rely on.

The source thread did not provide explicit numeric bounds.

  • nums.length >= 1
  • Count only contiguous subarrays whose adjacent values differ by exactly -1.
  • Use a wide enough integer type to store the number of valid subarrays.
public long countDescendingSubarrays(int[] nums) {
    // write your code here
}
Input

nums

[7, 6, 5, 5, 4]

Output

4

Sign in to submit your solution.