Maximum Product Of Three numbers in an array

https://leetcode.com/problems/maximum-product-of-three-numbers/submissions/

Why Iam Getting Runtime Error?

Paste your code.

Sort the array in ascending order and print product of last three numbers, maybe? Then complexity would be O(nlog n)

array can have negative numbers too. therefore sorting and printing product of last three would give WA

int val1=nums[n-1]*nums[n-2]*nums[n-3];
int val2=nums[0]*nums[1]*nums[n-1];
return max(val1,val2);

If you want O(n lgn) sol.

Find max, second_max, third_max, min, second_min from the array
and print(max( min*second_min*max , max*second_max*third_max))