Find a Subset of Exactly K Elements with a Target Sum
Updated
Problem statement
Given an integer array nums, return whether you can choose exactly k distinct array positions whose values sum to target. The chosen positions need not be adjacent. Each position may be used at most once; equal values at different positions remain separate choices.
For this exercise, assume signed values are allowed, the empty subset has sum 0, and the interface and bounds below apply.
Note that this asks for a subset of length k, not a contiguous subarray.
Function
hasKSubsetSum(nums: int[], k: int, target: int) → booleanExamples
Example 1
nums = [1,50,2,50,3]k = 3target = 6return = trueChoose the distinct positions holding 1, 2 and 3. The elements need not be contiguous.
Example 2
nums = [1,2,3]k = 2target = 6return = falseNo pair sums to 6. All three values sum to 6, but exactly two positions are required.
Constraints
- For this exercise, assume
0 <= nums.length <= 20. -100 <= nums[i] <= 100.0 <= k <= nums.lengthand-2000 <= target <= 2000.
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Two SumONSITE INTERVIEW · Seen Nov 2025
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
Similar questions at other companies
- Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions
- Beautiful ArrangementMathWorks · ONSITE INTERVIEWMathWorks interview questions
- First Valid Word SegmentationAmazon · ONSITE INTERVIEW
- Generate One-Two Strings with Sum NGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Distinct Palindrome PartitionsMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Unique Unordered Dice OutcomesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count and Score Distinct Hat AssignmentsRoblox · ONSITE INTERVIEWRoblox interview questions
- Partition a Card Deck into FifteensMeta · ONSITE INTERVIEWMeta interview questions
- Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)