You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers.


	For example, given finalSum = 12, the following splits are valid (unique positive even integers summing up to finalSum): (12), (2 + 10), (2 + 4 + 6), and (4 + 8). Among them, (2 + 4 + 6) contains the maximum number of integers. Note that finalSum cannot be split into (2 + 2 + 4 + 4) as all the numbers should be unique.


Return a list of integers that represent a valid split containing a maximum number of integers. If no valid split exists for finalSum, return an empty list. You may return the integers in any order.

&nbsp;
Example 1:

Input: finalSum = 12
Output: [2,4,6]
Explanation: The following are valid splits: (12), (2 + 10), (2 + 4 + 6), and (4 + 8).
(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6].
Note that [2,6,4], [6,2,4], etc. are also accepted.


Example 2:

Input: finalSum = 7
Output: []
Explanation: There are no valid splits for the given finalSum.
Thus, we return an empty array.


Example 3:

Input: finalSum = 28
Output: [6,8,2,12]
Explanation: The following are valid splits: (2 + 26), (6 + 8 + 2 + 12), and (4 + 24). 
(6 + 8 + 2 + 12) has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.


&nbsp;
Constraints:


	1 &lt;= finalSum &lt;= 1010


class Solution:
    def maximumEvenSplit(self, finalSum: int) -> List[int]:
        l=[]
        if finalSum%2!=0:
            return l
        else:
            s=0
            i=2 # even pointer 2, 4, 6, 8, 10, 12...........
            while(s<finalSum):
                s+=i #sum
                l.append(i) # append the i in list
                i+=2
            if s==finalSum: #if sum s is equal to finalSum then no modidfication required
                return l
            else:
                l.pop(l.index(s-finalSum)) #Deleting the element which makes s greater than finalSum
            return l
class Solution {
    public List<Long> maximumEvenSplit(long finalSum) {
        List<Long> res = new ArrayList<Long>();
        //odd sum cannot be divided into even numbers
        if(finalSum % 2 != 0) {
            return res;
        }
        //Greedy approach, try to build the total sum using minimum unique even nos
        long currNum = 2;
        long remainingSum = finalSum;
        //as long as we can add subtract this number from remaining sum
        while(currNum <= remainingSum) {
            res.add(currNum);
            remainingSum -= currNum;//reducing remaining sum
            currNum += 2;//next even number
        }
        //now, remaining sum cannot be fulfilled by any larger even number
        //so extract the largest even number we added to the last index of res, and make it even larger by adding this current remaining sum
        //add remaining sum to the last element
        long last = res.remove(res.size()-1);
        res.add(last+remainingSum);
        return res;
    }
}
class Solution {
public:
    using ll = long long;

    ll bs(ll low , ll high, ll fs){

        ll ans = 1;
        while(low<=high){
            ll mid = low + (high-low)/2;
            if(mid*(mid+1)>fs){
                high = mid-1; // If sum till index equal to 'mid' > fs then make high = mid-1
            }
            else if(mid*(mid+1)==fs){
                return mid; // If sum till index equal to 'mid == fs, return 'mid'
            }
            else{
                ans = mid; // If sum till index equal to 'mid' < fs, update answer
                low = mid+1; // check for better answer
            }
        }
        return ans;
    }

    vector<long long> maximumEvenSplit(long long finalSum) {
        // ****some base cases / corner cases****
        if(finalSum&1) return {};
        if(finalSum==4) return {4};
        if(finalSum==8) return {2,6};

        vector<ll> ans;

        // assume that we are giving indices to even numbers
        // EVEN NUMBERS -> 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ..............
        // THEIR INDICES-> 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ..............

        // 'idx' is the index of that EVEN number uptil which the total sum of all even numbers <= finalSum
        ll idx = bs(1,finalSum/2,finalSum);

        //Consequently, 'end' is that EVEN number uptil which the total sum of all even numbers <= finalSum
        ll start = 2, end = idx*2;

        //Now, we add all the even numbers from index 1 to index 'idx-1'
        // 2 + 4 + 6 + 8 ........................... + (end-2) + end
        // 1 2 3 4 ........................... idx-1 idx
        for(int i = start; i<= (idx-1)*2; i+=2){
            ans.push_back(i);
        }

        // We do not add the last even number yet, so that we can modify it and add it later to make the (totalSumSoFar) == finalSum
        // 'totalSumSoFar' can be easily calculated by using the formula ( totalSumSoFar = idx*(idx+1) )

        // increasing the last even number 'end' by the difference of (finalSum and totalSumSoFar)
        if(idx*(idx+1)<finalSum){
            end = end + abs(finalSum - idx*(idx+1));
        }

        // adding the last even number after increasing it with the required factor
        ans.push_back(end);
        return ans;
    }
};
 var maximumEvenSplit = function(finalSum) {

    if(finalSum % 2) return [];

    const set = new Set();

    let n = 2, sum = 0;

    while(sum < finalSum) {
        sum += n;
        set.add(n);
        n += 2;
    }

    set.delete(sum - finalSum);

    return [...set];
};
Maximum Split of Positive Even Integers