Fewest Increment and Duplicate Operations to Reach an Array Sum of at Least k

Quick Overview

Starting from the array [1], find the fewest operations, each either increasing one element by 1 or duplicating one element, that bring the array sum to at least k. Tests reducing an open-ended sequence of operations to a small search and justifying that reduction with a counting argument.

Fewest Increment and Duplicate Operations to Reach an Array Sum of at Least k

Company: Zscaler

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a positive integer `k`. Initially, you have an array `nums = [1]`. You can perform any of the following operations on the array any number of times (possibly zero): - Choose any element in the array and increase its value by `1`. - Duplicate any element in the array and add it to the end of the array. Return the minimum number of operations required to make the sum of elements of the final array greater than or equal to `k`. ### Function Signature ```python def min_operations(k: int) -> int: ... ``` ### Rules - Each increase and each duplication counts as one operation. - A duplicate copies the element's current value at the moment of duplication. - Operations can be applied in any order and the array can grow to any length. ### Constraints - `1 <= k <= 10^5` ### Examples ```text Input: k = 11 Output: 5 ``` Increase the element to 4 (3 operations), then duplicate it twice (2 operations) to get `[4, 4, 4]` with sum 12. Increasing to 3 and duplicating three times to get `[3, 3, 3, 3]` also costs 5, and no plan with 4 operations reaches 11. ```text Input: k = 1 Output: 0 ``` The initial array `[1]` already has sum 1, so no operation is needed. ```text Input: k = 100 Output: 18 ``` Increase the element to 10 (9 operations) and duplicate it 9 times to get ten 10s, a sum of 100.

Overview: Starting from the array [1], find the fewest operations, each either increasing one element by 1 or duplicating one element, that bring the array sum to at least k. Tests reducing an open-ended sequence of operations to a small search and justifying that reduction with a counting argument.

Zscaler logo
Zscaler
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a positive integer k. Initially, you have an array nums = [1].

You can perform any of the following operations on the array any number of times (possibly zero):

  • Choose any element in the array and increase its value by 1 .
  • Duplicate any element in the array and add it to the end of the array.

Return the minimum number of operations required to make the sum of elements of the final array greater than or equal to k.

Function Signature

def min_operations(k: int) -> int:
    ...

Rules

  • Each increase and each duplication counts as one operation.
  • A duplicate copies the element's current value at the moment of duplication.
  • Operations can be applied in any order and the array can grow to any length.

Constraints

  • 1 <= k <= 10^5

Examples

Input: k = 11
Output: 5

Increase the element to 4 (3 operations), then duplicate it twice (2 operations) to get [4, 4, 4] with sum 12. Increasing to 3 and duplicating three times to get [3, 3, 3, 3] also costs 5, and no plan with 4 operations reaches 11.

Input: k = 1
Output: 0

The initial array [1] already has sum 1, so no operation is needed.

Input: k = 100
Output: 18

Increase the element to 10 (9 operations) and duplicate it 9 times to get ten 10s, a sum of 100.