Returns the and (ring) of the largest sub-array in an integer array

Source: Internet
Author: User

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). Ideas: Pair Development:Zhang Zixua ma si mian just saw this topic first thought is to copy the randomly generated array is placed behind the original array, and then to solve, and then in the discussion with Ma Si mian to get a new solution, first in accordance with the solution of the integer array method, the largest sub-array solution, and then put the first number after the last number, A new array is formed and the solution is continued, and this requires defining an integer group of twice-fold arrays, each finding one and putting the first number last. In the actual knockout process, the maximum number of sub-arrays found is much greater than the correct value, and not expected. Therefore, from the Internet to find ideas, found a more simple idea, the method of finding the ring into a non-annular, that is, the most small array of the and, and then use the entire array and subtract the smallest sub-array of the and, get is definitely the largest sub-array and. The code is as follows:
1#include <iostream>2#include <string>3#include <ctime>5 #defineN 56 using namespacestd;7 8 intMaxsum (int*arr,intsize)9 {Ten     inti, Sum, Max1, Max2, dp,min; OneDP = MAX1 = arr[0]; A      for(i =1; i < size; ++i)//non-annular array; ++i, I add 1 first, then I use the value -     { -         if(DP <0) theDP =Arr[i]; -         Else -DP + =Arr[i]; -         if(DP >max1) +Max1 =DP; -     } +sum = min = DP = arr[0]; A      for(i =1; i < size; ++i)//The smallest subarray of arrays and, again, all elements of the array and subtract, the result crosses arr[n-1] to arr[0] at     { -         if(dp>0) -DP =Arr[i]; -         Else -DP + =Arr[i]; -         if(DP <min) inMin =DP; -Sum + =Arr[i]; to     } +Max2 = Sum-min;//array all elements and minus minimum sub-arrays -     returnMax1>max2? MAX1:MAX2;//three mesh operator; if max1>max2, returns the value of MAX1, otherwise returns MAX2 the } * intMain () $ {Panax Notoginseng     intArr[n]; -Srand (Time (0)); thecout <<"the random array is:"<<Endl; +      for(intj =0; J < N; J + +) A     { theARR[J] = rand ()% --Ten; +cout << Arr[j] <<" "; -     } $cout <<Endl; $cout <<"the number of the largest sub-arrays is:"<< maxsum (arr, N) <<Endl; -     return 0; -}

Summary: The solution to this idea is fairly simple, but no method is found to return the maximum subarray. Through this problem, the sense of thought is crucial, there is a complete idea, the rest is to its realization.

Returns the and (ring) of the largest sub-array in an integer 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.