How to resolve the algorithm Equilibrium index step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Equilibrium index step by step in the BASIC 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 BASIC programming language

Source code in the basic programming language

arraybase 1

dim list = {-7, 1, 5, 2, -4, 3, 0}
print "equilibrium indices are : "; equilibrium(list)
end

function equilibrium (l)
	r = 0: s = 0
	e$ = ""
	for n = 1 to l[?]
		s += l[n]
	next
	for i = 1 to l[?]
		if r = s - r - l[i] then e$ += string(i-1) + " "
		r += l[i]
	next
	e$ = left(e$, length(e$)-1)
	return e$
end function

  

You may also check:How to resolve the algorithm Mind boggling card trick step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hash join step by step in the Scheme programming language
You may also check:How to resolve the algorithm Bifid cipher step by step in the Wren programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Simulate input/Mouse step by step in the GUISS programming language