Skip to the code
cf.
+50 XP

#119 Four Widgets Fill a Row

hard

A dashboard builder offers widgets in several widths, listed in widths (the same width can appear more than once, as different widgets). A row looks balanced with exactly four widgets whose widths add up to rowWidth.

Write fourWidgetRows(widths, rowWidth) that returns every distinct group of four width values [a, b, c, d], taken from four different positions, with a + b + c + d === rowWidth. Write each group in ascending order, list the groups in ascending order (compare first values, then second, then third), and list each distinct group once.

Example 1

Input: widths = [1,0,-1,0,-2,2], rowWidth = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

These are the three distinct groups of four that add up to 0.

Example 2

Input: widths = [3,3,3,3,3], rowWidth = 12
Output: [[3,3,3,3]]

Five widgets of width 3 give only one distinct group.

Constraints

  • 0 <= widths.length <= 200
  • -10^6 <= widths[i] <= 10^6
  • -10^6 <= rowWidth <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).