C++ implementation of Hackerrank's "Maximum Element in a Stack" I just attempted the Maximum Element Challenge on Hackerrank. This question has been posted before in swift Swift Hackerrank Maximum Element in a Stack but it has attracted low views/no answers. Moving forward, based on the requirements of finding the maximum element, I used a vector&lt;int&gt; as my underlying data structure as opposed to using Stack as I can't iterate through a stack. This exercise is meant to be a simple one but the time limit gets exceeded for larger input. 

How do I improve the performance? Please feel free to criticise my approach as I'm new to C++ with 5 days of experience.


  You have an empty sequence, and you will be given \$N\$ queries. Each
  query is one of these three types:
  
  
  1 x  -Push the element x into the stack
  2    -Delete the element present at the top of the stack
  3    -Print the maximum element in the stack
  
  
  Input Format
  
  The first line of input contains an integer, \$N\$ . The next \$N\$
  lines each contain an above mentioned query. (It is guaranteed that
  each query is valid.)
  
  Constraints 
  
  
  \$1 \leq N \leq 10^5 \$
  \$1 \leq x \leq 10^9 \$
  \$1 \leq \text{type} \leq 3 \$
  
  
  Output Format
  
  For each type \$3\$ query, print the maximum element in the stack on a
  new line.
  
  Sample Input

10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3

  
  Sample Output

26
91



#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;


//using namespace std;
bool compareValues(int i, int j) { return i&lt;j; }

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int N;
    int queryType;
    int element;
     std::vector&lt;int&gt; stackOfNumbers;

    std::cin &gt;&gt; N;    
    for(int i =0; i &lt; N ; i++){
        std::cin &gt;&gt; queryType;

        switch(queryType) {
      case 1 :
            std::cin &gt;&gt; element;              
            stackOfNumbers.push_back(element);
         break;
      case 2 :
            stackOfNumbers.erase(stackOfNumbers.end()-1);
            break;
      case 3 :
           std::cout&lt;&lt; *std::max_element(std::begin(stackOfNumbers), std::end(stackOfNumbers),compareValues ) &lt;&lt; std::endl;
         break;
      default :
        std::cout &lt;&lt; "Invalid queryType" &lt;&lt; std::endl;
   }


    }
    return 0;
}
I believe to finish this within the time limit, you need to complete each of the three operations with \$O(1)\$ time complexity.

There are a number of variations in how you do that, but nearly all of them involve keeping track of the maximum value on the stack as you build the stack, rather than searching through all the existing stack items each time you receive a query. Given that this is an online challenge, I hesitate to go into a lot more detail on this point than that.

As far as the code style goes, the most obvious point would be that your compareValues isn't really necessary--std::less&lt;T&gt; will be used by default, and your comparison adds nothing it doesn't already do perfectly well. In fact, std::less&lt;T&gt; may be superior in one respect: it's defined as a function object--a class that overloads operator() to do the comparison. You'r defining an actual function, and passing a pointer to a function. Passing a function object tends to make it easier for a compiler to generate inline code, so letting std::less&lt;T&gt; do the job may well improve speed at least slightly.

One more (somewhat less obvious) point is that in the process of improving efficiency to the point that you can finish within the time limit, I'm (reasonably) confident that you'll eliminate the need to traverse the stack, so you should be able to use std::stack&lt;int&gt;.