Returns the and of the largest subarray in an array of integers (a ring array)

Source: Internet
Author: User

First, the experimental topic

Title: Returns the and of the largest sub-array in an integer array.

Requirements:

Enter an array of shapes, with integers in the array with negative numbers.

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] End -to-end, 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 experimental ideas

Since the required sub-array is equivalent to an array of rings, the problem is solved by thinking of a form similar to the right loop of numbers. (actually the main idea is to refer to a classmate's thoughts in class)

Let me use an example to illustrate our approach to solving the problem:

For example, the array I entered is{5,-3,2,-1}The first cycle is calculated5, andMaxcompare size, and then put5+(-3) andMaxCompare the size, then the5+(-3)+2and theMaxCompare the size, and finally the5+(-3)+2+(-1) andMaxcompare size, give maximum value toMax, then put5move to-1, and then the same method is used to find the maximum value of the second cycle, but this time starts at-3. And so on, to find the largest subarray.

Since I am not programming too well with my little partner, the requirement to return the position of the largest subarray is not implemented.

Third, the source code

1 //Letter 1201-2 class Si Sungong Wan Tong2#include"stdafx.h"3#include"iostream"4 using namespacestd;5 6 /*Main function*/7 intMain ()8 {9     intLength//defines the length of an arrayTen     intN//defines the length of the changed array Onecout<<"Please enter the number of array elements:"<<Endl; ACin>>length; -n=2*length; -     int* a=New int[n];//initializing an array thecout<<"Please enter the elements in the array:"<<Endl; -      for(intI=0; i<length;i++) -     {     -Cin>>A[i]; +     } -     intmax=a[0]; +     ints=0;//define the element after summing A       for(intI=0; i<length;i++) at      { -s=0; -          for(intj=i;j<length+i;j++) -         { -s=s+A[j]; -             if(s>max) in             { -max=s; to             } +         } -A[length+i]=a[i];//each time you put the calculated number to the last the      } *cout<<Endl; $cout<<"the number of the largest sub-arrays is:"<<Endl;Panax Notoginsengcout<<max<<Endl; -  the     return 0; +}

Iv. Results of operation

Five, experience

This procedure, in the time I just saw, there is no idea, so the last to take the class of a classmate's thinking, in the process of programming we also encountered a lot of problems, such as do not know how to move the first number to the end of the array, and then after asking the students, and our own research has finally been resolved. Although I and my small partners are not very good at programming, but we still strive to complete the topic, perhaps this is a little progress, in the future, we will continue to strive to be able to complete a process independently.

Attach a photo of me and my little friend:

Returns the and of the largest subarray in an array of integers (a ring array)

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.