
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Next Greater Element in Circular Array in JavaScript
Circular Array
An array in which the next element of the last element is the first element of the array is often termed as circular.
Obviously, there exists no such mechanism to store data like this, data will still be stored in continuous memory blocks and circular arrays are more like an idea than reality.
Problem
We are required to write a JavaScript function that takes in a circular array of Integers, arr, as the first and the only argument.
The function should then construct and return an array that contains the next greater element for each corresponding element of the original array. The Next Greater Number of a number, say num, is the first greater number to its traversing-order (right in our case) next in the array, which means we could search circularly to find its next greater number. If that doesn't exist, we should consider -1 for this number.
For example, if the input to the function is −
const arr = [7, 8, 7];
Then the output should be −
const output = [8, -1, 8];
Output Explanation
Next greater to both the 7s in the array is 8 and since the array is circular but for 8, there’s no greater element hence we put -1 for it.
Example
The code for this will be −
const arr = [7, 8, 7]; const nextGreaterElement = (arr = []) => { const res = []; const stack = []; if (!arr || arr.length < 1){ return res; }; for (let i = 0; i < arr.length; i++) { while (stack.length > 0 && arr[stack[stack.length - 1]] < arr[i]) { const small = stack.pop(); res[small] = arr[i]; }; stack.push(i); } for (let i = 0; i < arr.length; i++) { while (stack.length > 0 && arr[stack[stack.length - 1]] < arr[i]) { const small = stack.pop(); res[small] = arr[i]; }; } const rem = stack.length; for (let i = 0; i < rem; i++) { res[stack.pop()] = -1; } return res; }; console.log(nextGreaterElement(arr));
Code Explanation:
While iterating over the array if we find an element which is bigger than one in the stack, we set res[small] to the current larger element found.
Now, we again begin from the start of arr and deal with elements for which we couldn't find a next bigger element in the previous for loop. Finally, still there would be some elements for which there was no next greater element.
Output
And the output in the console will be −
[8, -1, 8]
- Related Articles
- Finding distance to next greater element in JavaScript
- Next Greater Element in C++
- Next Greater Element II in C++
- Next Greater Element III in C++
- Elements greater than the previous and next element in an Array in C++
- Find next Smaller of next Greater in an array in C++
- Next greater element in same order as input in C++
- Finding next greater node for each node in JavaScript
- Program to find circular greater element to the right in Python
- Returning just greater array in JavaScript
- Finding element greater than its adjacent elements in JavaScript
- Find closest greater value for every element in array in C++
- Maximum subarray sum in circular array using JavaScript
- Program to count elements whose next element also in the array in Python
- Next Smaller Element in C++
