How to resolve the algorithm Equilibrium index step by step in the AutoHotkey programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Equilibrium index step by step in the AutoHotkey 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 AutoHotkey programming language
Source code in the autohotkey programming language
Equilibrium_index(list, BaseIndex=0){
StringSplit, A, list, `,
Loop % A0 {
i := A_Index , Pre := Post := 0
loop, % A0
if (A_Index < i)
Pre += A%A_Index%
else if (A_Index > i)
Post += A%A_Index%
if (Pre = Post)
Res .= (Res?", ":"") i - (BaseIndex?0:1)
}
return Res
}
list = -7, 1, 5, 2, -4, 3, 0
MsgBox % Equilibrium_index(list)
You may also check:How to resolve the algorithm Binary digits step by step in the Phix programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Q programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the BASIC programming language
You may also check:How to resolve the algorithm Exceptions step by step in the Ada programming language