Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to check if an array contains a contiguous subarray having zero-sum in Java

3 Answers

0 votes
public class MyClass {
    public static boolean hasZeroSumSubarray(int[] arr) {
        int size = arr.length;
        
        for (int i = 0; i < size; i++) {
            int sum = arr[i];
            if (sum == 0)
                return true;
            for (int j = i + 1; j < size; j++) {
                sum += arr[j];
                if (sum == 0)
                    return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = { 8, 4, -5, 1, 9 };
        
        boolean hasZeroSum = hasZeroSumSubarray(arr);
        
        if (hasZeroSum) {
            System.out.println("The array contains a contiguous subarray with zero-sum");
        } else {
            System.out.println("The array does not contain a contiguous subarray with zero-sum");
        }
    }
}
 
 
 
 
/*
run:
 
The array contains a contiguous subarray with zero-sum
 
*/

 



answered Sep 8, 2023 by avibootz
0 votes
public class MyClass {
    public static boolean hasZeroSumSubarray(int[] arr) {
        int size = arr.length;
        
        for (int i = 0; i < size; i++) {
            int sum = arr[i];
            int startIndex = i;
            if (sum == 0)
                return true;
            for (int j = i + 1; j < size; j++) {
                sum += arr[j];
                int endIndex = j;
                if (sum == 0) {
                    System.out.println("index from: " + startIndex + " to: " + endIndex);
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = { 8, 32, 4, -5, 1, 9 };
        
        boolean hasZeroSum = hasZeroSumSubarray(arr);
        
        if (hasZeroSum) {
            System.out.println("The array contains a contiguous subarray with zero-sum");
        } else {
            System.out.println("The array does not contain a contiguous subarray with zero-sum");
        }
    }
}
 
 
 
 
/*
run:
 
index from: 2 to: 4
The array contains a contiguous subarray with zero-sum
 
*/

 



answered Sep 8, 2023 by avibootz
0 votes
import java.util.Set;
import java.util.HashSet;

public class MyClass {
    public static Boolean hasZeroSumSubarray(int[] arr) {
        Set<Integer> set = new HashSet<>();
 
        set.add(0);
 
        int sum = 0;
 
        for (int value: arr) {
            sum += value;

            // if set contains sum, there is a subarray with zero-sum
            if (set.contains(sum)) {
                return true;
            }
 
            set.add(sum);
        }

        return false;
    }
    public static void main(String args[]) {
        int[] arr = { 4, 3, 5, -7, -1, 8, 6, 2 };
 
        if (hasZeroSumSubarray(arr)) {
            System.out.println("Subarray exists");
        }
        else {
            System.out.println("Subarray does not exist");
        }
    }
}
 

// 3 5 -7 -1
 
 
/*
run:
 
Subarray exists
 
*/

 



answered Oct 23, 2023 by avibootz
edited Oct 24, 2023 by avibootz
...