A Java Swing desktop app that finds the longest contiguous subarray with an equal number of 0s and 1s.
Longest Balanced Binary Subarray Finder is a lightweight Java Swing desktop application that solves a classic array problem: given a binary array (only 0s and 1s), find the length of the longest contiguous subarray containing an equal number of 0s and 1s.
The app runs two implementations side by side so results can be cross-checked instantly:
| 🔁 Non-recursive | ♻️ Recursive |
|---|---|
| Iterative prefix-sum approach | Recursive prefix-sum approach |
- 🖥️ Simple, intuitive Java Swing graphical interface
- ⌨️ Space-separated binary array input
- ⚖️ Side-by-side comparison of recursive vs. non-recursive results
⚠️ Clear, descriptive validation messages for invalid input- 🧾 Clean output panel showing the parsed array and final result
The core trick: treat each 0 as -1 and each 1 as +1, then track running prefix sums.
When the same prefix sum appears twice, the elements between those two indices must contain an equal number of
0s and1s. The maximum distance between two indices sharing a prefix sum gives the longest balanced subarray length.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Non-recursive | O(n) |
O(n) |
| Recursive | O(n) |
O(n) |
.
├── src/
│ ├── LongestBalancedSubarray.java
│ └── LongestBalancedSubarrayGUI.java
├── screenshots/
└── README.md
1. Compile the project
javac src/*.java2. Run the GUI
java -cp src LongestBalancedSubarrayGUI0 1
0 1 0
0 0 1 0 1 1 0
0 0 0 0
![]() Balanced input → result 2 |
![]() Short array → result 2 |
![]() Long array → result 6 |
![]() All zeroes → result 0 |
![]() Spacing error |
![]() Invalid format |
![]() Only 0/1 allowed |
![]() Letter input error |
Created with ❤️ by malakmohamed217






