# freeCodeCamp Challenge Guide: Cut a rectangle

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-cut-a-rectangle/302242>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 6:37pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-cut-a-rectangle/302242 "2019-08-03T18:37:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 6:37pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-cut-a-rectangle/302242/1 "2019-08-03T18:37:52Z")

</div>

# [Cut a rectangle](https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/cut-a-rectangle)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function cutRectangle(w, h) {
> if (w % 2 == 1 && h % 2 == 1)
> return;
> 
> var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]];
> 
> var grid = new Array(h); for (var i = 0; i < grid.length; i++) grid[i]=new Array(w);
> var stack = [];
> 
> var half = Math.floor((w * h) / 2);
> var bits = Math.pow(2, half) - 1;
> var result=0;
> for (; bits > 0; bits -= 2) {
> 
> for (var i = 0; i < half; i++) {
> var r = Math.floor(i / w);
> var c = i % w;
> grid[r][c] = (bits & (1 << i)) != 0 ? 1 : 0;
> grid[h - r - 1][w - c - 1] = 1 - grid[r][c];
> }
> 
> stack.push(0);
> grid[0][0] = 2;
> var count = 1;
> while (stack.length!=0) {
> 
> var pos = stack.pop();
> var r = Math.floor(pos / w);
> var c = pos % w;
> 
> for (var dir of dirs) {
> var nextR = r + dir[0];
> var nextC = c + dir[1];
> 
> if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) {
> 
> if (grid[nextR][nextC] == 1) {
> stack.push(nextR * w + nextC);
> grid[nextR][nextC] = 2;
> count++;
> }
> }
> }
> }
> 
> if (count == half) {
> result++;
> }
> }
> 
> return result;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:49pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-cut-a-rectangle/302242/2 "2025-01-08T15:49:13Z")

</div>

Closed on January 8, 2025, 3:49pm UTC


