www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Median of Two Sorted Arrays

Last update: 11/12/2016 8:11:00 AM

By Fatih KABAKCI

The problem is finding the median value of two sorted arrays. As everyone knows, median value is the middle element of a group of sorted data. If that collection of data has even number of elements, the middle value is the average of two middle values of that collection. For the main solution, we can create a new array to copy of the elements of both previous arrays. After we sort the array, the new median value of the final array is going to be found easily. Solution in Java has been shared below.

Problem #4 - Median of Two Sorted Arrays

Description: Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Difficulty: Hard

Solution in Java

package com.fatihkabakci.Hard.MedianTwoSortedArr;

import java.util.Arrays;

/**
 * 
 * @author fkabakci
 * Problem Description: There are two sorted arrays nums1 and nums2 of size m and n respectively.
 * Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
 * 
 * Examples:
 * 
 * nums1 = [1, 3]
 * nums2 = [2]
 * The median is 2.0
 * 
 * nums1 = [1, 2]
 * nums2 = [3, 4]
 * The median is (2 + 3) / 2 = 2.5
 * 
 * Solution:
 * 
 * 1. Create a new third array.
 * 2. Copy of the all elements of the first array to the third array.
 * 3. Do the second operation for the second array as well.
 * 4. Sort the new array.
 * 5. Find the median value of the new array.
 *
 */
public class MedianOfTwoSortedArrays {
	public double median(int[] nums) {
		int l = nums.length;
		if (l == 0)
			return 0;
		else if(l == 1)
			return nums[0];
		
		int h = l / 2;
		
		if (l % 2 == 0)
			return (nums[h - 1] + nums[h]) / 2.0;
		return nums[h];
	}
	
	public double findMedianSortedArrays(int[] nums1, int[] nums2) {
		int m = nums1.length, n = nums2.length, sum = m + n;
		int[] nums = new int[sum];
		// copy of 1st array
		for(int i = 0; i < m; i++) {
			nums[i] = nums1[i];
		}
		// copy of 2nd array
		for(int i = m; i < sum; i++) {
			nums[i] = nums2[i - m];
		}
		Arrays.sort(nums);
		return median(nums);
    }

	public static void main(String[] args) {
		int[] nums1 = {2};
		int[] nums2 = {};
		MedianOfTwoSortedArrays a = new MedianOfTwoSortedArrays();
		double r = a.findMedianSortedArrays(nums1, nums2);
		System.out.println(r);
	}
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.