#237. 分组求和:强制包含特定数字
分组求和:强制包含特定数字
Group Sum (Must Include 6)
Background
Congcong is playing a number game. He has a list of integers and needs to select some of them such that their sum equals a given target value. However, there's a special rule: if the number appears in the list, all occurrences of must be selected.
Problem Description
Given an array of ints, is it possible to choose a group of some of the ints, beginning at the start index, such that the group sums to the given target? However, with the additional constraint that all 's must be chosen. (No loops needed.)
Input Format
Input is given from standard input in the following format.
startnumstarget
Where start is an integer representing the starting index; nums is an array of integers; target is an integer representing the target sum.
Output Format
Output is printed to standard output in the following format.
trueorfalse
Output true if a valid group of numbers can be chosen, otherwise output false.
Sample
0 [5 6 2] 8
true
0 [5 6 2] 9
false
0 [5 6 2] 7
false
Sample Explanation
For Sample 1: start = 0, nums = [5, 6, 2], target = 8.
Since the array contains , must be chosen. The remaining target is . From the remaining numbers , choosing achieves the target sum of . Thus, choosing and results in a sum of , so true is output.
Constraints
Time limit: second, Memory limit: KiB for each test case.