Given a sequence of K integers {N1, N2, ..., NK}, "Continuous child column" is defined as {Ni, ni+1, ..., Nj}, where 1 <= i <= J <= K. "Maximum child columns and" are defined as the and the largest of all contiguous child column elements. For example, given sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub-columns {11,-4, 13} have the largest and 20. You are now asked to write a program that calculates the maximum sub-columns of a given integer sequence.
Input Format:
Enter line 1th to give the positive integer k (<= 100000), and the 2nd line to give the k integers, separated by a space.
output Format:
Outputs the maximum child columns in a row. If all integers in the sequence are negative, the output is 0.
Input Sample:
6-2 11-4 13-5-2
Sample output:
20
Algorithm one:
#include<iostream>using namespace Std;int MaxsubseqSum1 (int a[],int k) {int Thissum=0,maxsum=0; for(int i=0;i<k;i++) {thissum+=A[i]; if(thissum>maxsum) {Maxsum=thissum; }Else if(thissum<0) {thissum=0; } } returnMaxsum;} int main () {int k; int*A; CIN>>K; A=NewInt[k]; for(int i=0;i<k;i++) {cin>>A[i]; } cout<<maxsubseqsum1 (A,k); return0;}
Data structure Exercise 01-complexity 1. Maximum child columns and problems (20)