How to resolve the algorithm Equilibrium index step by step in the Arturo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Equilibrium index step by step in the Arturo programming language
Table of Contents
Problem Statement
An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.
For example, in a sequence
A
{\displaystyle A}
: 3 is an equilibrium index, because: 6 is also an equilibrium index, because: (sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence
A
{\displaystyle A}
.
Write a function that, given a sequence, returns its equilibrium indices (if any). Assume that the sequence may be very long.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Equilibrium index step by step in the Arturo programming language
Source code in the arturo programming language
eqIndex: function [row][
suml: 0
delayed: 0
sumr: sum row
result: new []
loop.with:'i row 'r [
suml: suml + delayed
sumr: sumr - r
delayed: r
if suml = sumr -> 'result ++ i
]
return result
]
data: @[
@[neg 7, 1, 5, 2, neg 4, 3, 0]
@[2 4 6]
@[2 9 2]
@[1 neg 1 1 neg 1 1 neg 1 1]
]
loop data 'd ->
print [pad.right join.with:", " to [:string] d 25 "=> equilibrium index:" eqIndex d]
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the RPL programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Ring programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the PL/I programming language
You may also check:How to resolve the algorithm Element-wise operations step by step in the Standard ML programming language