code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

Subarraywithzerosum

import java.util.HashSet;

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class SubarrayWithZeroSum {

  static int func(int[] nums){
    HashSet<Integer> hs = new HashSet<>();
    int prefixSum = 0;
    hs.add(0);
    for(int i = 0; i < nums.length; i++){
      prefixSum += nums[i];
      if(hs.contains(prefixSum)){
        System.out.println(hs);
        return 1;
      }
      hs.add(prefixSum);
    }
    return 0;
  }

  public static void main(String[] args){
    int[] arr = new int[] {-3, 4, -3, -1, 1};
    int result = func(arr);
    System.out.println(result);
  }
}

Created 2021-12-26T23:20:21+00:00 · Edit