The oldest array length in the unsorted array and for the given value

Source: Internet
Author: User

Title: Given an array of arr, the array is unordered, but each value is a positive number, and a positive k is given. All the elements in the sub-array are added by Arr and the oldest array length of K.

Solution: The optimal solution can be the time complexity of O (n), the additional space complexity of O (1). First, the left and right ends of the subarray are labeled with two positions, left and right, starting at the leftmost (left=0,right=0) of the array. The overall process is as follows:

1. Start with variable left=0,right=0, representing sub-array arr[left......right].

2. The variable sum always represents the sum of the sub-array arr[left......right]. At the beginning, sum=arr[0], or arr[0....0].

3. The variable Len keeps track of the length of the largest subarray in all the sub-arrays that accumulate and are k. At first, len=0.

4. The result of the comparison between Sum and K determines whether to move left or right, as follows:

(1) If sum==k, description arr[left.....right] accumulate and K, if arr[left.....right] length is greater than Len, then with the new Len, because all the values of the array are positive, then all start from the left position, The sub-array at the end of the position after right, accumulating and must be greater than K. So let left add 1, which means we start to look at the sub-array starting at the position after left and make sum=sum-arr[left],sum represent Arr[left+1.....right]

(2) If sum is less than K, the description arr[left.....right] also need to add the value behind right, its sum may reach K, so right+1,sum+=arr[right]. It is important to note that the right+1 is out of bounds.

(3) If sum is greater than K, all the sub-arrays that start at the left position and end in position after right are accumulated and must be greater than k, so we examine the sub-array starting at the position immediately after leaving, and sum-=arr[left],sum at this time representing Arr[left +1....right] of the cumulative sum.

5. If right<arr.length, repeat step 4. Otherwise, return directly to Len, the entire process is complete.

The specific reference code is as follows:

1  Public  intGetmaxlength (int[] arr,intk)2 {3     if(arr==NULL|| arr.length==0 | | K<=0)4                   return0;5     intLeft=0;6     intRight=0;7     intSum=arr[0];8     intLen=0;9      while(right<arr.length)Ten    { One        if(sum==k) A      { -Len=math.max (len,right-left+1); -sum-=arr[left++]; the}Else if(sum<k) -      { -right++; -          if(right==arr.length) +                 Break; -sum+=Arr[right]; +     } A    Else atsum-=arr[left++]; -    } -  -   returnLen; -}
View Code

The oldest array length in the unsorted array and for 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.