Problem · Array

Partition into Equal-Sum Groups

Learn this problem
HardPalo Alto Networks logoPalo Alto NetworksFULLTIMEONSITE INTERVIEW

Problem statement

Given positive integers nums, partition every element occurrence into nonempty groups whose sums are equal. Among all valid partitions with at least two groups, maximize the number of groups.

Sort the values inside each group. Sort the groups lexicographically by their integer sequences, where a shorter equal prefix comes first. If several maximum-group partitions remain, return the lexicographically smallest sorted list of groups.

Encode each group as its comma-separated values, with no spaces. Return an empty array when no partition into at least two equal-sum groups exists.

Function

partitionEqualSumGroups(nums: int[]) → String[]

Examples

Example 1

nums = [1,2,3]return = ["1,2","3"]

The total is 6. Three groups are impossible because value 3 exceeds target 2, while two groups of sum 3 are possible.

Example 2

nums = [1,1,1,1]return = ["1","1","1","1"]

Four singleton groups all have sum 1, which is the maximum possible group count.

Example 3

nums = [1,2,3,4]return = ["1,4","2,3"]

The consistent equal-sum rule yields two groups of sum 5.

Constraints

  • 1 <= nums.length <= 10.
  • 1 <= nums[i] <= 30.
  • Repeated values are distinct occurrences and must all be used.

More Palo Alto Networks problems

drafts saved locally
public String[] partitionEqualSumGroups(int[] nums) {
    // Write your code here.
}
nums[1,2,3]
expected["1,2", "3"]
checking account