1

I am trying to solve the below problem. I think my solution is working well. But, the system in which I am trying to upload the solutions, is not accepting my solution. Probably some tests are failing. May I know what am I missing?

The problem is:

As the last question of a successful interview, your boss gives you a few pieces of paper with numbers on it and asks you to compose a largest number from these numbers. The resulting number is going to be your salary, so you are very much interested in maximizing this number. How can you do this?

Sample 1

Input:

2

21 2

Output: 221

Sample 2

Input:

3

23 39 92

Output: 923923

My solution is:

function MaxSallary(nums) {
  let maxSize = Math.max(...nums).toString().length;
  let newArr = [];

  nums.map((num) => {
    while (num.toString().length < maxSize) {
      num = num.toString().concat(num.toString().split("").slice(-1)[0]);
    }

    newArr.push(Number(num));
  });

  finalArr = [];
  while (newArr.length > 0) {
    let minIndex = newArr.indexOf(Math.max(...newArr));
    newArr.splice(minIndex, 1);
    finalArr.push(...nums.splice(minIndex, 1));
  }
  return finalArr.join("");
}

console.log(MaxSallary([2, 12, 34, 11, 43, 21, 5]));
Cihat Şaman
  • 3,674
  • 4
  • 14
  • 24
  • I would guess such a automated testing tool would need `MaxSallary` to return the calculated salary as a number instead of printing it? As for the actual algorithm itself, I feel like sort+join should do somehow. – sp00m Oct 02 '21 at 11:59
  • I edited the codes but the situation is the same as before. May I have your solution, please? – Cihat Şaman Oct 02 '21 at 12:08
  • Duplicate of [python - Constructing the largest number possible by rearranging a list - Stack Overflow](https://stackoverflow.com/questions/14532105/constructing-the-largest-number-possible-by-rearranging-a-list) – user202729 Oct 02 '21 at 12:14
  • ... except for the programming language – user202729 Oct 02 '21 at 12:14

1 Answers1

1

You want to know in which order the numbers should be concatenated, so that, once parsed back to a number, the result is the highest possible. Reworded this way, it looks like we should sort the array first.

When comparing two numbers a and b, to know which one should come first, we need to know which one is higher between ${a}${b} and ${b}${a}:

.sort((a, b) => parseInt(`${b}${a}`, 10) - parseInt(`${a}${b}`, 10)))

.sort mutates the array (and returns it), so I'm cloning it first.

function MaxSallary(nums) {
  const salary = [...nums]
    .sort((a, b) => parseInt(`${b}${a}`, 10) - parseInt(`${a}${b}`, 10))
    .join("");
  return salary;
}

console.log(MaxSallary([21, 2]));
console.log(MaxSallary([23, 39, 92]));
console.log(MaxSallary([2, 12, 34, 11, 43, 21, 5]));
Cihat Şaman
  • 3,674
  • 4
  • 14
  • 24
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • The output is : ``Your output: 9.999999998888888e+108 Correct output: 9999999998888888888887777777776666666666555555554444444443333333333222222222111111111111111101010101010101010`` – Cihat Şaman Oct 02 '21 at 17:10
  • 1
    @CihatŞaman they seem to want a string as an output then, not a number. Try removing the final `parseInt` and `return salary;` directly. – sp00m Oct 02 '21 at 18:18
  • The outputs are slightly different if I use the below array as input for your and for my solutions. ``let testVal = [37, 21, 4, 86, 42, 54, 81, 53, 76, 37, 16, 63, 6, 15, 58, 93, 73, 43, 85, 2,39, 14, 63, 62, 29, 12, 56, 95, 24, 98, 95, 77, 55, 95, 28, 96, 81, 20, 47, 21, 2, 92, 87, 52, 52, 67, 73, 93, 68, 9, 2, 94, 65, 57, 89, 64, 50, 83, 16, 99, 52, 64, 100, 28, 94, 71, 58, 13, 64, 6, 56, 79, 42, 90, 75, 97, 29, 59, 32, 1, 10, 45, 50];`` if you check that array for both solutions you will see just the last 2 digits are different?Really,I want to know why outputs are'nt same. – Cihat Şaman Oct 02 '21 at 21:53
  • Or just `const maxSalary = (xs) => xs .map (String) .sort () .reverse () .join ('')` – Scott Sauyet Oct 04 '21 at 18:56