Search for a command to run...
For each element, find the first larger value to its right
Try your own input
Change the array and replay the algorithm from step one.
Initialization
For each element, scan everything to its right and take the first value that is strictly greater. If none exists, the answer is -1.
Brute Force
1function nextLargerElement(arr) {2 const n = arr.length;3 const ans = new Array(n).fill(-1);//n: 4ans: [, , , ]4 for (let i = 0; i < n; i++) {5 const currEle = arr[i];6 for (let j = i + 1; j < n; j++) {7 if (arr[j] > currEle) {8 ans[i] = arr[j];9 break;10 }11 }12 }13 return ans;14}