The longest-oldest array length of an unordered array in which the algorithm sums up and is less than or equal to the given value

Source: Internet
Author: User

Given an unordered array of arr, where elements can be positive, negative, and 0, given an integer k, for all of the subarray in Arr and the oldest array length less than or equal to K.

For example: arr=[3,-2,-4,0,6], k=-2, add and less than or equal to-2 of the oldest oldest array is {3,-2,-4,0}, so the result returns 4

Thinking of solving problems:

Preprocessing thought, record the information

Changes in accumulation and array

Summation and the maximum value in the array

Find the first position greater than or equal to a value with two points

Orderly fit with binary search

The first generation of Sumarr is the cumulative array of the array. Because the first 0 means that when there is no number, the sum is 0.

Next generate Sumarr the left maximum number Helparr, sumarr={0,1,3,2,7,5}->help={0,1,3,3,7,7} We only care about the accumulation and the earliest occurrence of a value greater than or equal to the position!

Helparr is an array of left-largest values in each position of Sumarr, then of course it is ordered!

So you can use two-point search Method! Finds the cumulative and earliest occurrences of a value greater than or equal to one.

 PackageTT; Public classTest72 { Public Static intMaxLength (int[] arr,intk) {                  int[] h =New int[Arr.length+1]; intsum = 0; h[0] =sum;  for(inti = 0; I!=arr.length; i++) {sum+=Arr[i]; H[i+1]=Math.max (sum, h[i]); } Sum= 0; intres = 0; intPre = 0; intLen = 0;  for(inti = 0; I!=arr.length; i++) {sum+=Arr[i]; Pre= Getlessindex (H, sum-k); Len= Pre = =-1? 0:i-pre+1; Res=Math.max (res, Len); }          returnRes; }          Public Static intGetlessindex (int[] arr,intnum) {         intLow = 0; intHigh = Arr.length-1; intMID = 0; intRes=-1;  while(Low <=High ) {Mid= (Low+high)/2; if(arr[mid]>=num) {Res=mid; High=mid-1; }Else{ Low= Mid +1; }              }                    returnRes; }          Public Static voidMain (string[] args) {int[] arr =New int[5]; arr[0]=3; arr[1]=-2; arr[2]=-4; arr[3]=0; arr[4]=6; intx = MaxLength (arr,-2);                                 SYSTEM.OUT.PRINTLN (x); }               }

The longest-oldest array length of an unordered array in which the algorithm sums up and is less than or equal to the given value

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.