We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I too faced the same problem and solved it check the below code
The idea is very simple
for each item a[i] from the array check if it is greater than the top of stack... if yes then calc sum and add i to the stack
if a[i] is lesser than top of stack then calc sum for each popped out element from stack until you find an element from stack which is lesser then a[i]
AND xor OR
You are viewing a single comment's thread. Return to all comments →
I too faced the same problem and solved it check the below code
The idea is very simple for each item a[i] from the array check if it is greater than the top of stack... if yes then calc sum and add i to the stack if a[i] is lesser than top of stack then calc sum for each popped out element from stack until you find an element from stack which is lesser then a[i]