Flux Balance

Medium

A power grid monitoring system tracks energy flux at a central node. Each reading represents net energy flow: positive values indicate energy entering the node, negative values indicate energy leaving.

Engineers need to detect if there exists any contiguous period where the total energy flux is exactly zero - meaning the energy that entered equals the energy that left during that time window. This would indicate a potential feedback loop that needs investigation.

Given an array of energy flux readings, determine if any contiguous subarray sums to zero.

Input: flux = [3, 4, -7, 2, -3, 1]
Output: true
Explanation: The subarray [3, 4, -7] sums to 0.
Input: flux = [1, 2, 3]
Output: false
Explanation: No contiguous subarray sums to zero.
  • 1 <= flux.length <= 10^6
  • -10^6 <= flux[i] <= 10^6
  • The sum of any contiguous subarray fits in a 32-bit signed integer

CodeCrack Method Cheat Sheet

What’s this?

A six-step reference for working through unfamiliar coding problems, with an optional scratch pad for your approach.

Learn the method ↗
  1. 1.Understand the Problem
  2. 2.Brute Force
  3. 3.Identify the Lower Bound
  4. 4.Find Overlapping Sub-Problems
  5. 5.Consider Data Structures
  6. 6.Iterate

No submissions yet

Run your code and your attempts will show up here.