# Last test case Time Limit Exceeded where as it take less than 2ms to execute that test case

• Below is the Java Code that I wrote for this problem. But it shows TLE in last test case whereas if I execute the same in custom test case it takes atmost 2ms to execute. Can someone tell me why?

``````public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1Length = nums1.length;
int n2Length = nums2.length;
//System.out.println(n1Length +" + " + n2Length);
if(n1Length > n2Length)
return findMedianSortedArrays(nums2,nums1);
if(n1Length <= 1 || n2Length <= 1){
if(n1Length == 0){
if(n2Length == 0)
return -1;
else if(n2Length == 1)
return nums2[0];
else if(n2Length % 2 == 0)
return (nums2[n2Length/2 - 1] + nums2[n2Length/2]) / 2.0;
else
return nums2[n2Length/2];
}/*else if(n2Length == 0){
return nums1[n1Length/2];
}*/else if(n1Length == 1){
if(n2Length == 1){
return (nums1[0] + nums2[0]) / 2.0;
}else{
int median = 0;
if((n2Length%2) == 0){ // Even Case
median = n2Length / 2 - 1;
if(nums2[median] > nums1[0])
return nums2[median];
else if(nums2[median + 1] > nums1[0])
return nums1[0];
else
return nums2[median + 1];
}else{ //Odd case
median = n2Length / 2;
if(nums2[median - 1] > nums1[0])
return (nums2[median] + nums2[median - 1]) / 2.0;
else if(nums2[median + 1] > nums1[0])
return (nums1[0] + nums2[median]) / 2.0;
else
return (nums2[median + 1] + nums2[median]) / 2.0;
}
}
}
}else if(n1Length == 2){
if(n2Length == 2){
if(nums1[0] > nums2[1]){
return (nums2[1] + nums1[0]) / 2.0;
}else if(nums2[0] > nums1[1]){
return (nums2[0] + nums1[1]) / 2.0;
}else{
return ((Math.min(nums1[1],nums2[1])) + (Math.max(nums1[0],nums2[0]))) / 2.0;
}
}else{
int med2 = 0;
if(n2Length % 2 == 0){
med2 = n2Length/2 -1;
if(nums1[1] < nums2[med2]){
return (Math.max(nums1[1],nums2[med2-1]) + nums2[med2]) / 2.0;
}else if(nums1[0] > nums2[med2 + 1]){
if(med2 + 2 < n2Length)
return (Math.min(nums1[0],nums2[med2 + 2]) + nums2[med2 + 1]) / 2.0;
else
return (nums1[0] + nums2[med2 + 1]) / 2.0;
}else{
return ((Math.min(nums1[1],nums2[med2+1])) + (Math.max(nums1[0],nums2[med2]))) / 2.0;
}
}else{
med2 = n2Length/2;
if(nums1[1]< nums2[med2]){
return Math.max(nums1[1],nums2[med2-1]);
}else if(nums1[0] > nums2[med2]){
if(med2 + 1 < n2Length)
return (Math.min(nums1[0],nums2[med2 + 1]));
else
return nums1[0];
}else{
return nums2[med2];
}
}

}
}else if(nums1[n1Length - 1] <= nums2[0]){
int[] mergedArray = new int[n1Length+n2Length];
System.arraycopy(nums1,0,mergedArray,0,n1Length);
System.arraycopy(nums2,0,mergedArray,n1Length,n2Length);
int med = mergedArray.length / 2;
if(mergedArray.length % 2 == 0)
return (mergedArray[med] + mergedArray[med - 1]) / 2.0;
else
return mergedArray[med];
}else if(nums1[0] >= nums2[n2Length-1]){
int[] mergedArray = new int[n1Length+n2Length];
System.arraycopy(nums2,0,mergedArray,0,n2Length);
System.arraycopy(nums1,0,mergedArray,n2Length,n1Length);
int med = mergedArray.length / 2;
if(mergedArray.length % 2 == 0)
return (mergedArray[med] + mergedArray[med - 1]) / 2.0;
else
return mergedArray[med];
}else{
int med1 = 0;
int med2 = 0;
if(n1Length % 2 == 0)
med1 = n1Length/2 -1;
else
med1 = n1Length/2;
if(n2Length % 2 == 0)
med2 = n2Length/2 -1;
else
med2 = n2Length/2;
if(nums1[med1] >= nums2[med2]){
if(n1Length % 2 != 0){
if(n2Length % 2 != 0){
if(n1Length != n2Length)
return findMedianSortedArrays(Arrays.copyOfRange(nums1,0,med1+1),Arrays.copyOfRange(nums2,( n1Length - med1 - 1), n2Length));
else
return findMedianSortedArrays(Arrays.copyOfRange(nums1,0,med1+1),Arrays.copyOfRange(nums2,((n1Length - med1 - 1)), n2Length));
}
else
return findMedianSortedArrays(Arrays.copyOfRange(nums1,0,med1+1),Arrays.copyOfRange(nums2,((n1Length - med1 - 1)), n2Length));
}
else{
if(n2Length % 2 != 0)
return findMedianSortedArrays(Arrays.copyOfRange(nums1,0,med1+2),Arrays.copyOfRange(nums2,((n1Length - med1 - 2)), n2Length));
else{
if(n1Length != n2Length)
return findMedianSortedArrays(Arrays.copyOfRange(nums1,0,med1+2),Arrays.copyOfRange(nums2,((n1Length - med1 - 2)), n2Length));
else
return findMedianSortedArrays(Arrays.copyOfRange(nums1,0,med1+2),Arrays.copyOfRange(nums2,((n1Length - med1 - 2)), n2Length));
}
}
}else{
if(n1Length % 2 != 0){
if(n2Length % 2 != 0)
return findMedianSortedArrays(Arrays.copyOfRange(nums1,med1,n1Length),Arrays.copyOfRange(nums2,0, n2Length + 1 -(n1Length - med1)));
else
return findMedianSortedArrays(Arrays.copyOfRange(nums1,med1,n1Length),Arrays.copyOfRange(nums2,0, n2Length + 1 - (n1Length - med1)));
}
else{
if(n2Length % 2 != 0)
return findMedianSortedArrays(Arrays.copyOfRange(nums1,med1, n1Length),Arrays.copyOfRange(nums2,0,(n2Length + 2 - (n1Length - med1))));
else
return findMedianSortedArrays(Arrays.copyOfRange(nums1,med1,n1Length),Arrays.copyOfRange(nums2,0,(n2Length + 2- (n1Length - med1))));
}
}
}
return -1;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.