Design Gurus Logo
Number of Valid Subarrays (hard)

Problem Statement

Given an integer array nums, return the total count of non-empty subarrays within nums where each subarray must have its first element as the smallest among all its elements.

A subarray is a Contiguous part of an array.

Examples

  • Example 1:

    • Input: nums = [4, 3, 1]
    • Expected Output: 3
    • Justification: The valid subarrays are [4], [3], and [1]. Each subarray consists of a single element, satisfying the condition since there's no smaller element than the first.
  • Example 2:

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

    • Input: nums = [3, 3, 3]
    • Expected Output: 6
    • Justification: The valid subarrays are [3], [3], [3], [3, 3], [3, 3], and [3, 3, 3]. Since all elements are equal, all subarrays starting with any element satisfy the condition.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory