Software Engineering pair Development--Returns the largest subarray of integers in an integer array and (JAVA)

Source: Internet
Author: User

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

Requirements: Enter an integer array with positive 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);

First, design ideas

1. First randomly generate an array, the length of the array is self-input, the array has positive and negative numbers.

2. Start with the first element in the array a[0], and then calculate the value of a[0], a[0]+a[1], A[0]+a[1]+...+a[i], and the and of each sub-array starting from a[0], and remove the maximum value.

3. Start with the second element of the array, a[1], and then calculate the number of each sub-array and remove the maximum.

4. Loop, loop num times until the last element of the array, and save the maximum value of each estimate to an array of list[].

5. Compare the maximum value of each sub-array, and then the maximum value.

Second, the source program

1 //xueqing Lu Yu2 //Date: March 18, 20153  PackageCom.java.lianxi;4 5 ImportJava.util.*;6  Public classLianxi27 {8      Public Static voidMain (string[] args)9     {TenScanner input=NewScanner (system.in); OneSystem.out.print ("Please enter the number of numbers in the array:"); A         intnum=input.nextint (); -         intarray[]=New int[num]; -          for(inti=0;i<num;i++) the         { -             if((int) (Math.random () * *) ==0) -             { -Array[i]= (int) (Math.random () *10); +             } -             Else +             { Aarray[i]=-(int) (Math.random () *10); at             } -         } -          for(inti=0;i<num;i++) -         { - System.out.println (Array[i]); -         } in         intMax=0; -         intlist[]=New int[num]; to          for(intj=0;j<num;j++) +         { -max=Array[j]; the             intSum=0; *              for(intt=j;t<num;t++) $             {Panax Notoginsengsum=sum+Array[t]; -                 if(sum>max) the                 { +max=sum; A                 } the             } +list[j]=Max; -         } $          for(inti=0;i<num;i++) $         { -The maximum value of the System.out.print (the number of sub-arrays of the "+ (i+1) +" second comparison) is: "); - System.out.println (List[i]); the         } -          for(inti=1;i<num;i++)Wuyi         { theMax=list[0]; -             if(list[i]>max) Wu             { -max=List[i]; About             } $         } -System.out.print ("sub-array and maximum value:" +max); -     } - } A     

Third, the operation result:

Iv. Experience and summary

Our group of two people are my xueqing and Lu Yu, this topic realization method is Lu Yu think of, then the program was written by me, the problem in the program is the two of us solved together. This program basically achieve the requirements of the topic, but time complexity does not meet the requirements, in the future we will make improvements in this area, so that the procedures more perfect.

Software Engineering pair Development--Returns the largest subarray of integers in an integer array and (JAVA)

Related Article

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.