# Coding Interview Question: Median of Arrays

## Question

Find the median of two sorted arrays.

eg.

Once you think that you’ve solved the problem, click below to see the solution.

As always, remember that practicing coding interview questions is as much about how you practice as the question itself. Make sure that you give the question a solid go before skipping to the solution. Ideally if you have time, write out the solution first by hand and then only type it into your computer to verify your work once you've verified it manually. To learn more about how to practice, check out this blog post.

## Solution

How was that problem? You can check out the solution in the video below.

Here is the source code for the solution shown in the video (Github):

### Nail your coding interview and get the job you deserve

Sign up for my weekly emails and I’ll show you how to become an interview rockstar, solve brutal coding problems, and get the dev job you deserve.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

### Nail your coding interview and get the job you deserve

Sign up for my weekly emails and I’ll show you how to become an interview rockstar, solve brutal coding problems, and get the dev job you deserve.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit
• joel

Given an array containing integers, zero is considered an invalid number and rest all other numbers are valid. If two nearest valid numbers are equal then double the value of the first one and make the second number as 0.At last move all the valid numbers on the left.

for(int i=0;i>a[i];
}
int i=0,j=1;
while(i<n&&j<n)
{
if(a[j]==0)
j++;
else if(a[j]==a[i])
{
a[i]=2*a[i];
a[j]=0;
}
else
{
j++;
i=j-1;
}
}
int count = 0; // Count of non-zero elements

// Traverse the array. If element encountered is non-
// zero, then replace the element at index 'count'
// with this element
for (int i = 0; i < n; i++)
if (a[i] != 0)
a[count++] = a[i]; // here count is
// incremented

// Now all non-zero elements have been shifted to
// front and 'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
a[count++] = 0;
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}

Here in the else loop why cant we write i++ instead of i=j-1;