This problem can be solved by modified Dutch National Flag problem.
Instead of '0' we have -ve numbers
Instead of '1' we have 0's(zero).
and for 2 we have +ve Numbers.
------------------------------------------
So modified algo will be

The question does not clearly mention whether all the negatives are the same or whether all the positives are the same. If they are diff say we have -2, -5, -1,etc. and 4,8,7,etc then we cannot use the DNF algo as it is.

It seems due to indentation people are getting confused.. As I take the array from user.. I am using 2 for loops but they are not nested. :)
I only parse the array once... indicated by second for loop.

Hi,
Would you please let me know if the below sequence of input works for your logic???
1,2,5,7,-1,-2,-5,0,0,0
If not, I have still more sequences to share with you.
If possible, explain the logic behind your code.

Whenever you come across 0 you are just skipping that instead of putting it in some place. It is not working in making sure 0s are in the middle - which is what supposed to make this challenging. I can do it in two passes though, first bring only negative to one side and then take only remainign array and bring 0s to one side.

Read his code correctly what he is doing is allright..He is just swapping all the positive number towards the end all negative number towrads the begining..In that sense he is swapping even positive with positive, that can be avoided.

I try to make it more logical..Consider the case when there are only positive and negative numbers and no zero..In that case we use two pointer from two ends and swap positive with negative..Here what we have to do is, swap positive with negative as well as zero..Swap negative with positive but swap with zero too..

/*
* This has a runtime of O(n).
* Places all the negative no on the left side.
* All the positive no on the right side.
* And all the zeroes in the middle.
*/
public int[] arrangeArray(int[] arr) {
int[] res = new int[arr.length];
int left = 0;
int right = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
res[left] = arr[i];
left++;
} else if (arr[i] > 0) {
res[right] = arr[i];
right--;
}
}
return res;
}

You don't have to do 2 passes. In first pass place the negatives on the left, the positive on the right, and the remaining must be zeroes. As when you initialize an array the value gets initialised with zero so you don't even need to set anything now. Just 1 pass required.

Here is my one pass algorithm:
let ptr_left points to the left end of the array a[] and ptr_right points to the right end
let nzero_left is the counts of zero from left sides; nzero_right for right side

nzero_left=0; nzero_right=0;
while (ptr_left<=ptr_right)
{{

if (a[ptr_left]==positive and
a[ptr_right]==negative)
swap a[ptr_left] and a[ptr_right]

we can just use partition algorithm of quick sort to partition this array, just use 0 as the pivot element and hence all negative numbers will come to its left and positive numbers will come to its right
you can understand partition algorithm at
youtube.com/watch?v=rZdV64uq5KM

Sorting is nlog(n), but there's a linear solution. Not sure if this is optimal, but here's a two pass algorithm that first swaps positive/zero numbers from left to right with negative numbers, and then a similar pass for zeros:

You don't have to do 2 passes. In first pass place the negatives on the left, the positive on the right, and the remaining must be zeroes. As when you initialize an array the value gets initialised with zero so you don't even need to set anything now. Just 1 pass required.

void Lineararrange(int X[]){
this.X=X;
int i=0,j=0;
int m=X.length-1,n;
while(i<X.length){
//Increase i as long as found negative
while(X[i]<0 && i<X.length)i++;
//Set j=i+1
j=i+1;//X[j] is either 0 or positive
//increase j as long as another negative is not found
while(j<X.length && X[j]>=0){j++;}
//if j=length finish
if(j>=X.length){
break;
}
//else swap with X[i],increase i
else{
int temp=X[i];
X[i]=X[j];
X[j]=temp;
}
}
//Now we have all negative to the left of i
while(m>0){
//Decrease m as long as found positive
while(X[m]>0 && m>0)m--;
//Set n=m-1
n=m-1;//X[n] is either 0 or negative
//decrease n as long as another positive is not found
while(n>=0 && X[n]<=0 ){n--;}
if(n<=0){
break;
}
//if n=0 finish
//else swap with X[m],decrease m
else{
int temp=X[m];
X[m]=X[n];
X[n]=temp;
}
}
//Now we have all positive to the right of m
output();
}
public void output(){
for(int i=0;i<X.length;i++){
System.out.print(X[i]+",");
}
}

take zero as pivot element. nd traverse once.compare all elements with zero. anythin less than zero will come to the left and anythin greater than zero will come to the right of zero.. so basically partition around zero.

well, since it doesn't say we cannot have an extra array. I will just create an extra array with the same size and initialize to 0.
Then I will scan through the given array, if it's less than 0, then put it at the front (say, a counter that starts at 0). If it's greater than 0, then put it at the end (have a counter that starts at n-1).
It should be done after the first pass since it's initialized to 0's already, so the middle ones are all 0's.

1.get two pointer for head and tail,
2.trace the array from the beginning,
3.if the pointer see a negative number swap with head and if is positive swap with tail
4. move to next item until hit the tail

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.