Stacks and Queues Difference – You are given a 0-indexed array A of length N…


You are given a 0-indexed array A of length N. You make an array B from A by inserting all the elements of A into a stack and removing the elements one by one in order. Similarly, you make an array C from A by inserting all the elements of A into a queue and removing the items one by one in order. Calculate sum(abs(CiBi)) over all i from 0 to N-1 modulo 10^9+7.

Note, that due to the fixed order of addition and removal from a stack and a queue, B and C are uniquely determined.

Related Posts

Close Bitnami banner
Bitnami