Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:55:08+00:00 2026-05-23T10:55:08+00:00

I am stuck in one interview question.. The question is, *given two arrays A

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:55:08+00:00Added an answer on May 23, 2026 at 10:55 am

    I suggest the following algorithm:
    1. Initially consider all C[ i ] as empty nests.
    2. For each i where B[ i ] = 0 we put C[ i ] = A[ i ]
    3. Go through array from left to right, and for each i where B[ i ] = -1 put
    C[ j ] = A[ i ], where 0 <= j < i is the smallest index for which C[ j ] is still empty. If no such index exists, the answer is impossible.
    4. Go through array from right to left, and for each i where B[ i ] = 1 put
    C[ j ] = A[ i ], where i < j < n is the greatest index for which C[ 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:

    A: [ 1, 2, 3 ]
    B: [ 1, 1,-1 ]
    

    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 ] to C[ 0 ] or to C[ 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 ] and A[ 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:

    A: [ 1, 2, 3 ]
    B: [ 1, -1, -1 ]
    

    Let’s go through the algorithm step by step:
    1. C: [ _, _, _ ]
    2. Empty, because no 0-s in B
    3. Putting A[ 1 ] and A[ 2 ] to leftmost empty positions:

    C: [ 2, 3, _ ]
    

    4. Putting A[ 0 ] to the rightmost free (in this example the only one) free position:

    C: [ 2, 3, 1 ]
    

    Which is the answer.

    Source code:

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    
    vector< int > a;
    vector< int > b;
    vector< int > c;
    int n;
    
    bool solve ()
    {
        int i;
        for( i = 0; i < n; ++i )
            c[ i ] = -1;
        for( i = 0; i < n; ++i )
            if( b[ i ] == 0 )
                c[ i ] = a[ i ];
        int leftmost = 0;
        for( i = 0; i < n; ++i )
            if( b[ i ] == -1 )
            {
                for( ; leftmost < i && c[ leftmost ] != -1; ++leftmost ); // finding the leftmost free cell
                if( leftmost >= i )
                    return false; // not found
                c[ leftmost++ ] = a[ i ];
            }
        int rightmost = n - 1;
        for( i = n - 1; i >= 0; --i )
            if( b[ i ] == 1 )
            {
                for( ; rightmost > i && c[ rightmost ] != -1; --rightmost ); // finding the rightmost free cell
                if( rightmost <= i )
                    return false; // not found;
                c[ rightmost-- ] = a[ i ];
            }
        return true;
    }
    
    
    int main ()
    {
        cin >> n;
        a.resize(n);
        b.resize(n);
        c.resize(n);
        int i;
        for( i = 0; i < n; ++i )
            cin >> a[ i ];
        for( i = 0; i < n; ++i )
            cin >> b[ i ];
        if( !solve() )
            cout << "Impossible";
        else
            for( i = 0; i < n; ++i )
                cout << c[ i ] << ' ';
        cout << endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was stuck at one question in an interview. Given two threads, and each
I am just stuck in one simple jQuery question. I am trying to each
I am reading one of the books and stuck at one particular question. Definition
I'm preparing for a job interview. I was stuck at one of the binary
I'm kinda stuck with this one so I hoped someone could help me. I
I'm reading the not so complete pytz documentation and I'm stuck on understand one
I'm stuck for ideas on this one. I'm working on a CMS that uses
I am stuck in between of a problem where only one pass of regular
This is probably a simple one to answer, but I'm stuck, so here goes.
I'm kind of stuck with one - presumably simple to resolve problem. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.