Count Distinct Arrays After Decrement Operations
Updated
Problem statement
You are given an array arr of n non-negative integers. In one operation, you choose a positive integer x and subtract 1 from every element of arr that is greater than or equal to x. Elements smaller than x do not change.
You may perform the operation zero or more times, and each operation may use a different x. Return the number of different final arrays you can obtain. Performing no operations leaves the original array, and it counts as one of them. Two final arrays are different if they differ at even one position.
Since the answer can be very large, return it modulo 10^9 + 7 (that is, 1000000007).
Function
countDistinctArrays(arr: int[]) → intExamples
Example 1
arr = [1,2]return = 4With no operation the array stays [1, 2]. Choosing x = 2 lowers only the 2, giving [1, 1]. Choosing x = 1 lowers both elements, giving [0, 1], and choosing x = 1 once more gives [0, 0]. No operation can make the difference between the two elements bigger, so [0, 2] can never appear. The 4 final arrays are [1, 2], [1, 1], [0, 1] and [0, 0], so the answer is 4.
Example 2
arr = [3,0,3]return = 4The 0 never changes, because every x is at least 1. The two 3s are always lowered together or left alone together, so they stay equal. Choosing x = 1 again and again gives [2, 0, 2], [1, 0, 1] and [0, 0, 0]. With the original array that makes 4 final arrays, so the answer is 4.
Example 3
arr = [5]return = 6Each operation with x at most the current value lowers the single element by 1, and it can never go below 0, because 0 is smaller than every allowed x. So the element can end at any value from 5 down to 0: [5], [4], [3], [2], [1] and [0]. The answer is 6.
Constraints
1 <= n <= 100000, wheren = arr.length0 <= arr[i] <= 1000000000
More Bloomberg LP Online Assessment questions
- Minimum BeautyOA · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Longest Distance in a Frequency-Limited NetworkOA · Seen Sep 2026
- Maximum Redundancy After One Segment FlipOA · Seen Sep 2026
- Sum of Pipeline Response TimesOA · Seen Sep 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Design an Ordered StreamOA · PHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Count Teams Outscoring a Target TeamOA · Seen Feb 2023
- Ashley Loves Numbers (Financial Engineer)OA · Seen Nov 2019
- Consecutive Sum (Financial Engineer Intern)OA · Seen Nov 2019
Similar questions at other companies
- Product of Subset MaximaArcesium · OAArcesium interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Prepare Taxi Driver Classification DataBoston Consulting Group · OABoston Consulting Group interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Get Triplet CountIBM · OAIBM interview questions
- Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
- Count Fancy NumbersHudson River Trading · OAHudson River Trading interview questions
- Count Numbers with the Same Set-Bit CountMicrosoft · OAMicrosoft interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)