One-dimensional arrays for maximum sub-arrays (solving overflow problems)

Source: Internet
Author: User
Tags array definition pow

First, the topic requirements

Title: Returns the and of the largest sub-array in an integer array.
Requirements:
Requires that the procedure be capable of handling 1000 elements;
Each element is of the int32 type;
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.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

Second, the design idea

The program in the macro defines the size of the array length, in the length of more than 1 million when the program crashes directly, in fact, this is not because the result is too large caused by the numerical overflow, but because of memory overflow, our array is defined within the program, local variables, storage location on the stack, the default size of the stack is about 1MB, When our array is defined as 1 million, the size is approximately 1000000x4x8bit, and the size is 3.81MB, so a memory overflow is generated at this time. This time we have defined the array outside of the function so that the length of the array can be defined very large.

With regard to the problem that the value may overflow during the accumulation process, we are beginning with the idea of adding an array of type char instead of the int type so long as the Char array definition is large enough that there will never be an overflow. But I and Tian Yongtao in the process of writing code, found that the implementation of simple large number addition can, but the combination of the topic also requires the largest subarray in the array, so the program becomes very complex, the process of passing parameters will encounter a variety of problems. We write the code that adds up to the large number, implements the code that solves the maximum subarray of time complexity O (n), but it is too difficult to combine the two together, perhaps because we are lacking in the ability to write code.

We have had the following two kinds of ideas:

1. Replace all the additions in the code with the addition of the array, but later find that it is too complex to combine the sum of the large number addition and the one-dimensional array, discarding the method.

2. Compare the accumulation and variables in the program with the 2 30-square (the int value is the maximum of 2 31-square-1), and when the 2-bit is greater than 30, place its value in an array and re-accumulate it by 0. In this case, the end of the array to save the intermediate result is the final result, of course, the cumulative value of the array addition, but the total problem in the process, changed for a long time did not change well, we directly put that intermediate result array of the number output, so as to avoid overflow, but the final result is presented as a few numbers added.

Third, the source code

1/*======================================================================2# Author:tianyongtao && Zhangyapeng3# e-mail: [Email protected]4# Last modified:2015-03-29 21:145# Filename:Demo.cpp6# Description: one-dimensional array for maximum sub-array (resolves numeric overflow)7======================================================================*/8 # include <iostream>9 # include <time.h>Ten # include <math.h>11UsingNamespaceStd12#define LENGTH 10# define Templen100014IntArr[length];15int Temp[templen];//Array for storing intermediate results16int Maxsubarr (int * arr,int & Count,int &Temp17{18int max=0;19int flag=True;20int max1=arr[0];//Store the largest value in an array with all negative numbers21stint k = POW (2.0,30.0);22Forint i=0;i<length;i++)//Determine if all of the arrays are negative23{24if (arr[i]>MAX1)25{max1=Arr[i];27}28if (arr[i]>0)flag=False;30}31If(flag)32ReturnMAX1;33Forint i=0;i<length;i++)34{temp+=Arr[i];36if (temp>K37{temp[count++]=Temptemp=0;40}41if (temp>Max42{max=Temp44}45if (temp<0)46{temp=0;48}49}50ReturnMax51}5253//Test function54IntMain ()55{56Srand (Time (NULL));57IntMax58IntK59int temp=0;//Store intermediate Results60int count=0;//Several intermediate results are stored in the computed temp array61Forint i=0;i<length;i++)62{K=rand ()%10001-5000;//Generate a random number of -5000~+500064//int k = POW (2.0,30.0);arr[i]=K66}67Forint i=0;i<length;i++)68{cout<<arr[i]<<"";70}cout<<EndlMax =Maxsubarr (arr,count,temp);cout<<" maximum sub-array and for: ";  i= for (int.0;i<count;i++)cout<<temp[i]<<"+";  cout<<temp<<Endl;  return 0;  +}                    

  

Iv. Results of operation

  

Five, experience

Write this program, in solving the problem of overflow I and Tian Yongtao can think of a good way but realize it is very difficult, may also be because previously did not contact a similar topic, do not know what solution to solve, there are a lot of solutions but easy to write code but not much.

Previously write code only consider the implementation of the function, did not consider the time complexity, and did not consider the value overflow, now it seems that a small function of the implementation to consider too many problems, in the process of writing code later we should pay more attention to these problems, to avoid similar errors occur.

One-dimensional arrays for maximum sub-arrays (solving overflow problems)

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.