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

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Greatest subsequential sum step by step in the C++ 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 C++ programming language

Purpose: The provided C++ code implements the max_subseq function, which finds a contiguous subsequence within a given sequence of integers that maximizes the sum of its elements.

Implementation Details:

1. max_subseq Function:

  • Template: The max_subseq function is a function template that takes a forward iterator begin and end as arguments. Forward iterators allow traversing a sequence in one direction (forward).
  • Variables:
    • seq_begin, seq_end: Track the start and end of the maximum subsequence.
    • seq_sum: Stores the sum of the maximum subsequence.
    • current_begin: Tracks the start of the current subsequence being considered.
    • current_sum: Stores the sum of the current subsequence.
    • zero: Represents the neutral element for adding integers, which is typically 0.
  • Loop: The function iterates through the input sequence:
    • For each element value, it checks if value is positive.
    • If value is positive:
      • If the current_sum is negative, it resets current_sum to zero and sets current_begin to the current iterator.
      • Otherwise, it increases current_sum by value.
    • If value is negative:
      • If the seq_sum is less than the current_sum:
        • Updates seq_begin, seq_end, and seq_sum to represent the current subsequence.
  • Final Check: After the loop finishes, it checks if the seq_sum is less than the current_sum. In this case, it updates seq_begin, seq_end, and seq_sum to represent the current subsequence, which is the maximum subsequence.

2. main Function:

  • Test Array: The array variable contains the sequence of integers to find the maximum subsequence.
  • Finding the Subsequence: The code calls max_subseq with the array's begin and end iterators, stored in seq.
  • Output: The code then prints the maximum subsequence by copying its elements from seq.first to seq.second to the standard output using an ostream_iterator.

Source code in the cpp programming language

#include <utility>   // for std::pair
#include <iterator>  // for std::iterator_traits
#include <iostream>  // for std::cout
#include <ostream>   // for output operator and std::endl
#include <algorithm> // for std::copy
#include <iterator>  // for std::output_iterator

// Function template max_subseq
//
// Given a sequence of integers, find a 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.
//
// Requirements:
// * ForwardIterator is a forward iterator
// * ForwardIterator's value_type is less-than comparable and addable
// * default-construction of value_type gives the neutral element
//   (zero)
// * operator+ and operator< are compatible (i.e. if a>zero and
//   b>zero, then a+b>zero, and if a<zero and b<zero, then a+b<zero)
// * [begin,end) is a valid range
//
// Returns:
//   a pair of iterators describing the begin and end of the
//   subsequence
template<typename ForwardIterator>
 std::pair<ForwardIterator, ForwardIterator>
 max_subseq(ForwardIterator begin, ForwardIterator end)
{
  typedef typename std::iterator_traits<ForwardIterator>::value_type
    value_type;

  ForwardIterator seq_begin = begin, seq_end = seq_begin;
  value_type seq_sum = value_type();
  ForwardIterator current_begin = begin;
  value_type current_sum = value_type();

  value_type zero = value_type();

  for (ForwardIterator iter = begin; iter != end; ++iter)
  {
    value_type value = *iter;
    if (zero < value)
    {
      if (current_sum < zero)
      {
        current_sum = zero;
        current_begin = iter;
      }
    }
    else
    {
      if (seq_sum < current_sum)
      {
        seq_begin = current_begin;
        seq_end = iter;
        seq_sum = current_sum;
      }
    }
    current_sum += value;
  }

  if (seq_sum < current_sum)
  {
    seq_begin = current_begin;
    seq_end = end;
    seq_sum = current_sum;
  }

  return std::make_pair(seq_begin, seq_end);
}

// the test array
int array[] = { -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 };

// function template to find the one-past-end pointer to the array
template<typename T, int N> int* end(T (&arr)[N]) { return arr+N; }

int main()
{
  // find the subsequence
  std::pair<int*, int*> seq = max_subseq(array, end(array));

  // output it
  std::copy(seq.first, seq.second, std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  return 0;
}


  

You may also check:How to resolve the algorithm Sum of squares step by step in the REXX programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Qi programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the Quackery programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Mathematica/Wolfram Language programming language