One day algorithm problem (1)---the oldest array in the unsorted array and for the given value

Source: Internet
Author: User
Tags array length

    • Topic

Given an unordered array, arr, where elements can be positive, can be negative, can be 0, given an integer k. The length of the oldest array in all the sub-arrays of arr and for K.

    • Analysis

To solve the problem, introduce a concept where s (i) represents the sum of all elements of the subarray arr[0..i]. The sum of the Subarray arr[j-1, I] (0<=j<=i<arr.length) is then S (i)-S (j-1).

1. Set the variable sum=0, which represents the and of all elements that have been added to the I position starting from the 0 position. Sets the variable len=0, which represents the sum of the oldest array lengths for K. Defines a hashmap where key is the sum value and value is the first occurrence of the sum value.

2. Start the traverse from left to right, the current element is arr[i]

1) sum = sum + arr[i], i.e. s (i), in map to see if there is a sum-k

If sum-k exists, remove the sum-k corresponding value from the map, recorded as J, to get S (i)-S (j) =k, so the length of arr[j+1, I] is the length of the sub-array required by the title, if the length is greater than Len, then update Len.

2) Check that the current sum (that is, S (i)) is in the map, and if the description is not present for the first time, add the record to the map.

* Note that according to the sum of arr[j+1, I] and for S (i)-S (j), if starting from 0 accumulate, then j+1>=1. means that all sub-arrays starting from 0 are not considered, so it should be added from the-1 position, that is, before the traversal, put (0,-1) into the map, that is, if any of the numbers are not overtime, and the sum is 0.

    • Code (Java)
ImportJava.util.HashMap;ImportJava.util.Map; Public class_01_array_maxlength { Public intMaxLength (int[] arr,intk) {if(arr = =NULL|| Arr.length = = 0) {            return0; } Map<integer, integer> map =NewHashmap<integer, integer>(); Map.put (0,-1); intLen = 0; intsum = 0;  for(inti = 0; i < arr.length; i++) {sum+=Arr[i]; if(Map.containskey (Sum-k)) {Len= Math.max (I-map.get (Sum-k), Len); }            if(!map.containskey (sum))            {map.put (sum, i); }        }        returnLen; }}

    • Supplementary topics

1. Given an unordered array of arr, where elements can be positive, negative, and 0. The length of the oldest array that is equal to the number of negative numbers in all the sub-arrays of arr.

Change all the positive numbers of the array to 1, the negative numbers to -1,0 unchanged, and then the oldest array length of 0.

2. Given an unordered array of arr, where the element is only 1 or 0. Find all of Arr's subarray of 0 and 1 numbers equal to the oldest array length

All 0 of the array is changed to -1,1 unchanged, and then the oldest array length of 0 is calculated.

    • Resources
    1. Programmer Code Interview Guide left Chengyun

One-day algorithm problem (1)---the oldest array in an unordered array and for a 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.