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.



Given an array of integers, devise a program to replace every element with the next greatest element..

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

Post New Answer

More Data Structures Interview Questions

What are linked list?

1 Answers  


Which is faster array or linked list?

1 Answers  


List the data structures which are used in rdbms, network data modal, and hierarchical data model.

1 Answers  


Which sort is best for linked list?

1 Answers  


What is entryset in hashmap?

1 Answers  


What is difference between stack and queue?

1 Answers  


What can be stored in an arraylist?

1 Answers  


What are the complexity of binary search?

1 Answers  


What is the application of queue?

1 Answers  


What is data structure and why it is important?

1 Answers  


What is the complexity of bubble sort?

1 Answers  


Is bubble sort faster than selection sort?

1 Answers  


Categories