AdSense

Friday, October 14, 2016

Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.
Example 1:
Given nums = [1, -1, 5, -2, 3]k = 3,
return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)
Example 2:
Given nums = [-2, -1, 2, 1]k = 1,
return 2. (because the subarray [-1, 2] sums to 1 and is the longest)
Follow Up:
Can you do it in O(n) time?


The idea follows the similar solution as we did for Query Sum. We use a map, the key is the sum we get from index 0 to index i, and the value is index i. If we find sum - k is also in the map, we know there exists a subarray with sum k, and we compare it with the previous result.


public int maxSubArrayLen(int[] nums, int k) {
        int len = nums.length;
        if (len == 0) {
            return 0;
        }
        Map<integer, integer> sumMap = new HashMap<>();
        int sum = 0;
        int maxLen = 0;
        for (int i = 0; i < len; i++) {
            sum += nums[i];
            sumMap.put(sum, i);
            if (sumMap.containsKey(sum - k)) {
                int index = sumMap.get(sum - k);
                maxLen = Math.max(maxLen, i - index);
            }
        }
        return maxLen;
    }


2 comments:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete