A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
For example, consider array A such that:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3We can split this tape in four places:
- P = 1, difference = |3 − 10| = 7
- P = 2, difference = |4 − 9| = 5
- P = 3, difference = |6 − 7| = 1
- P = 4, difference = |10 − 3| = 7
Write a function:
def solution(a)
that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3the function should return 1, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of array A is an integer within the range [−1,000..1,000].
4
user.rb:6:in `block in solution': undefined method `<' for [3, 3, 1, 2, 4, 3]:Array (NoMethodError) from user.rb:4:in `times' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:6:in `block in solution': undefined method `<' for [3, 3, 1, 2, 4, 3]:Array (NoMethodError) from user.rb:4:in `times' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:6:in `block in solution': undefined method `<' for [3, 3, 1, 2, 4, 3]:Array (NoMethodError) from user.rb:4:in `times' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:6:in `block in solution': undefined method `<' for [3, 3, 1, 2, 4, 3]:Array (NoMethodError) from user.rb:4:in `times' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:4:in `solution': undefined method `times' for 0..4:Range (NoMethodError) from user.rb:110:in `<main>'
user.rb:4:in `solution': undefined method `times' for 0..4:Range (NoMethodError) from user.rb:110:in `<main>'
user.rb:6:in `block in solution': undefined method `<' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:6:in `block in solution': undefined method `<' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
def solution(a)
# write your code in Ruby 1.9
smallest_result = a.reduce(:+)
(0..a.size - 1).each do |index|
iteration_result = ((a[0]..a[index]).reduce(:+) - (a[index]..a[a.size-1]).reduce(:+)).abs
smallest_result = iteration_result if iteration_result < smallest_result
end
smallest_result
end
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
def solution(a)
# write your code in Ruby 1.9
smallest_result = a.reduce(:+)
(0..a.size - 1).each do |index|
iteration_result = (((a[0]..a[index]).reduce(:+)} - ((a[index]..a[a.size-1]).reduce(:+))).abs
smallest_result = iteration_result if iteration_result < smallest_result
end
smallest_result
end
user.rb:5: syntax error, unexpected '}', expecting ')' ... (((a[0]..a[index]).reduce(:+)} - ((a[index]..a[a.size-1]).r... ... ^ user.rb:7: syntax error, unexpected keyword_end, expecting ')' user.rb:111: syntax error, unexpected $end, expecting ')'
user.rb:5: syntax error, unexpected '}', expecting ')' ... (((a[0]..a[index]).reduce(:+)} - ((a[index]..a[a.size-1]).r... ... ^ user.rb:7: syntax error, unexpected keyword_end, expecting ')' user.rb:111: syntax error, unexpected $end, expecting ')'
def solution(a)
# write your code in Ruby 1.9
smallest_result = a.reduce(:+)
(0..a.size - 1).each do |index|
iteration_result = ((a[0..index]).reduce(:+) - (a[index..a[a.size-1]]).reduce(:+)).abs
smallest_result = iteration_result if iteration_result < smallest_result
end
smallest_result
end
user.rb:5:in `-': nil can't be coerced into Fixnum (TypeError) from user.rb:5:in `block in solution' from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `-': nil can't be coerced into Fixnum (TypeError) from user.rb:5:in `block in solution' from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
def solution(a)
# write your code in Ruby 1.9
smallest_result = a.reduce(:+)
(0..a.size - 1).each do |index|
iteration_result = ((a[0..index]).reduce(:+) - (a[index+1..a.size-1]).reduce(:+)).abs
smallest_result = iteration_result if iteration_result < smallest_result
end
smallest_result
end
user.rb:5:in `-': nil can't be coerced into Fixnum (TypeError) from user.rb:5:in `block in solution' from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `-': nil can't be coerced into Fixnum (TypeError) from user.rb:5:in `block in solution' from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `-': nil can't be coerced into Fixnum (TypeError) from user.rb:5:in `block in solution' from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `-': nil can't be coerced into Fixnum (TypeError) from user.rb:5:in `block in solution' from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `-' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `reduce' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `reduce' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `reduce' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `reduce' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `reduce' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:5:in `block in solution': undefined method `reduce' for nil:NilClass (NoMethodError) from user.rb:4:in `each' from user.rb:4:in `solution' from user.rb:110:in `<main>'
user.rb:7:in `-': Array can't be coerced into Fixnum (TypeError) from user.rb:7:in `block in solution' from user.rb:6:in `each' from user.rb:6:in `solution' from user.rb:112:in `<main>'
user.rb:7:in `-': Array can't be coerced into Fixnum (TypeError) from user.rb:7:in `block in solution' from user.rb:6:in `each' from user.rb:6:in `solution' from user.rb:112:in `<main>'
user.rb:8:in `block in solution': undefined local variable or method `result' for main:Object (NameError) from user.rb:6:in `each' from user.rb:6:in `solution' from user.rb:112:in `<main>'
user.rb:8:in `block in solution': undefined local variable or method `result' for main:Object (NameError) from user.rb:6:in `each' from user.rb:6:in `solution' from user.rb:112:in `<main>'
The following issues have been detected: wrong answers, timeout errors.
range sequence, length = ~1,000
running time: >0.14 sec., time limit: 0.12 sec.
random medium, numbers from 0 to 100, length = ~10,000
running time: >1.02 sec., time limit: 0.16 sec.
random medium, numbers from -1,000 to 50, length = ~10,000
running time: >1.01 sec., time limit: 0.16 sec.
large sequence, numbers from -1 to 1, length = ~100,000
running time: >1.02 sec., time limit: 0.80 sec.
random large, length = ~100,000
running time: >1.02 sec., time limit: 0.88 sec.
large sequence, length = ~100,000
running time: >1.02 sec., time limit: 0.44 sec.
large test with maximal and minimal values, length = ~100,000
running time: >1.03 sec., time limit: 0.84 sec.