Title: Returns the and of the largest sub-array in an integer array. (Requires the program to be able to handle 1000 elements)

Source: Internet
Author: User

1. Title: Returns the and of the largest subarray in an integer array.

2, requirements: The program must be able to handle 1000 elements; Each element is of type int32; Enter an array of shapes, with positive numbers in the array and negative values.  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). 3, design ideas: The array size is defined as 1000, for each element is defined as the Int32 type, we take the number when it is multiplied by 4294967296, so that the elements within the array can be crossed. 4. Program code:
1#include <iostream>2#include <stdlib.h>3#include <time.h>4 using namespacestd;5 6 intMain ()7 {8     inti;9     inta[10000];Ten     intMax =0; One     intb =0; A  - Srand (Time (NULL)); -cout<<"the array is:"<<Endl; the      for(i =0; i<10000; i++) -     { -A[i] = rand () *4294967296 ; -     } +      for(i =0; i<10000; i++) -     { +cout << A[i] <<'\ t'; A     } atcout <<Endl; -  -      for(i =0; I <10000; i++) -     { -B + =A[i]; -         if(b <0) inb =0; -         if(B >max) toMax =b; +     } -     if(max = =0) the     { *max = a[0]; $          for(i =0; I <10000; i++)Panax Notoginseng         { -             if(Max <A[i]) the             { +Max =A[i]; A             } the         }     +     } -cout <<"the maximum number of sub-arrays is:"<< Max <<Endl; $System"Pause"); $     return 0; -}

6. Operation:

When there is no overflow

7. Experimental Thoughts:

It can be seen that when the program overflow, the results of the operation will be displayed as 0, the program error, and no overflow, because we take a number of too many (10,000), the operation time of the program has increased a lot, which let us realize that in the process of large amounts of data processing, the time complexity of the algorithm is important.

8, the Truth map:

Title: Returns the and of the largest sub-array in an integer array. (Requires the program to be able to handle 1000 elements)

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.