Maintain two indexes to traverse.Initialize first index left as 0 and second index right as n-1.
Do following while left<right:
1. keep inc index left while there are -ve no at it
2. keep dec index right while there are +ve no at it
3. if left<right then exchange A[left] and A[right]
Void Rearrange(int A[], int n)
{
int left=0,right=n-1;
while(let<right)
{
if(A[left]<0 && left<right)
left++;
elseif(A[right]>0 && left<right)
right--;
else
{
if(left<right)
{
int temp;
temp=A[left];
A[left]=A[right];
A[right]=temp;
left++;
right--;
}
}
}
}
Time Complexity=O(n)
Space Complexity=O(1)
Note:Please take help from Algorithm book by Narasimha Karumanchi