Using a Stack
To speed things up, what we can do is use an auxiliary data structure that helps us keep track of comparisons. Why don't we use a stack?
With a stack, instead of nested loop, we can do one pass instead. This is done by using the follow pseudocode:
Step 1
Push the first element to the stack.

xxxxxxxxxx17
var assert = require('assert');function nextLargerNumber(nums) { // implement this function return nums;}try { assert.deepEqual(nextLargerNumber([3, 1, 3, 4]), [4, 3, 4, -1]); console.log( 'PASSED: assert.deepEqual(nextLargerNumber([3, 1, 3, 4]), [4, 3, 4, -1])' );} catch (err) { console.log(err);}OUTPUT
Results will appear here.