1
+ // Runtime: 308 ms (Top 27.53%) | Memory: 40.6 MB (Top 11.24%)
1
2
class Solution {
2
3
public:
3
4
using ll = long long ;
4
-
5
+
5
6
ll bs (ll low , ll high, ll fs){
6
7
7
8
ll ans = 1 ;
@@ -20,42 +21,42 @@ class Solution {
20
21
}
21
22
return ans;
22
23
}
23
-
24
+
24
25
vector<long long > maximumEvenSplit (long long finalSum) {
25
26
// ****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
37
38
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
40
41
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
45
46
for (int i = start; i<= (idx-1 )*2 ; i+=2 ){
46
47
ans.push_back (i);
47
48
}
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)
53
54
if (idx*(idx+1 )<finalSum){
54
55
end = end + abs (finalSum - idx*(idx+1 ));
55
56
}
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
58
59
ans.push_back (end);
59
- return ans;
60
+ return ans;
60
61
}
61
- };
62
+ };
0 commit comments