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

Published on 12 May 2024 09:40 PM

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

Source code in the fortran programming language

program Equilibrium
  implicit none
  
  integer :: array(7) = (/ -7, 1, 5, 2, -4, 3, 0 /)
 
  call equil_index(array)
 
contains

subroutine equil_index(a)
  integer, intent(in) :: a(:)
  integer :: i

  do i = 1, size(a)
    if(sum(a(1:i-1)) == sum(a(i+1:size(a)))) write(*,*) i
  end do

end subroutine
end program


  

You may also check:How to resolve the algorithm Create an object at a given address step by step in the Racket programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm First-class functions step by step in the Delphi programming language
You may also check:How to resolve the algorithm Amb step by step in the TXR programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Emacs Lisp programming language