How to resolve the algorithm Greatest subsequential sum step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greatest subsequential sum step by step in the Fortran programming language

Table of Contents

Problem Statement

Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one.

An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be the empty sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greatest subsequential sum step by step in the Fortran programming language

Source code in the fortran programming language

program MaxSubSeq
  implicit none

  integer, parameter :: an = 11
  integer, dimension(an) :: a = (/ -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 /)

  integer, dimension(an,an) :: mix
  integer :: i, j
  integer, dimension(2) :: m

  forall(i=1:an,j=1:an) mix(i,j) = sum(a(i:j))
  m = maxloc(mix)
  ! a(m(1):m(2)) is the wanted subsequence
  print *, a(m(1):m(2))

end program MaxSubSeq


  

You may also check:How to resolve the algorithm Reverse a string step by step in the SAS programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Ruby programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the APL programming language
You may also check:How to resolve the algorithm Program name step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the 360 Assembly programming language