3 things that I see:
First of all, note the difference between the incremental operators i++ and ++i.
You use ++i and ++j in the statement of your for-loops.
++i will first increment the value and then use it. i++ will use the value and then increment it.
This means that your loops are actually beginning from 1 instead of 0, because you are setting var i/j to zero and then incrementing it immediately. So you’re entirely missing the first value of each subarray.
You want to use i++ here.
Secondly, you’re setting your initial max value outside of both for-loops and thus outside of the entire array of arrays, meaning that the max never gets reset for each subarray of which you want to find the max value.
When your loops move on to the next subarray, you are comparing the new array’s values to the max value from the last array instead of comparing to the first value within their own array.
This means that an entire subarray could be skipped if the max value from the last array is greater than any of the new array’s values.
You MUST reset your max before looping through a new subarray - i.e. inside your first for-loop.
Last of all, your code here is problematic:
if(arr[i][j] > max)
max = arr[i][j];
You’re adding to your result array every time you find a value bigger than your current max, instead of when you find the actual max of each subarray.
This means for an array like
[32, 35, 37, 39], every one of those values is being added to your result array, including 32 because 32 is being compared to 27, the max value from the previous subarray (which is why you must reset your max value for each subarray before looping through it).
You want to finish looping through the subarray, updating
max as you go and only push to your result array once you have looped through the entire subarray.
Hope that helps out!