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.
Complete the function countDescendingSubarrays in the editor below.
countDescendingSubarrays has the following parameter:
int[] nums: the input array
Returns
long: the number of valid descending subarrays.
Examples
01 · Example 1
nums = [7, 6, 5, 5, 4] return = 4
The valid subarrays are [7, 6], [6, 5], [7, 6, 5], and [5, 4].
02 · Example 2
nums = [4, 3, 2, 1] return = 6
Every subarray of length at least 2 is valid, so the answer is 3 + 2 + 1 = 6.
Constraints
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.
More IBM problems
- Count Ideal NumbersOA · Seen Jun 2026
- Count Power Products in RangeOA · Seen Apr 2026
- Minimum Operations to Make Alternating Binary StringSeen Feb 2026
- Minimum Number of Non-Empty Disjoint SegmentsSeen Feb 2026
- Count Unstable ProcessesOA · Seen Feb 2026
- Longest Balanced Binary SubarrayOA · Seen Feb 2026
- Process Execution TimeOA · Seen Nov 2025
- Service Timeout DetectionOA · Seen Nov 2025
public long countDescendingSubarrays(int[] nums) {
// write your code here
}
nums[7, 6, 5, 5, 4]
expected4
sign in to submit