HOW CAN I SOLVE THIS USING STACK?
1 Like
well, have you solved this question ? I think it can be solved in O(n) without using stack as well.
1 Like
yes i have solved it without using stack…i was just thinking how to solve this using stack…
1 Like
yes i have applied this approach…
1 Like
Initially if N = 1, print -1. Here’s a rough algorithm for the rest:
- while input != empty(), S.push(input())
- A.push(S.pop()) while the popped elements are in reverse order
- If S = empty(), print -1
- L <- S.pop()
- O(push(S.pop())) while S != empty()
- print O.pop() while O != empty()
- T <- 9
- O.push(num <- A.pop()) and T <- min(T, num) while A != empty()
- Print T
- While O != empty(), num <- O.pop() and print s.t.:
i. T is not printed the first time it is encountered
ii. Print L when L = min(L, num)
yes…thanks brother
1 Like
No problem 