1 //New presum 2 class Solution { 3 public boolean checkSubarraySum(int[] nums, int k) { 4 if(k == 0){ 5 for(int i = 1; i < nums.length; i++){ 6 if(nums[i] == 0 && nums[i-1] == 0) return true; 7 } 8 return false; 9 }10 int[] presum = new int[nums.length + 1];11 for(int i = 0; i < nums.length; i++){12 presum[i+1] = presum[i] + nums[i];13 }14 for(int i = 0; i <= nums.length-2; i++){15 for(int j = i+2; j <= nums.length; j++){16 if((presum[j] - presum[i]) % k == 0){17 return true;18 }19 }20 }21 return false;22 23 }24 }25 26 //Old27 class Solution {28 public boolean checkSubarraySum(int[] nums, int k) {29 30 Listlist = new ArrayList<>();31 for(int i = 0; i < nums.length; i++){32 for(int j = 0; j < list.size(); j++){33 // System.out.println(list.get(j) + nums[i]);34 if((k == 0 && list.get(j) + nums[i] == 0) || (k != 0 && (list.get(j) + nums[i]) % k == 0)){35 return true;36 }else{37 list.set(j, list.get(j) + nums[i]);38 }39 }40 list.add(nums[i]);41 }42 return false;43 44 }45 }