Given an array of integers, devise a program to replace every element with the next greatest element on the right side in the array. Also, replace the last element with 5 as there no element on the right side of it.
Answer / Lokendra Singh Thakur
Algorithm:
1. Initialize a variable 'nextGreater' to store the next greater element. Set it to -1 for all elements.
2. Iterate through the array from the end towards the beginning, updating 'nextGreater' values as you go.
3. After finding the 'nextGreater' value for an element, move backwards in the array and replace the current element with its 'nextGreater' value if it is greater than the current one.
4. Replace the last element with 5.
| Is This Answer Correct ? | 0 Yes | 0 No |
What are linked list?
Which is faster array or linked list?
List the data structures which are used in rdbms, network data modal, and hierarchical data model.
Which sort is best for linked list?
What is entryset in hashmap?
What is difference between stack and queue?
What can be stored in an arraylist?
What are the complexity of binary search?
What is the application of queue?
What is data structure and why it is important?
What is the complexity of bubble sort?
Is bubble sort faster than selection sort?