One-dimensional loopback array solves the problem of maximal subarray

Source: Internet
Author: User

I. Topics and Requirements

Title: Returns the and of the largest sub-array in an integer array.
Requirements:
Enter an array of shapes with positive and negative numbers in the array.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest.
Returns the position of the largest subarray at the same time.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n)

Second, the design idea

By solving the problem of the maximal subarray of a simple one-dimensional array, we found a method of implementing time complex O (n), using dichotomy and recursion, and then checking the program again to find that the time complexity is O (logn), this time we consider using a more efficient method to implement O (n , we re-examine the topic, in order to achieve the smallest possible time complexity, as little as possible to iterate over the array, so we take a way to go through an array only to find the largest sub-array (the implementation method see Code and its comments). As for implementing an array loopback, our approach is to resolve the loopback by defining a twice-fold array to access two times to find the array.

Third, the source code

  

1 /*======================================================================2 # Author:tianyongtao && Zhangyapeng3 # e-mail: [Email protected]4 # last modified:2015-03-28 20:485 # Filename:Demo.cpp6 # Description: Computes the largest subarray in a one-dimensional loopback array and outputs it7 ======================================================================*/8# include <iostream>9 using namespacestd;Ten# define LENGTH5 One  A intMaxsubarr (int* Arr,int& Start,int& Count)//start Save maximum subarray start point count Save maximum sub-array length - { -     intMax =0; the     intMAX1 = arr[0];//Max1 holds the maximum value in an array that is all negative -     inttemp =0; -     BOOLflag=false;  -      for(intI=0;i<2*length;i++)//determines whether the array elements are all less than 0 +     { -         if(arr[i]>0) +         { Aflag=true; at         } -         if(arr[i]>=max1) -         { -max1=Arr[i]; -Start =i; -         } in     } -     if(!flag) to     { +Count=1; -         returnMAX1;//returns the largest negative number in an array that is all negative numbers the     } *start=0; $      for(intI=0;i<2*length;i++)//the array is less than zero and the maximum sub-array is traversed oncePanax Notoginseng     { -temp+=Arr[i]; thecount++; +         if(temp>max) A         { theMax =temp; +         } -         if(temp<=0)//if the array is superimposed and less than 0 is removed from the back of the part. $         { $temp=0; -start=i+1; -Count=0; the         } -         if(count==LENGTH)Wuyi         { the             if(arr[i]<0) -             { Wucount--; -             } About             returnMax; $         } -     } -     returnMax; - } A  + intMain () the { -     intArr[length]; $     intarray[2*length];//replacing a loopback array with a twice-fold , end-to-end array the     intstart=0; the     intCount=0; thecout<<"Please enter"<<LENGTH<<"integer (can be positive negative):"; the      for(intI=0; i<length;i++) -     { inCin>>Arr[i]; the     } the     intk=0; About      for(intI=0;i<2*length;i++) the     { theArray[i] = arr[k++]; the         if(k==LENGTH) +         { -k=0; the         }Bayi     } the     intMax =Maxsubarr (array,start,count); thecout<<"the maximum number of sub-array sequences is:"; -      for(intI=0; i<count;i++) -     { thecout<<array[start+i]<<" "; the     } thecout<<Endl; thecout<<"the number of the largest sub-arrays is:"<<max<<Endl; -     return 0; the}

Iv. Results of operation

  

V. Results test

1. Array elements are ordered positive numbers

  

2. Array elements are scrambled positive

  

3. Array elements are ordered negative numbers

  

4. The array element is a disorderly sequence negative number

  

5. Array elements are positive and negative blends

  

Six, experience

This pair development makes me and the teammate Tian Yongtao gradually realizes that the pair development can avoid the logic error in the writing code to some extent, and the pair development must express own idea fully, in order to seek out a problem the best solution, can avoid detours, our algorithm realizes the time complexity O (n), Difficulty is not difficult, but initially did not carefully analyze how to achieve the minimum time complexity, just blindly want to consider a number of complex algorithms to solve this problem, in fact, seemingly complex problems often may have a clever way to solve.

  

  

One-dimensional loopback array solves the problem of maximal subarray

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.