-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the code with understanding Solution
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Hard/4. Median of Two Sorted Arrays/New approach[add two array]/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.Arrays; | ||
public class Solution{ | ||
public double findMedianSortedArrays(int[] nums1, int[] nums2) { | ||
// Get the lengths of input arrays nums1 and nums2 | ||
int a = nums1.length; | ||
int b = nums2.length; | ||
|
||
// Calculate the total length of the merged array | ||
int c = a + b; | ||
|
||
// Create a new integer array to hold the merged elements | ||
int[] arr = new int[c]; | ||
|
||
// Copy elements from nums1 into arr | ||
for (int i = 0; i < a; i++) { | ||
arr[i] = nums1[i]; | ||
} | ||
|
||
// Copy elements from nums2 into arr, starting from position a | ||
for (int i = 0; i < b; i++) { | ||
arr[a + i] = nums2[i]; | ||
} | ||
|
||
// Sort the merged array in ascending order | ||
Arrays.sort(arr); | ||
|
||
// Check if the length of the merged array is even or odd | ||
if (c % 2 == 0) { | ||
// Calculate the indices of the two middle elements | ||
int mid1 = (c - 1) / 2; | ||
int mid2 = mid1 + 1; | ||
|
||
// Calculate the median as the average of the two middle elements | ||
return (double) (arr[mid1] + arr[mid2]) / 2; | ||
} else { | ||
// Calculate the index of the middle element | ||
int mid = (c - 1) / 2; | ||
|
||
// Return the middle element as the median | ||
return arr[mid]; | ||
} | ||
} | ||
} |