-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2178.rb
48 lines (41 loc) · 984 Bytes
/
2178.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 2178. Maximum Split of Positive Even Integers
# Input: finalSum = 12
# Output: [2,4,6]
# Input: finalSum = 7
# Output: []
def maximum_even_split(final_sum)
# return [] if odd(not even) number
return [] if final_sum % 2 != 0
# elseif
integers = []
i = 0
digit = 0
while final_sum > 0 do
# if remainder of difference is already included don't include that
i += 2
if !(integers.include? (final_sum - i))
final_sum -= i
integers << i
# p integers
end
if !integers.empty? && final_sum == i
integers << digit unless digit.zero?
digit = integers.delete_at(0)
final_sum += digit
end
end
# p "-------------------------------------"
return integers
end
# p maximum_even_split(6914017674).reduce(:+)
p maximum_even_split(28)
# n = 1
# while n < 10000000000 do
# array = maximum_even_split(n)
# unless array.empty?
# if n != array.reduce(:+)
# p false
# end
# end
# n += 1
# end