Help needed in LeetCode problem (Median of Two Sorted Arrays)

I’m a beginner and not sure about the complexities. So, can someone please tell me the time and space complexity of my code (I think the time complexity is O(1) as there are no loops, correct me if I’m wrong). And, is my approach good?

Problem Link: https://leetcode.com/problems/median-of-two-sorted-arrays/
My Solution:

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int fal = nums1.length;
        int sal = nums2.length;
        int[] result = new int[fal + sal];
        System.arraycopy(nums1, 0, result, 0, fal);  
        System.arraycopy(nums2, 0, result, fal, sal); 
        Arrays.sort(result);
        if(result.length%2==0){
            int first=result.length/2;
            double answer=(result[first-1]+result[first]);
            return answer/2;
        }
        int first=result.length/2;
        return result[first];
               
    }
}

The time complexity is O((fal + sal) *log(fal + sal)) and space complexity is O( fal + sal )

1 Like