Binary Search for Equal Areas of Axis-Aligned Squares

Day 13 Problem Statement: You’re given an array squares where each element is: [some x‑coordinate, some y‑coordinate, side length l] Each triple describes an axis‑aligned square on the 2D plane. The goal is to find the lowest possible y‑value of a horizontal line such that: The total area above the line equals The total area below the line Important rule: Overlapping areas are counted multiple times — i.e., if two squares overlap, the overlapped region contributes to the total for each square individually. Approach : The problem is to find a horizontal line that splits all squares into equal areas above and below. To solve it: Calculate the total area of all squares. Use binary search on y-coordinate to find the line. For each candidate y, sum the area below the line for all squares (full square if below, 0 if above, partial if intersecting). Move the line up or down depending on whether the area below is less or more than half the total. Stop when the areas are balanced — that y is the answer. #HappyCoding #LeetCode #Coding #ProblemSolving #Programming #Java #SoftwareEngineering #DSA #DataStructures #Algorithms #CompetitiveProgramming #CodeNewbie #TechLearning #DailyCodingChallenge #BinarySearch #LearnToCode #DeveloperCommunity

To view or add a comment, sign in

Explore content categories