5
ll bs(ll low, ll high, ll fs) {8
ll mid = low + (high - low) / 2;9
if (mid * (mid + 1) > fs) {10
high = mid - 1; // If sum till index equal to 'mid' > fs then make high = mid-111
} else if (mid * (mid + 1) == fs) {12
return mid; // If sum till index equal to 'mid == fs, return 'mid'14
ans = mid; // If sum till index equal to 'mid' < fs, update answer15
low = mid + 1; // check for better answer21
vector<long long> maximumEvenSplit(long long finalSum) {22
// ****some base cases / corner cases****23
if (finalSum & 1) return {};24
if (finalSum == 4) return {4};25
if (finalSum == 8) return {2, 6};29
// assume that we are giving indices to even numbers30
// EVEN NUMBERS -> 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ..............31
// THEIR INDICES-> 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ..............33
// 'idx' is the index of that EVEN number uptil which the total sum of all34
// even numbers <= finalSum35
ll idx = bs(1, finalSum / 2, finalSum);37
// Consequently, 'end' is that EVEN number uptil which the total sum of all38
// even numbers <= finalSum39
ll start = 2, end = idx * 2;41
// Now, we add all the even numbers from index 1 to index 'idx-1'42
// 2 + 4 + 6 + 8 ........................... + (end-2) + end43
// 1 2 3 4 ........................... idx-1 idx44
for (int i = start; i <= (idx - 1) * 2; i += 2) {48
// We do not add the last even number yet, so that we can modify it and add49
// it later to make the (totalSumSoFar) == finalSum 'totalSumSoFar' can be50
// easily calculated by using the formula ( totalSumSoFar = idx*(idx+1) )52
// increasing the last even number 'end' by the difference of (finalSum and54
if (idx * (idx + 1) < finalSum) {55
end = end + abs(finalSum - idx * (idx + 1));58
// adding the last even number after increasing it with the required factor