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.
Discussion :
Discussion :
public int maxSubArrayLen(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, -1); int result = 0; int sum = 0; for(int i=0; i<nums.length; i++){ sum += nums[i]; if(map.containsKey(sum - k)){ result = Math.max(result, i - map.get(sum - k)); } map.putIfAbsent(sum, i); } return result; }
Comments
Post a Comment