Tap here to switch tabs
Problem
Submissions

Problem: Minimum Elements to Add to Form a Given Sum

med
30 min
Try to solve the Minimum Elements to Add to Form a Given Sum problem.

Statement

You are given an integer array nums and two integers limit and goal. The array nums has an initial sum that you want to adjust so that the sum of all elements becomes equal to goal.

To achieve this, you can append elements to the array where each appended element xx must satisfy ∣x∣≤|x| \leq limit.

Return the minimum number of elements you need to append so that the sum of the array equals goal.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq limit ≤106\leq 10^6

  • −-limit ≤\leq nums[i] ≤\leq limit

  • −109≤-10^9 \leq goal ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Elements to Add to Form a Given Sum

med
30 min
Try to solve the Minimum Elements to Add to Form a Given Sum problem.

Statement

You are given an integer array nums and two integers limit and goal. The array nums has an initial sum that you want to adjust so that the sum of all elements becomes equal to goal.

To achieve this, you can append elements to the array where each appended element xx must satisfy ∣x∣≤|x| \leq limit.

Return the minimum number of elements you need to append so that the sum of the array equals goal.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq limit ≤106\leq 10^6

  • −-limit ≤\leq nums[i] ≤\leq limit

  • −109≤-10^9 \leq goal ≤109\leq 10^9