Reduce the largest pile to its square root k times and return the gifts left

Quick Overview

Simulate k seconds of taking gifts from the largest pile, where each step leaves the floor of that pile's square root, and return how many gifts remain in total. Tests repeated maximum extraction with a max-heap, exact integer square roots and complexity analysis.

Reduce the largest pile to its square root k times and return the gifts left

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `gifts`, where `gifts[i]` is the number of gifts in pile `i`. Every second, you do the following: - Select the pile with the maximum number of gifts. If more than one pile has the maximum number of gifts, choose any one of them. - Leave behind the floor of the square root of that pile, and take the rest of the gifts. Return the number of gifts remaining across all piles after `k` seconds. ### Function Signature ```python def pick_gifts(gifts: list[int], k: int) -> int: ``` ### Rules - The floor of the square root of `x` is the largest integer `r` with `r * r <= x`. A pile of 1 stays at 1. - When several piles tie for the maximum, the choice does not change the answer, because the tied piles hold the same count. - Return the sum of all piles after exactly `k` seconds. ### Constraints - `1 <= len(gifts) <= 10^3` - `1 <= gifts[i] <= 10^9` - `1 <= k <= 10^3` ### Examples ```text Input: gifts = [25, 64, 9, 4, 100], k = 4 Output: 29 ``` Second 1 takes the pile of 100 and leaves 10. Second 2 takes 64 and leaves 8, second 3 takes 25 and leaves 5, and second 4 takes the pile of 10 and leaves 3. The piles end as `[5, 8, 9, 4, 3]`, which sum to 29. ```text Input: gifts = [1, 1, 1, 1], k = 4 Output: 4 ``` Every pile has 1 gift and the floor of the square root of 1 is 1, so nothing changes. The total stays 4. ```text Input: gifts = [9, 9], k = 2 Output: 6 ``` The first second reduces one pile of 9 to 3, and the second second reduces the other pile of 9 to 3. The piles end as `[3, 3]`.

Overview: Simulate k seconds of taking gifts from the largest pile, where each step leaves the floor of that pile's square root, and return how many gifts remain in total. Tests repeated maximum extraction with a max-heap, exact integer square roots and complexity analysis.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array gifts, where gifts[i] is the number of gifts in pile i. Every second, you do the following:

  • Select the pile with the maximum number of gifts. If more than one pile has the maximum number of gifts, choose any one of them.
  • Leave behind the floor of the square root of that pile, and take the rest of the gifts.

Return the number of gifts remaining across all piles after k seconds.

Function Signature

def pick_gifts(gifts: list[int], k: int) -> int:

Rules

  • The floor of the square root of x is the largest integer r with r * r <= x . A pile of 1 stays at 1.
  • When several piles tie for the maximum, the choice does not change the answer, because the tied piles hold the same count.
  • Return the sum of all piles after exactly k seconds.

Constraints

  • 1 <= len(gifts) <= 10^3
  • 1 <= gifts[i] <= 10^9
  • 1 <= k <= 10^3

Examples

Input: gifts = [25, 64, 9, 4, 100], k = 4
Output: 29

Second 1 takes the pile of 100 and leaves 10. Second 2 takes 64 and leaves 8, second 3 takes 25 and leaves 5, and second 4 takes the pile of 10 and leaves 3. The piles end as [5, 8, 9, 4, 3], which sum to 29.

Input: gifts = [1, 1, 1, 1], k = 4
Output: 4

Every pile has 1 gift and the floor of the square root of 1 is 1, so nothing changes. The total stays 4.

Input: gifts = [9, 9], k = 2
Output: 6

The first second reduces one pile of 9 to 3, and the second second reduces the other pile of 9 to 3. The piles end as [3, 3].