{"id":1948,"date":"2020-10-14T21:38:28","date_gmt":"2020-10-14T12:38:28","guid":{"rendered":"http:\/\/bebensiganteng.com\/?p=1948"},"modified":"2020-10-14T21:44:10","modified_gmt":"2020-10-14T12:44:10","slug":"algorithm-challenge","status":"publish","type":"post","link":"https:\/\/rahmat-hidayat.com\/?p=1948","title":{"rendered":"Algorithm Challenge"},"content":{"rendered":"\n<p>I rarely code anymore, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/bebensiganteng\/algorithm_challenge\" target=\"_blank\">this is <\/a>just a way to jog my brain.<\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction threeNumberSum(array, targetSum) {\n\n  array.sort(function(a, b) {\n\t\treturn a - b;\n\t})\n\n  let i = 0;\n  let tmp = [];\n\n  while(i &lt; array.length) {\n\n    let a = array[i];\n    \/\/ a + b + c = targetSum\n    \/\/ b + c = targetSum - a\n    let bc = targetSum - a;\n\n    let j = i+1;\n    while (j &lt; array.length) {\n\n      let b = array[j];\n      \/\/ c = (targetSum - a) - b\n      let c = bc - b;\n\n      if(array.indexOf(c) &gt; -1 &amp;amp;&amp;amp; b &lt; c) {\n        tmp.push([a,b,c]);\n      }\n\n      j++;\n    }\n\n    i++;\n  }\n\n  return tmp;\n\n}\n\nlet input = {&quot;array&quot;: [12, 3, 1, 2, -6, 5, -8, 6], &quot;targetSum&quot;: 0}\n\/\/ let input = {&quot;array&quot;: [1, 2, 3], &quot;targetSum&quot;: 6}\nconsole.log(threeNumberSum(input[&quot;array&quot;], input[&quot;targetSum&quot;]));\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>I rarely code anymore, this is just a way to jog my brain.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[209],"tags":[],"_links":{"self":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts\/1948"}],"collection":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1948"}],"version-history":[{"count":2,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts\/1948\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts\/1948\/revisions\/1950"}],"wp:attachment":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}