# JAVA----------------------Easy Version To Understand!!

• ``````public static int findKthSmallest(int[] a, int m, int begin1, int[] b, int n, int begin2, int k) {

if (m > n)
return findKthSmallest(b, n, begin2, a, m, begin1, k);
if (m == 0)
return b[begin2 + k - 1];
if (k == 1)
return Integer.min(a[begin1], b[begin2]);
int partA = Integer.min(k / 2, m), partB = k - partA;
if (a[begin1 + partA - 1] == b[begin2 + partB - 1])
return a[begin1 + partA - 1];
else if (a[begin1 + partA - 1] > b[begin2 + partB - 1])
return findKthSmallest(a, m, begin1, b, n - partB, begin2 + partB, k - partB);
else
return findKthSmallest(a, m - partA, begin1 + partA, b, n, begin2, k - partA);

}

public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length, len2 = nums2.length, sumLen = len1 + len2;
if (sumLen % 2 != 0) {

return findKthSmallest(nums1, len1, 0, nums2, len2, 0, sumLen / 2 + 1);
} else {
return (findKthSmallest(nums1, len1, 0, nums2, len2, 0, sumLen / 2)
+ findKthSmallest(nums1, len1, 0, nums2, len2, 0, sumLen / 2 + 1)) / 2.0;
}

}``````

• I think the recursive calling still can be optimized. The array length of smaller value can be reduced from m to partA, and the array length of bigger value can be from n to partB. But here you still keep the array size.

• Great solution!
@LionelWang agreed, can be optimized like this

``````    	else if (a[begin1 + partA - 1] > b[begin2 + partB - 1])
return findKthSmallest(a, partA, begin1, b, n - partB, begin2 + partB, k - partB);
else
return findKthSmallest(a, m - partA, begin1 + partA, b, partB, begin2, k - partA);
``````

• I want to know why java program doesn't have a {public static void main()}

• ``````public class Solution {
public static int[] sort(int[] ori) {
for (int i = 0; i < ori.length; i++) {
for (int j = i; j < ori.length; j++) {
if (ori[i] > ori[j]) {
int temp = ori[i];
ori[i] = ori[j];
ori[j] = temp;
}
}
}
return ori;
}
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
int sumLen = len1 + len2;
int[] sumArray = new int[sumLen];
for (int i = 0; i < len1; i++) {
sumArray[i] = nums1[i];
}
for (int j = 0; j < len2; j++) {
sumArray[j + len1] = nums2[j];
}
for (int i = 0; i < sumArray.length; i++) {
System.out.print(sumArray[i]);
}
int[] sortedArray = sort(sumArray);
if (sumLen%2==0) {
return (sortedArray[(sumLen/2)-1]+sortedArray[(sumLen/2)])/2.0;
}
else{
return sortedArray[(sumLen/2)];
}
}
}
``````

• @ggakki In sites where you do competitive programming or want to test the application of your concepts, like Hackerrank, LeetCode or others, you're not expected to write the main() function. Only the logic of the given method has to be built in most cases along with writing fluent code.

• @HelloWorld123456 I want to answer you a small question.In your code,'Integer.min",it means the minimum between the two numbers.Is there this function in Integer?Thank you.

• @ggakki because the code will be used in server machine

• int partA = Integer.min(k / 2, m), partB = k - partA;
if (a[begin1 + partA - 1] == b[begin2 + partB - 1])
return a[begin1 + partA - 1];
else if (a[begin1 + partA - 1] > b[begin2 + partB - 1])
return findKthSmallest(a, m, begin1, b, n - partB, begin2 + partB, k - partB);
else
return findKthSmallest(a, m - partA, begin1 + partA, b, n, begin2, k - partA);

could you explain why we are doing these and creating partA and partB?

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