Minimum Cost to Supply an Ordered Processing Run

Quick Overview

Minimize the cost of fixed-size resource refills while processing array elements in order, carrying unused resources and returning the total modulo one billion and seven.

Minimum Cost to Supply an Ordered Processing Run

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Minimum Cost to Supply an Ordered Processing Run You are given an integer array `nums` and a positive integer `k`. You start with `k` resource units and must process the elements of `nums` from left to right. Processing an element requires its value in available resources and consumes that many units. If the resources available before the next element are insufficient, you may perform a refill operation that adds exactly `k` units. Repeat refills if necessary until that element can be processed. The first refill in the entire run costs `1`, the second costs `2`, and so on. The refill amount `k` stays fixed, and unused resources carry forward to the next element. Return the minimum total refill cost, modulo `1000000007`. ### Function `int minimumCost(vector<int>& nums, int k)` ### Example 1 **Input:** `nums = [6, 2, 11], k = 5` **Output:** `6` **Explanation:** Start with `5` resources. Before processing `6`, refill once for cost `1`, then consume `6`, leaving `4`. Processing `2` leaves `2`. Before processing `11`, refill twice for costs `2` and `3`, giving `12` resources, then consume `11`. The total cost is `1 + 2 + 3 = 6`. ### Example 2 **Input:** `nums = [1000000000], k = 1` **Output:** `28` **Explanation:** The initial resource unit is free, so `999999999` refills are required. Their costs sum to `499999999500000000`, which is `28` modulo `1000000007`. ### Constraints - `1 <= nums.length <= 100000` - `1 <= nums[i] <= 1000000000` - `1 <= k <= 1000000000` ### Clarifications - This is one complete processing run inside a single function call. State starts with `k` resources and no refills already performed. - Process elements in their given order; each consumes resources only after enough are available. - Equality is sufficient: no refill is needed when available resources exactly equal the next requirement. - The refill cost counter does not reset between elements, and the initial `k` resources do not count as a paid refill. ### What a Strong Answer Covers - Carrying leftover resources and refill numbering across the whole ordered run. - Handling an element that needs many refills without iterating through each refill. - Minimizing the actual cost before returning its modular value and avoiding overflow in large totals.

Overview: Minimize the cost of fixed-size resource refills while processing array elements in order, carrying unused resources and returning the total modulo one billion and seven.

Infosys logo
Infosys
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Minimum Cost to Supply an Ordered Processing Run

You are given an integer array nums and a positive integer k. You start with k resource units and must process the elements of nums from left to right.

Processing an element requires its value in available resources and consumes that many units. If the resources available before the next element are insufficient, you may perform a refill operation that adds exactly k units. Repeat refills if necessary until that element can be processed.

The first refill in the entire run costs 1, the second costs 2, and so on. The refill amount k stays fixed, and unused resources carry forward to the next element.

Return the minimum total refill cost, modulo 1000000007.

Function

int minimumCost(vector<int>& nums, int k)

Example 1

Input: nums = [6, 2, 11], k = 5

Output: 6

Explanation: Start with 5 resources. Before processing 6, refill once for cost 1, then consume 6, leaving 4. Processing 2 leaves 2. Before processing 11, refill twice for costs 2 and 3, giving 12 resources, then consume 11. The total cost is 1 + 2 + 3 = 6.

Example 2

Input: nums = [1000000000], k = 1

Output: 28

Explanation: The initial resource unit is free, so 999999999 refills are required. Their costs sum to 499999999500000000, which is 28 modulo 1000000007.

Constraints

  • 1 <= nums.length <= 100000
  • 1 <= nums[i] <= 1000000000
  • 1 <= k <= 1000000000

Clarifications

  • This is one complete processing run inside a single function call. State starts with k resources and no refills already performed.
  • Process elements in their given order; each consumes resources only after enough are available.
  • Equality is sufficient: no refill is needed when available resources exactly equal the next requirement.
  • The refill cost counter does not reset between elements, and the initial k resources do not count as a paid refill.

What a Strong Answer Covers Guidance

  • Carrying leftover resources and refill numbering across the whole ordered run.
  • Handling an element that needs many refills without iterating through each refill.
  • Minimizing the actual cost before returning its modular value and avoiding overflow in large totals.