Given a huge integer array, want to compute product of each element when it smaller than the given number.
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], max = 50
product: 1*1, 1*2, 1*3, ... 4*4..., 10*10
sum = 0;
for(i=0,i<size,i++) {
for(j=0,j<size,j++){
if(arr[i]*arr[j] <= max) { // any product in range [0,max] is acceptable
sum++;
} // else {break;} // I also tried to cut down here, but it does not work for me
}
}
return sum;I used this logic, and it only passed 40% testcase. I assume it caused by runout time. But there is a case I did not pass because my sum is larger than his given output.
Suggested Approaching: