{"id":1185,"date":"2019-10-08T17:35:03","date_gmt":"2019-10-08T21:35:03","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=1185"},"modified":"2019-10-08T23:25:39","modified_gmt":"2019-10-09T03:25:39","slug":"matrix-matching","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=1185","title":{"rendered":"Matrix Matching"},"content":{"rendered":"\n<p>You are given 3 parameters: int U, int L, and array C containing n ints. Find a matrix with 2 rows and n columns where:<br>&#8211; Each element is either 1 or 0.<br>&#8211; The sum of ints in the upper row is U.<br>&#8211; The sum of ints in the lower row is L.<br>&#8211; C is an array of ints of length n, where the sum of ints in k-th column is C[k].<br><br>Convert each matching matrix to a comma separated string and return them in an array. If there is no match, return &#8220;IMPOSSIBLE&#8221;.<br><br>For example, given:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">U = 2\nL = 2\nC = [2, 0, 2, 0]<\/pre>\n\n\n\n<p>Its only valid matrix and result is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1  0  1  0\n1  0  1  0\n\n[ \"1010,1010\" ]<\/pre>\n\n\n\n<p>To solve, simply increment through ints, convert them to binary arrays, and compare sums. Since C is of length n, the range of numbers to check is 0 to 2^n &#8211; 1.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function solution(U, L, C) {\n  const max = Math.pow(2, C.length)\n  const matches = []\n\n  for (let upper = 0; upper &lt; max; upper++) {\n    let upperArray = intToBinaryArray(upper, C.length)\n\n    if (!isBitSumEqual(upperArray, U))\n      continue\n\n    for (let lower = 0; lower &lt; max; lower++) {\n      let lowerArray = intToBinaryArray(lower, C.length)\n\n      if (!isBitSumEqual(lowerArray, L))\n        continue\n\n      if (isMatch(upperArray, lowerArray, U, L, C))\n        matches.push(<code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">${upperArray.join(&quot;&quot;)},${lowerArray.join(&quot;&quot;)}<\/code>)\n    }\n  }\n\n  if (matches.length > 0)\n    return matches\n\n  return \"IMPOSSIBLE\"\n}\n\nfunction isMatch(upperArray, lowerArray, U, L, C) {\n  for (let i = 0; i &lt; C.length; i++) {\n    let u = upperArray[i]\n    let l = lowerArray[i]\n    let c = C[i]\n\n    if (u + l != c)\n      return false\n  }\n\n  if (!isBitSumEqual(upperArray, U))\n    return false\n\n  if (!isBitSumEqual(lowerArray, L))\n    return false\n\n  return true\n}\n\nfunction isBitSumEqual(array, sum) {\n  let total = 0\n\n  for (let i = 0; i &lt; array.length; i++) {\n    total += array[i]\n  }\n\n  return (total == sum)\n}\n\nfunction intToBinaryArray(n, length) {\n  let binary = n.toString(2)\n  let binaryPadded = binary.padStart(length, '0')\n  let binStringArray = binaryPadded.split(\"\")\n  let binIntArray = binStringArray.map(Number)\n  return binIntArray\n}\n\nlet U = 2\nlet L = 2\nlet C = [2,0,2,0]\n\nconsole.log(<code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">U: ${U} L: ${L} C: ${C}<\/code>)\nlet result = solution(U, L, C)\nconsole.log(result)\n\nU = 3\nL = 2\nC = [2,1,1,0,1]\n\nconsole.log(<code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">U: ${U} L: ${L} C: ${C}<\/code>)\nresult = solution(U, L, C)\nconsole.log(result)\n\n\nU: 2 L: 2 C: 2,0,2,0\n[ '1010,1010' ]\nU: 3 L: 2 C: 2,1,1,0,1\n[ '10101,11000', '11001,10100', '11100,10001' ]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You are given 3 parameters: int U, int L, and array C containing n ints. Find a matrix with 2 rows and n columns where:&#8211; Each element is either 1 or 0.&#8211; The sum of ints in the upper row is U.&#8211; The sum of ints in the lower row is L.&#8211; C is an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":24,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1185"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1185"}],"version-history":[{"count":39,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1185\/revisions"}],"predecessor-version":[{"id":1247,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1185\/revisions\/1247"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}