BackmediumUncategorized uncategorized medium

Minimum Elements for Target Sum Solution

Problem Statement

You are given an array of integers `nums` and an integer `target`. Your task is to find the minimum number of elements from `nums` that sum up exactly to `target`. Each element `nums[i]` can be included in the sum at most once. If no such combination of elements exists, return -1.

Example 1
Input
nums = [3, 5, 7, 10], target = 15
Output
2

Explanation: The combination `[5, 10]` sums to 15 and uses 2 elements. Other combinations like `[3, 5, 7]` also sum to 15 but use 3 elements. 2 is the minimum.

Constraints

  • 1 <= nums.length <= 100
  • -1000 <= nums[i] <= 1000
  • -10^5 <= target <= 10^5
  • Each `nums[i]` can be used at most once.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free