Create an algorithm to identify what is the next largest element on a stack (using stack/queue operations only) INPUT: [ 10, 3, 1, 14, 15, 5 ] OUTPUT: 10 -> 14 3 -> 14 1 -> 14 14 -> 15 15 -> -1 5 -> -1

Use the following C++ algorithm to identify the next largest element on a stack using stack/queue operations only and provide explanations for each step:

#include <iostream>
#include <stack>
#include <vector>
#include <unordered_map>

void nextLargestElement(const std::vector<int>& input) {
    std::stack<int> stack;
    std::unordered_map<int, int> nextLargest;

    for (int i = 0; i < input.size(); ++i) {
        while (!stack.empty() && input[i] > stack.top()) {
            nextLargest[stack.top()] = input[i];
            stack.pop();
        }
        stack.push(input[i]);
    }

    while (!stack.empty()) {
        nextLargest[stack.top()] = -1;
        stack.pop();
    }

    for (int num : input) {
        std::cout << num << " -> " << nextLargest[num] << " ";
    }
}

int main() {
    std::vector<int> input = {10, 3, 1, 14, 15, 5};
    nextLargestElement(input);

    return 0;
}

Explanation: - The algorithm uses a stack and a map to store the next largest element for each element in the input vector. - It iterates through the input vector and compares elements with the top of the stack to find the next largest element. - If it finds a next largest element, it updates the map with the appropriate value. - Finally, it prints the input elements along with their corresponding next largest elements.