Help needed in solving subarray-functions from hackerearth

I was trying to solve Sub-array functions problem from hackerearth but I couldn’t solve it, please help me out.

Note:-
Please don’t propose the brute force solutions