Replace With Next Largest Number

Published by Helen Yu in

Write a function that replaces each integer with the next largest in the array.

Examples

replaceNextLargest([5, 7, 3, 2, 8]) ➞ [7, 8, 5, 3, -1]

replaceNextLargest([2, 3, 4, 5]) ➞ [3, 4, 5, -1]

replaceNextLargest([1, 0, -1, 8, -72]) ➞ [8, 1, 0, -1, -1]

Notes

  • Replace the maximum with -1.
  • Elements in the array will be unique.
  • You don't have to swap the elements.
Watch a quick demo on how Edabit works.