Split Array into Two Subsequences with Equal Average

viaGlassdoor

Problem: Given an array of numbers, split it into two non-empty subsequences such that the average of the two subsequences is equal. Return the two subsequences.

Constraints: Every element must be used in exactly one of the two subsequences; both subsequences must be non-empty.

Example: [1,2,3,4,5,6] -> total sum=21, total count=6, overall average=3.5; find a subset whose average also equals 3.5, e.g., {1,6} (avg 3.5) and the rest {2,3,4,5} (avg 3.5).

Approach: Key insight - a subset has the same average as the whole array if and only if sum(subset) * n == sum(total) * k, where k is the subset size and n is total count. Enumerate subset sizes k from 1 to n-1, and for each k use subset-sum DP (or meet-in-the-middle for larger n) to check whether a subset of size k with sum = total*k/n exists; return the first such subset found along with its complement.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account