Smallest Subset Array
**Input** [1, 2, 5, 10, 20, 1]
**Half Sum** 19
**Result** [20]**Input** [1, 2, 5, 10, 20, 3, 7]
**Half Sum** 24
**Result** [1, 3, 20]Solutions
Steps
Last updated
**Input** [1, 2, 5, 10, 20, 1]
**Half Sum** 19
**Result** [20]**Input** [1, 2, 5, 10, 20, 3, 7]
**Half Sum** 24
**Result** [1, 3, 20]Last updated
public int[] Subset(int[] nums)
{
long sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
}
long target = sum / 2;
sum = 0;
Array.Sort(nums);
for (int i = nums.Length - 1; i >= 0; i--)
{
sum += nums[i];
if (sum > target)
{
var result = new int[nums.Length - i];
for (int j = nums.Length - 1; j >= i; j--)
{
result[j - i] = nums[j];
}
return result;
}
}
return nums;
}