Designing a stack such that getMin() is both constant time and constant space
If the grader is complaining about "time limit exceeded," it must be because some loop is executing too many times. All of your functions are clearly O(1) — they have no loops. So which loop is executing too many times? It must be the only loop in the entire program:

    while(!isFull(n)){
        cin&gt;&gt;a;
        push(a);
    }


Is it possible that your isFull is returning an incorrect answer? It's very straightforward: it tests s.size() == n. Is it possible that push(a) is not updating s.size()?

Reflowed for brevity and clarity, your push looks like this:

void push(int a)
{
     if (s.empty()) {
         minEle = a;
         s.push(a);
     } else if (a &lt; minEle) {
         s.push(a - minEle);
         minEle = a;
     } else if (a &gt; minEle) {
         s.push(a); 
     }
}


Do you see the bug yet? The problem is that when a == minEle, you never push anything! So if you get the input 1 1, you'll cause the test driver to loop forever... and that is what causes the timeout.

Also consider that there is no limit on the values of the integers you're given (just on the number of test cases T and the number of integers per test case N). So you'll have to find a way to make your code work with input -1 as well.

Consider maintaining a second stack std::stack&lt;int&gt; minEle alongside your std::stack&lt;int&gt; s. Does that make the problem easier?



EDITED TO ADD: If you kept the current push, then you could rewrite it for even more clarity by factoring the s.push to the bottom of the function, like this:

void push(int a)
{
     if (s.empty()) {
         minEle = a;
     } else if (a &lt; minEle) {
         a -= std::exchange(minEle, a);
     } else if (a &gt; minEle) {
         // do nothing
     } else {
         return;  // uh-oh!
     }
     s.push(a);
}


This shows clearly that each call to push results in one and only one call to s.push (except for the buggy case).