notice that, for an index i:
SL(i) + SR(i) = total number of elements in array less than A[i]
GL(i) + GR(i) = total number of elemets in array greater than A[i]
since in the end you have to anyways add sl, sr, gl and gr no need to calc each of them seperately, instead for each element of array find number of elements less than it and number of elements greater than it.
To do that also since in the end you only need maximum power, you can sort and find out for element. (by finding total occurance of the element. Power would be length of array - total occurances of element)
in the given test case
for element 2 - count = 1 => power = 5 - 1 = 4
for element 3 : count = 2 => powet = 5 - 2 = 3
you can see anywhere 3 is there in array, its power will be coming 3 only.
So in the end just find element having maximum occurances and then return length - count(maximum occuring element)
(Dont know if this is correct tho, please check)