Mid/SeniorArraySorting

Maximize Area of Square Hole in Grid

You are given two integers, n and m, and two integer arrays, hBars and vBars.

The grid has n + 2 horizontal bars and m + 2 vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from 1.

You can remove some of the bars in hBars from the horizontal bars and some of the bars in vBars from the vertical bars. Other bars are fixed and cannot be removed.

Return an integer denoting the maximum area of a square-shaped hole in the grid after removing some bars, possibly none.

Example 1
Inputn = 2, m = 1, hBars = [2,3], vBars = [2]
Output4
Removing horizontal bar 2 and vertical bar 2 creates a square-shaped hole with area 4.
Example 2
Inputn = 1, m = 1, hBars = [2], vBars = [2]
Output4
Removing horizontal bar 2 and vertical bar 2 creates the maximum square-shaped hole with area 4.

Constraints

  • 1 <= n <= 10^9
  • 1 <= m <= 10^9
  • 1 <= hBars.length <= 100
  • 2 <= hBars[i] <= n + 1
  • 1 <= vBars.length <= 100
  • 2 <= vBars[i] <= m + 1
  • All values in hBars are distinct.
  • All values in vBars are distinct.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate