Skip to content

Commit 5614942

Browse files
committed
Runtime: 308 ms (Top 27.53%) | Memory: 40.6 MB (Top 11.24%)
1 parent 5706236 commit 5614942

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
// Runtime: 308 ms (Top 27.53%) | Memory: 40.6 MB (Top 11.24%)
12
class Solution {
23
public:
34
using ll = long long;
4-
5+
56
ll bs(ll low , ll high, ll fs){
67

78
ll ans = 1;
@@ -20,42 +21,42 @@ class Solution {
2021
}
2122
return ans;
2223
}
23-
24+
2425
vector<long long> maximumEvenSplit(long long finalSum) {
2526
// ****some base cases / corner cases****
26-
if(finalSum&1) return {};
27-
if(finalSum==4) return {4};
28-
if(finalSum==8) return {2,6};
29-
30-
vector<ll> ans;
31-
32-
// assume that we are giving indices to even numbers
33-
// EVEN NUMBERS -> 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ..............
34-
// THEIR INDICES-> 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ..............
35-
36-
// 'idx' is the index of that EVEN number uptil which the total sum of all even numbers <= finalSum
27+
if(finalSum&1) return {};
28+
if(finalSum==4) return {4};
29+
if(finalSum==8) return {2,6};
30+
31+
vector<ll> ans;
32+
33+
// assume that we are giving indices to even numbers
34+
// EVEN NUMBERS -> 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ..............
35+
// THEIR INDICES-> 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ..............
36+
37+
// 'idx' is the index of that EVEN number uptil which the total sum of all even numbers <= finalSum
3738
ll idx = bs(1,finalSum/2,finalSum);
38-
39-
//Consequently, 'end' is that EVEN number uptil which the total sum of all even numbers <= finalSum
39+
40+
//Consequently, 'end' is that EVEN number uptil which the total sum of all even numbers <= finalSum
4041
ll start = 2, end = idx*2;
41-
42-
//Now, we add all the even numbers from index 1 to index 'idx-1'
43-
// 2 + 4 + 6 + 8 ........................... + (end-2) + end
44-
// 1 2 3 4 ........................... idx-1 idx
42+
43+
//Now, we add all the even numbers from index 1 to index 'idx-1'
44+
// 2 + 4 + 6 + 8 ........................... + (end-2) + end
45+
// 1 2 3 4 ........................... idx-1 idx
4546
for(int i = start; i<= (idx-1)*2; i+=2){
4647
ans.push_back(i);
4748
}
48-
49-
// We do not add the last even number yet, so that we can modify it and add it later to make the (totalSumSoFar) == finalSum
50-
// 'totalSumSoFar' can be easily calculated by using the formula ( totalSumSoFar = idx*(idx+1) )
51-
52-
// increasing the last even number 'end' by the difference of (finalSum and totalSumSoFar)
49+
50+
// We do not add the last even number yet, so that we can modify it and add it later to make the (totalSumSoFar) == finalSum
51+
// 'totalSumSoFar' can be easily calculated by using the formula ( totalSumSoFar = idx*(idx+1) )
52+
53+
// increasing the last even number 'end' by the difference of (finalSum and totalSumSoFar)
5354
if(idx*(idx+1)<finalSum){
5455
end = end + abs(finalSum - idx*(idx+1));
5556
}
56-
57-
// adding the last even number after increasing it with the required factor
57+
58+
// adding the last even number after increasing it with the required factor
5859
ans.push_back(end);
59-
return ans;
60+
return ans;
6061
}
61-
};
62+
};

0 commit comments

Comments
 (0)