code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

492 Construct The Rectangle

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function constructTheRectangle(area) {
  let width = Math.floor(Math.sqrt(area))
  while(width > 0){
    let length = area/width
    if(Number.isInteger(length)){
      return [length, width]
    }
    width--
  }
}

test('construct The Rectangle', () => {
  expect(constructTheRectangle(4)).toEqual([2,2])
});

Created 2020-04-18T20:26:17+00:00 · Edit