Write a program to replace each element of an array with a number present on the right side of the element such that the number is least greater than the element. If there is no greater number replace it with -1
e.g : 8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28
ans : 18, 63, 80, 25, 32, 43, 80, 93, 80, 25, 93, -1, 28, -1, -1
Idea here is to create a BST with the elements in array starting from rightmost element in the array and keep track of left turn.
The last left turn node is the answer.
Here is the link:
http://ideone.com/UCfr8J
e.g : 8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28
ans : 18, 63, 80, 25, 32, 43, 80, 93, 80, 25, 93, -1, 28, -1, -1
Idea here is to create a BST with the elements in array starting from rightmost element in the array and keep track of left turn.
The last left turn node is the answer.
Here is the link:
http://ideone.com/UCfr8J
No comments:
Post a Comment