Maximum contiguous subarray

Given an array A consisting of n integers , we define beauty of the array as maximum sum of some contiguous elements with performing atmost one operation which is , you can multiply any subarray with ‘x’ but only once.
N=5,x=-2

A=[-3,8,-2,1,-6]

ans:- 22

sir / mam , couldnt even approach the question ., it is given in the quiz

hello @somilgupta4

multiply the subarray
-2 1 -6 by x
u get
-3 8 4 -2 12
take the sum of
8 4 -2 12
which is 22
that is the answer
the approach is simple, u need to find subarray with minimum sum if x is -ve and multiply that with x
if x is positive find subarray with maximum sum and multiply x with that
handle separately for -1 1 and 0

yes sir i knew the answer , but i knw kadane would be applied but how i dont know

because there would be two cases

  1. kadne applied without multiplying with x
  2. kadane applied by multiplying it with x

first apply simple kadane to find the max sum subarray say s1,
now find the min sum sub array again with the help of kadane (how? just flip the sign of the numbers and then apply kadane whatever answer come take its negative) say2

now answer will be
max(s1,s2,x* s1,x* s2)

refer this for dp solution

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.