I am stuck in one interview question.. The question is,
*given two arrays A and B. A has integers unsorted. B has the same
length as A and its values are in the
set {-1,0,1}you have to return an array C with the
following processing on A.if B[i] has 0 then C[i] must have A[i]
if B[i] has -1 then A[i] must be in C
within the sub array C[0] – C[i-1] ie.
left subarray
if B[i] has 1 then A[i]
must be in C within the sub array
C[i+1] – C[length(A)] ie right
subarray.if no such solution exists then
printf(“no solution”);*
I applied following logics:-
int indMinus1 = n-1;
int indPlus1 = 0;
//while(indPlus1 < n && indMinus1 > 0)
while(indPlus1 < indMinus1)
{
while(b[indMinus1] != -1) {
if(b[indMinus1] == 0)
c[indMinus1] = a[indMinus1];
indMinus1--;
}
while(b[indPlus1] != +1) {
if(b[indPlus1] == 0)
c[indPlus1] = a[indPlus1];
indPlus1++;
}
c[indMinus1] = a[indPlus1];
c[indPlus1] = a[indMinus1];
b[indMinus1] = 0;
b[indPlus1] = 0;
indMinus1--;
indPlus1++;
}
But this will not going to work,, for some cases like {1,2,3} >> {1,-1,-1}… One output is possible i.e. {2,3,1};
Please help…. does their any algorithm technique available for this problem?
Correct Solution Code
int arrange(int a[], int b[], int c[], int n)
{
for (int i = 0; i < n; ++i) {
if(b[i] == 0)
c[i] = a[i];
}
int ci = 0;
for (int i = 0; i < n; ++i) {
if(b[i] == -1) {
while(c[ci] != 0 && ci < i)
ci ++;
if(c[ci] != 0 || ci >= i)
return -1;
c[ci] = a[i];
ci++;
}
}
for (int i = 0; i < n; ++i) {
if(b[i] == 1) {
while(c[ci] != 0 && ci < n)
ci ++;
if(c[ci] != 0 || ci <= i)
return -1;
c[ci] = a[i];
ci++;
}
}
return 0;
}
I suggest the following algorithm:
1. Initially consider all
C[ i ]as empty nests.2. For each i where
B[ i ] = 0we putC[ i ] = A[ i ]3. Go through array from left to right, and for each
iwhereB[ i ] = -1putC[ j ] = A[ i ], where0 <= j < iis the smallest index for whichC[ j ]is still empty. If no such index exists, the answer is impossible.4. Go through array from right to left, and for each
iwhereB[ i ] = 1putC[ j ] = A[ i ], wherei < j < nis the greatest index for whichC[ j ]is still empty. If no such index exists, the answer is impossible.Why do we put A[ i ] to the leftmost position in step 2 ? Well, we know that we must put it to some position j < i. On the other hand, putting it leftmost will increase our changes to not get stucked in step 3. See this example for illustration:
Initially C is empty:
C:[ _, _, _ ]We have no 0-s, so let’s pass to step 2.
We have to choose whether to place element
A[ 2 ]toC[ 0 ]or toC[ 1 ].If we place it not leftmost, we will get the following situation:
C: [ _, 3, _ ]And… Oops, we are unable to arrange elements
A[ 0 ]andA[ 1 ]due to insufficient place 🙁But, if we put A[ 2 ] leftmost, we will get
C: [ 3, _, _ ],And it is pretty possible to finish the algorithm with
C: [ 3, 1, 2 ]🙂Complexity:
What we do is pass three times along the array, so the complexity is
O(3n) = O(n)– linear.Further example:
Let’s go through the algorithm step by step:
1.
C: [ _, _, _ ]2. Empty, because no 0-s in
B3. Putting
A[ 1 ]andA[ 2 ]to leftmost empty positions:4. Putting
A[ 0 ]to the rightmost free (in this example the only one) free position:Which is the answer.
Source code: