0
An array A of size N having positive integers is given. Another array B having permutation of numbers 1 to N is given. Your task is to perform N steps :

At i-th step, you have to remove element A[B[i]] from array A.

After removing the elements, the array might get broken in multiple segments. Find the segment with maximum element sum after each step of removal.
Return an array of size N having maximum sum after each step.
eg:
A: [10, 20, 5, 17, 22] B: [4, 1, 3, 5, 2]
Ans: [35, 25, 22, 20, 0]

eg-2
A: [10, 20, 5, 17, 2, 7, 8, 22, 13, 14, 19, 9] B: [7, 4, 8, 10, 1, 9, 3, 12, 5, 6, 2, 11]
Ans: [77,77,55,35,28,28,28,20,20,20,19,0]

will get solution in O(n2) but i want solution in o(n)

user972404
  • 11
  • 3

0 Answers0