wwwwwwking
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
I've ever been asked this question at Microsoft's interview
Please note: if our target is 8, and an array with {4, 4, 4, 4, 4, 4}, what should be output, and it is a good test case for your code..
Comment hidden because of low score. Click to expand.
0
of 0 vote
So, we can arrange all elements in order negative left and positive right like a step of quick sort, and then rearrange the left part and right part..
void swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
void ReArrange(int *src, int length)
{
int i = -1;
int j = 0;
for (j = 0; j < length; ++j)
{
if (src[j] < 0)
{
i++;
swap(src[i], src[j]);
}
}
j = ++i;
i = 0;
int posStart = j;
while (j < length && i < j && src[i] < 0)
{
swap(src[i], src[j]);
i += 2;
j++;
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
- wwwwwwking October 15, 2013