Maximum Sum of continuous subarrays

Source: Internet
Author: User
Description:

Hz occasionally fooled non-computer students with some professional questions. Today, after the jobdu test group was opened, he spoke again: in the Ancient One-dimensional pattern recognition, it is often necessary to calculate the largest sum of continuous subvectors. When all vectors are positive, the problem is well solved. However, if a vector contains a negative number, should it contain a negative number and expect the positive number next to it to compensate? For example: {6,-3,-0th,-15, 1, 3rd}, the maximum sum of continuous subvectors is 8 (starting from ). Will you be fooled by him?

Input:

The input contains multiple groups of data. Each group of test data includes two rows.

The first act is an integer N (0 <= n <= 100000). When n = 0, the input ends. The next row contains N integers (we guarantee that all integers belong to [-]).

Output:

For each test case, three integers must be output in a single row, representing the largest continuous subvector and the subscript of the first element of the subvector and the subscript of the last element respectively. If multiple subvectors exist, the smallest subscript of the starting element is output.

Sample input:
3-1 -3 -25-8 3 2 0 586 -3 -2 7 -15 1 2 20
Sample output:
-1 0 010 1 48 0 3

Code:

/* Maximum number of consecutive sub-arrays and by rowandjj2014/8/10 */# include <stdio. h> # include <stdlib. h> bool isinvalid = false; int findmaxsumofsubarray (INT arr [], int Len, int * begin, int * End) {isinvalid = false; if (ARR = NULL | Len <= 0) {isinvalid = true;} int cursum = 0; int result = 0x80000000; // The minimum int I value is initialized to int; * begin = 0; * end = 0; int curbegin = 0, curend = 0; for (I = 0; I <Len; I ++) {If (cursum <0) {cursum = arr [I]; curbegin = I; curend = I;} else {cursum + = arr [I]; curend = I;} If (cursum> result) {* begin = curbegin; * end = curend; result = cursum ;}} return result;} int main () {int N; while (scanf ("% d", & N )! = EOF) {If (n = 0) {break;} int * arr = (int *) malloc (sizeof (INT) * n); If (! ARR) {exit (-1) ;}for (INT I = 0; I <n; I ++) {scanf ("% d", arr + I );} int begin, end; int result = findmaxsumofsubarray (ARR, N, & begin, & End); If (! Isinvalid) {printf ("% d \ n", result, begin, end);} Free (ARR); arr = NULL;} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.