Code Challenge

Consider an array of n decimal integers named elements. We want to rearrange elements according to the following rules: Sort the integers in ascending order by the number of 1's in their binary representations. For example, 710 → 1112 and 810 → 10002, so 8 (which has single 1 in binary) would be ordered before 7 (which has triple 1's in binary). Two or more integers having the same number of 1's in their binary representations are ordered by increasing decimal value. For example, 510 → 1012 and 610 → 1102 both contain double 1's in their binary representation, so 5 would be ordered before 6 because it has the smaller decimal value. Complete the rearrange function in the editor below. It has one parameter: an array of n integers, elements. The function must sort the elements array according to the rules above and return the sorted array.

by Abhishek Kumar

HTML

<h5>
  Challenge
</h5>
<p>Consider an array of n decimal integers named elements. We want to rearrange elements according to the following rules:
</p>
<ol>
  <li>
    Sort the integers in ascending order by the number of 1's in their binary representations. For example, 7<sub>10</sub> → 111<sub>2</sub> and 8<sub>10</sub> → 1000<sub>2</sub>, so 8 (which has single 1 in binary) would be ordered before 7 (which has
    triple 1's in binary).
  </li>
  <li>Two or more integers having the same number of 1's in their binary representations are ordered by increasing decimal value. For example, 510 → 101<sub>2</sub> and 6<sub>10</sub> → 110<sub>2</sub> both contain double 1's in their binary representation,
    so 5 would be ordered before 6 because it has the smaller decimal value. Complete the rearrange function in the editor below. It has one parameter: an array of n integers, elements. The function must sort the elements array according to the rules
    above and return the sorted array.
  </li>
</ol>
<h5>
  Result
</h5>
<div id="konsole">

</div>

JavaScript

function Rearrange(elements) {
  let results = elements;
  let len = elements.length;
  var dec2bin = function(num) {
    return parseInt(num, 10).toString(2);
  }
  var count1 = function(str) {
    return str.replace(/0/g, '').length;
  }
  for (let i = 0; i < len; i++) {
    for (let j = 0; j < len - i - 1; j++) {
      let l = count1(dec2bin(results[j])),
        r = count1(dec2bin(results[j + 1]));
      console.log(i, j, results[j], results[j + 1], l, r);
      if (l > r) {
        let temp = results[j];
        results[j] = results[j + 1];
        results[j + 1] = temp;
      } else if (l == r) {
        if (results[j] > results[j + 1]) {
          let temp = results[j];
          results[j] = results[j + 1];
          results[j + 1] = temp;
        } else if (results[j] == results[j + 1]) {
          results.splice(j, 1);
          len--;
        }
      }
    }
  }
  return results;
}

var konsole = document.getElementById('konsole');
konsole.innerHTML = " $ " + Rearrange([3, 1, 2, 3]);
konsole.innerHTML += "<br> $ " + Rearrange([5, 5, 3, 7, 10, 14]);
konsole.innerHTML += "<br> $ " + Rearrange([6, 5, 3, 7, 10, 14]);