Partition into Equal-Sum Groups
Updated
Problem statement
You are given a list of positive integers nums. Split all of them into groups so that every group has the same sum. Every value must go into exactly one group, and if a value appears twice, the two copies are separate values. You need at least two groups. Among all such splits, use as many groups as possible.
Write each split in a standard form: sort the values inside each group from small to large, then sort the groups by comparing them value by value from the start. If one group is the start of another, like [1,2] and [1,2,5], the shorter one comes first. If several splits have the most groups, return the one whose standard form comes first, comparing group by group in the same way.
Return each group as its values joined by commas with no spaces, such as "1,4". If no split into at least two groups of equal sum exists, return an empty list.
Function
partitionEqualSumGroups(nums: int[]) → String[]Examples
Example 1
nums = [1,2,3]return = ["1,2","3"]The total is 6. Three groups would each need sum 2, but the value 3 is bigger than 2, so that is impossible. Two groups of sum 3 work: [1,2] and [3].
Example 2
nums = [1,1,1,1]return = ["1","1","1","1"]Each 1 can be its own group, which gives four groups of sum 1. That is the most groups possible with four values.
Example 3
nums = [1,2,3,4]return = ["1,4","2,3"]The total is 10. Four groups would need sum 2.5, and five or more groups need more values than there are. Three groups do not divide 10 evenly. So two groups of sum 5: [1,4] and [2,3] is the only way.
Constraints
1 ≤ nums.length ≤ 101 ≤ nums[i] ≤ 30
More Palo Alto Networks Onsite Interview questions
Similar questions at other companies
- Generate Circular Student ArrangementsAmazon · ONSITE INTERVIEW
- Generate All Permutations of a NumberFleetx · ONSITE INTERVIEW
- Bounded Combination Sum with Signed CandidatesConfluent · ONSITE INTERVIEWConfluent interview questions
- Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions
- Reduce Array Size to At Most HalfMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Enumerate Character Paths to a MarkerTesla · ONSITE INTERVIEWTesla interview questions
- Subsets IIBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Inventory Outfit CombinationsRoblox · ONSITE INTERVIEWRoblox interview questions
- Enumerate Compatible Meeting SubsetsExpedia · ONSITE INTERVIEWExpedia interview questions
- Sort by Checking PermutationsMercor · PHONE SCREENMercor interview questions