Skip to main content

Count Non Decreasing Arrays With Given Digit Sums - Solution & Explanation

HardArrayDynamic ProgrammingPrefix Sum4 min readAsked at: DocuSign
Practice this problem

Problem Statement

You are given an integer array digitSum of length n.

An array arr of length n is considered valid if:

  • 0 <= arr[i] <= 5000
  • it is non-decreasing.
  • the sum of the digits of arr[i] equals digitSum[i].

Return an integer denoting the number of distinct valid arrays. Since the answer may be large, return it modulo 109 + 7.

An array is said to be non-decreasing if each element is greater than or equal to the previous element, if it exists.

 

Example 1:

Input: digitSum = [25,1]

Output: 6

Explanation:

Numbers whose sum of digits is 25 are 799, 889, 898, 979, 988, and 997.

The only number whose sum of digits is 1 that can appear after these values while keeping the array non-decreasing is 1000.

Thus, the valid arrays are [799, 1000], [889, 1000], [898, 1000], [979, 1000], [988, 1000], and [997, 1000].

Hence, the answer is 6.

Example 2:

Input: digitSum = [1]

Output: 4

Explanation:

The valid arrays are [1], [10], [100], and [1000].

Thus, the answer is 4.

Example 3:

Input: digitSum = [2,49,23]

Output: 0

Explanation:

There is no integer in the range [0, 5000] whose sum of digits is 49. Thus, the answer is 0.

 

Constraints:

  • 1 <= digitSum.length <= 1000
  • 0 <= digitSum[i] <= 50

Approach Overview

Problem Overview: You need to count how many arrays satisfy two constraints: the array is non‑decreasing and the total digit sum equals a given target. Each element contributes to the total sum while also respecting the ordering constraint. The challenge is handling both conditions efficiently without enumerating every possible array.

Approach 1: Brute Force Enumeration (Exponential Time, O(10^n) time, O(n) space)

The most direct idea is backtracking. Build the array position by position and only allow values greater than or equal to the previous value to maintain the non‑decreasing property. Track the running digit sum and stop exploring a branch once the sum exceeds the target. While this approach correctly generates all valid arrays, the branching factor (up to 10 choices per position) leads to exponential growth. It becomes impractical even for moderate n. This method mainly helps verify correctness on small inputs.

Approach 2: Dynamic Programming by Length, Last Value, and Sum (O(n * S * 10) time, O(S * 10) space)

Dynamic programming avoids recomputation by storing intermediate states. Define dp[i][d][s] as the number of ways to build an array of length i where the last digit is d and the total sum is s. For the next element, you can only choose digits d' such that d' ≥ d to maintain non‑decreasing order. Transition by iterating possible next digits and updating the sum. This converts the exponential search into polynomial time. The approach is a classic application of dynamic programming with constrained transitions.

Approach 3: Prefix Sum Optimized DP (O(n * S * 10) time, O(S * 10) space)

The DP transition repeatedly sums ranges of previous states because any digit ≥ the previous one is allowed. Instead of iterating through all possible digits each time, maintain prefix sums over the digit dimension. This allows computing transitions in constant time per state. The optimization significantly reduces constant factors and is typically the implementation used in competitive solutions. Techniques like cumulative sums and state compression often appear in problems combining ordering constraints with totals, similar to patterns in prefix sum or counting problems.

Approach 4: Combinatorics Perspective (Problem‑specific, ~O(S * 10))

Non‑decreasing arrays can be viewed as choosing digits with repetition while maintaining sorted order. That turns the array into a multiset of digits. If the only constraint were length, this would reduce to a stars‑and‑bars counting problem. The digit sum restriction introduces an additional constraint, which can still be handled using combinatorial DP or generating functions. This interpretation connects the problem with classic combinatorics counting techniques.

Recommended for interviews: The dynamic programming solution with prefix optimization is what interviewers usually expect. Starting with brute force shows you understand the constraints. Moving to DP demonstrates the ability to recognize overlapping subproblems and reduce exponential search to polynomial time.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force BacktrackingO(10^n)O(n)Small inputs or validating correctness of optimized solutions
Dynamic Programming (Length, Last Digit, Sum)O(n * S * 10)O(S * 10)General solution that handles ordering and sum constraints efficiently
Prefix Sum Optimized DPO(n * S * 10)O(S * 10)Preferred implementation when DP transitions involve digit ranges
Combinatorics / Counting~O(S * 10)O(S)When the problem can be reframed as multiset counting with sum constraints

Video Solution

Leetcode 3883 | Count Non Decreasing Arrays With Given Digit Sums | Leetcode biweekly contest 179CodeWithMeGuys303 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Count Non Decreasing Arrays With Given Digit Sums easy or hard?
The problem is considered hard because it combines multiple constraints: non‑decreasing ordering and an exact digit sum. Solving it efficiently requires recognizing a dynamic programming pattern and optimizing transitions with prefix sums or combinatorial reasoning.
Count Non Decreasing Arrays With Given Digit Sums Python/Java solution
Most implementations use a 2D or 3D DP table with transitions that ensure the next digit is greater than or equal to the previous one. The same logic works in Python, Java, and C++ by iterating over length, digit, and sum states while applying prefix sum optimization.
How to solve Count Non Decreasing Arrays With Given Digit Sums in O(n)?
A pure O(n) solution generally does not exist because the digit sum dimension must be tracked. Most efficient implementations use dynamic programming with complexity O(n * S * 10). Prefix sums reduce transition costs but the sum dimension remains necessary.
What is the best approach for Count Non Decreasing Arrays With Given Digit Sums?
Dynamic programming with prefix sum optimization is the most practical approach. The state tracks array length, last chosen digit, and current digit sum. Prefix sums allow efficient transitions between digit ranges, keeping the complexity around O(n * S * 10).
Is Count Non Decreasing Arrays With Given Digit Sums asked at Google/Amazon/Meta?
Problems combining counting, ordering constraints, and digit sums frequently appear in interviews at companies like Google and Amazon. Variants often test dynamic programming, combinatorics, and state compression techniques.
What data structure is used in Count Non Decreasing Arrays With Given Digit Sums?
The core structure is a multidimensional DP array storing counts for each combination of length, last digit, and accumulated sum. Prefix sum arrays are often used to speed up transitions across digit ranges.
What is the time complexity of Count Non Decreasing Arrays With Given Digit Sums?
The optimized dynamic programming solution runs in O(n * S * 10) time, where n is the array length and S is the required digit sum. Space complexity is typically O(S * 10) after compressing the DP states to only keep the previous layer.

Ready to solve this problem?

Practice Count Non Decreasing Arrays With Given Digit Sums with our built-in code editor and test cases.

Practice on FleetCode