Search for a command to run...
Try your own input
Change the array and replay the algorithm from step one.
Initialization
For each element, walk forward through the remaining n-1 positions, wrapping around the end using ind = (j + i) % n. The first strictly greater value wins; otherwise the answer is -1.
Brute Force
1function nextGreaterElements(arr) {2 const n = arr.length;3 const ans = new Array(n).fill(-1);//n: 6ans: [, , , , , ]4 for (let i = 0; i < n; i++) {5 const currEle = arr[i];6 for (let j = 1; j < n; j++) {7 const ind = (j + i) % n;8 if (arr[ind] > currEle) {9 ans[i] = arr[ind];10 break;11 }12 }13 }14 return ans;15}