//I feel this solution is easier , the only thing I am not //sure about is complexity of the code as it has one //loop that takes input and runs n times
#include
#include
using namespace std;
int main(){
stack s;
int n;
int curr_min = INT_MAX;
cout<<"enter no of elements to be inserted: ";
cin>>n;
int x;
while(n–){
cin>>x;
curr_min = min(curr_min,x);
s.push(x);
}
cout<<endl<<“Minimum is :”<<curr_min;
return 0;
}
Is My solution O(1)?
hello @Sayon-Palit-2873308702757758
bro ur logic is not correct.
suppose after pushing n elements ,i say to pop one element and then tell me the min element.
in such case ur code might fail.
for example
push 4 , 2 , 3 , 1.
u will print min as 1.
but now if i say pop and then tell me the minimum u will print 1. which is not true.
correct answer is 2