Sobes.tech
Junior — Senior

Optimal "correct" subarray

livecode

Task condition

An array of positive integers is given. A subset of this array is considered 'valid' if each of its elements does not exceed the sum of any two other elements in the same subset. The task is to find the maximum possible sum of elements of a 'valid' subset formed from the original numbers. Note that any subsets consisting of one or two elements automatically satisfy the condition.

Example 1: [1, 7, 3, 15, 2, 5, 2, 1, 4] → 22 (subset {7, 15}) Incorrect example: [1, 7, 15], because 1 + 7 < 15. Examples of valid subsets: [1, 2, 1] with sum 4 or [3, 5, 4, 7] with sum 19.

Example 2: [3, 3, 3, 1, 3, 7, 1] → 12 (subset {3, 3, 3, 3})