Maximum Distance in Array




@Ryanm In that case also, the distance calculation isn't maxmin, but it is MAX(maxa[0], a[n1]min). Here a[0] and a[n1] belong to the next array being considered. Thus, the distance is always calculated among separate arrays only. You can see the illustration above which includes this case as well.




In the third solution you need to check if the array[i] is empty or not.
For example, an array like this: array = {{1,2,3}, {4,5} , {1,2,3} , {}}.Also needed to check if array is empty or not.
For example, an array like this: array = {{}}.I corrected the code as below:
public static int maxDistance (int [][] arrays){ int j=0; for (j=0; j<arrays.length && arrays[j].length ==0;j++); if (j == arrays.length){ return 0; } int res = 0; int min = arrays[0][0]; int max = arrays[0][arrays[0].length1]; for (int i=j; i<arrays.length; i++){ if (arrays[i].length == 0){ continue; } res = Math.max(res , Math.abs(arrays[i][arrays[i].length1]  min)); res = Math.max(res , Math.abs((max  arrays[i][0]))); min = Math.min(min , arrays[i][0]); max = Math.max(max , arrays[i][arrays[i].length1]); } return res; }