How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: Medium
Topics: Geometry, Math, Sets
You are given a large rectangular field of size (m โ 1) ร (n โ 1) with corners at:
Inside the field, there are:
hFencesvFencesYour task is to remove some fences (possibly none) and determine the maximum possible area of a square field that can be formed.
If itโs impossible to form a square, return -1.
โ ๏ธ Since the result can be very large, return the answer (10^9 + 7).
Each horizontal fence is located at:
(hFences[i], 1) โ (hFences[i], n)
Each vertical fence is located at:
(1, vFences[i]) โ (m, vFences[i])
The field is permanently surrounded by 4 fences:
(1, 1) โ (1, n)(m, 1) โ (m, n)(1, 1) โ (m, 1)(1, n) โ (m, n)These fences cannot be removed.
Find the largest square area that can be formed by removing some internal fences.
Input
m = 4, n = 3
hFences = [2, 3]
vFences = [2]
Output
4
Explanation
Removing:
Creates a 2 ร 2 square, so area = 4.
Input
m = 6, n = 7
hFences = [2]
vFences = [4]
Output
-1
Explanation
No possible way to remove fences to form a square.
๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.