![]() |
VOOZH | about |
Given a Set, the task is to traverse this Set in reverse order. Examples:
Input: set = [10 20 30 70 80 90 100 40 50 60] Output: 100 90 80 70 60 50 40 30 20 10 Input: set = [1 2 3 4 5] Output: 5 4 3 2 1
Approach: To traverse a Set in reverse order, a reverse_iterator can be declared on it and it can be used to traverse the set from the last element to the first element with the help of rbegin() and rend() functions.
Below is the implementation of the above approach: Program:
Elements of Set in normal order: 10 11 12 14 15 Elements of Set in reverse order: 15 14 12 11 10
Time complexity: O(n) where n is no of elements in the given set
Auxiliary Space: O(n)