How to resolve the algorithm Equilibrium index step by step in the XPL0 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Equilibrium index step by step in the XPL0 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 XPL0 programming language
Source code in the xpl0 programming language
code Ran=1, ChOut=8, IntOut=11;
def Size = 1_000_000;
int I, S, A(Size), Hi(Size), Lo(Size);
[for I:= 0 to Size-1 do A(I):= Ran(100) - 50;
S:= 0;
for I:= 0 to Size-1 do [S:= S+A(I); Lo(I):= S];
S:= 0;
for I:= Size-1 downto 0 do [S:= S+A(I); Hi(I):= S];
for I:= 0 to Size-1 do
if Lo(I) = Hi(I) then [IntOut(0, I); ChOut(0, ^ )];
]
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Ruby programming language
You may also check:How to resolve the algorithm Bitmap/Flood fill step by step in the Ruby programming language
You may also check:How to resolve the algorithm Date format step by step in the FunL programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Stata programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Erlang programming language