Golang
Golang

 

Go Program


package main

import "fmt"

// checkSubarrayWithZeroSum checks if there exists a subarray with a sum of zero
// in the given array of integers.
// 
// Parameters:
// - arr: an array of integers
//
// Returns:
// - true if a subarray with sum zero exists, otherwise false.
func checkSubarrayWithZeroSum(arr []int) bool {
    // Create a map to store the cumulative sum and its index
    sumMap := make(map[int]int)
    // Initialize cumulative sum
    cumulativeSum := 0

    // Iterate through the array
    for i, num := range arr {
        // Update the cumulative sum
        cumulativeSum += num

        // Check if the cumulative sum is zero or if it has been seen before
        if cumulativeSum == 0 || sumMap[cumulativeSum] != 0 {
            return true
        }
        
        // Store the cumulative sum in the map with its index
        sumMap[cumulativeSum] = i + 1
    }

    return false
}

func main() {
    // Test the function with an example array
    arr := []int{4, 2, -3, 1, 6}
    if checkSubarrayWithZeroSum(arr) {
        fmt.Println("A subarray with a sum of zero exists.")
    } else {
        fmt.Println("No subarray with a sum of zero exists.")
    }
}
    

Program Structure Explanation

The program consists of a function checkSubarrayWithZeroSum and a main function.

  • checkSubarrayWithZeroSum:
    • This function takes an array of integers as input.
    • It maintains a cumulative sum and a map to track previously seen cumulative sums.
    • If the cumulative sum becomes zero at any point, or if it has been encountered before, the function returns true, indicating that a subarray with a sum of zero exists.
  • main:
    • This is the entry point of the program.
    • It initializes an example array and calls the checkSubarrayWithZeroSum function.
    • It prints a message based on the result of the function.

How It Works

The program iterates through the given array while maintaining a cumulative sum. It checks two conditions:

  1. If the cumulative sum equals zero, it indicates that the subarray from the start to the current index has a sum of zero.
  2. If the cumulative sum has been seen before, it implies that the elements between the previous index of that cumulative sum and the current index sum to zero.

This approach runs in O(n) time complexity, making it efficient for checking the existence of a zero-sum subarray.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)