Interview Test - Regroup Array

Possible Duplicate:
Arranging Array Elements

In a given array of elements such as [a1, a2, a3, .. an, b1, b2, b3, ... bn, c1, c2, c3, ... cn] Write a program to merge them, for example [a1, b1, c1, a2, b2, c2, ... an, bn, cn]. We must do this in O (1) extra space.

Examples of test boxes:

Input #00:

{1,2,3,4,5,6,7,8,9,10,11,12}

Output #00:

{1,5,9,2,6,10,3,7,11,4,8,12}

Explanation:

Here as you can notice, the array is of the form
{a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4} 

EDIT: I got it in an Amazon placement test. Tried this for a long time. PLease provides the psuedo code. I tried to find a new position p for the second element e (the 1st is already in the correct position) by inserting e into p and repeating the same for the old element at position p. But it ends with a cycle. I tried to detect the loop and increased the starting position by 1. But even this does not work.

EDIT2:

#include <iostream>
using namespace std;

int pos(int i, int n) 
{
    if(i<n)  
     {
         return  3*i;

           }

       else if(i>=n && i<2*n)
       {

            return 3*(i-n) + 1;
            }
    else if(i>=2*n && i<3*n)
       {
            return 3*(i-2*n) + 2;
            }
return -1;
}
void printn(int* A, int n)
{
         for(int i=0;i<3*n;i++)  
             cout << A[i]<<";";

    cout << endl;
     }

void merge(int A[], int n)
{
 int j=1;    
 int k =-1;
 int oldAj = A[1];
 int count = 0;
 int temp;
 while(count<3*n-1){

 printn(A,n);
 k = pos(j,n);
 temp = A[k];
 A[k] = oldAj;
 oldAj = temp;
 j = k;
 count++;
 if(j==1) {j++;}
}

 }

int main()
{
    int A[21] = {1,4,7,10,13,16,19,2,5,8,11,14,17,20,3,6,9,12,15,18,21};
    merge(A,7);

    cin.get();}
+5
5

, , . , "", , O (1) - ...

, : a1 a2 a3 ... an b1 b2 b3 .. bn:

http://arxiv.org/PS_cache/arxiv/pdf/0805/0805.1598v1.pdf

+10

3 O (n ^ 2):

sa, sb, sc , , a, b c. d - .

:

  • sa, sb sc

  • , sa, sb sc

  • d

  • .

( "" ):

First iteration:
 copy to tmp: ., 2, 3, 4,  ., 6, 7, 8,   .,10,11,12
              1            5             9
 shift:       ., ., ., 2,  3, 4, 6, 7,   8,10,11,12
 copy to dst: 1, 5, 9, 2,  3, 4, 6, 7,   8,10,11,12

Second iteration:
copy to tmp: 1, 5, 9, .,   3, 4, ., 7,   8, .,11,12
                      2          6         10
shift:       1, 5, 9, .,   ., ., 3, 4,   7, 8,11,12
copy to dst: 1, 5, 9, 2,   6,10, 3, 4,   7, 8,11,12

Third iteration:
copy to tmp: 1, 5, 9, 2,   6,10, ., 4,   ., 8, .,12
                                 3       7    11 
shift:       1, 5, 9, 2,   6,10, ., .,   ., 4, 8,12
copy to dst: 1, 5, 9, 2,   6,10, 3,  7  11, 4, 8,12

( , :)))

#include <stdio.h>

#define N 4

int a[] = {1, 2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

void
rearrange ()
{
  int i;
  int d;
  int sa, sb, sc;
  int tmp [3];

  d = 0;
  sa = 0;
  sb = sa + N;
  sc = sb + N;

  while (sc < N*3)
    {
      /* Copy out.  */
      tmp [0] = a [sa];
      tmp [1] = a [sb];
      tmp [2] = a [sc];

      /* Shift  */
      for (i = sc; i > sb + 1; --i)
        a [i] = a [i - 1];

      for (i = sb + 1; i > sa + 2; --i)
        a [i] = a [i - 2];

      sa += 3;
      sb += 2;
      sc++;

      /* Copy in.  */
      a [d++] = tmp [0];
      a [d++] = tmp [1];
      a [d++] = tmp [2];
    }
}

int
main ()
{
  int i;
  rearrange ();

  for (i = 0; i < N*3; ++i)
    printf ("%d\n", a [i]);
  putchar ('\n');
  return 0;
}

.

+4

, .

, . :

  • . . . , , , . , ().
  • , . , 1, .

, . : , .


, O (1), N . O (1) , :

a1, b1, c1. . , : a1, b1, c1, , , ( :,, a2, a3,..., an, b2, b3,..., bn, c2, c3,..., cn) a1, b1, c1 . 3 , a2, b2, c2 ..

Edit:
. 3*n. N. , , O(n). O(1), n * O(n) = O(n^2). , , @yi_H, .

+3

O (n), O (n ^ 2) , , , , #, be is buggy, , :

        int[] a = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
        int m = a.Length / 3;
        int firstB = a[m];

        for (int i = m-1; i > 0; i--)
        {
            int second = a[3 * m - 3];
            int third = a[3 * m - 2];
            //a[i + 2 * m] = a[i +2 * m];
            a[3 * m - 2] = a[2 * m - 1];
            a[3 * m - 3] = a[m - 1];
            for (int j = m - 1; j < 2 * m - 1; j++)
            {
                a[j] = a[j + 1];
            }
            for (int j = 2 * m - 2; j < 3 * m - 3; j++)
            {
                a[j] = a[j + 2];
            }
            a[3 * m - 5] = second;
            a[3 * m - 4] = third;
            m--;
        }
        a[1] = firstB;
+1

x * y numbers:

a_11, a_12, ..., a_1x,
a_21, a_22, ..., a_2x,
...
a_y1, a_y2, ..., a_yx

that number a_ijhas an index i*x + jin the array;

after your program, the new index will be

j * y + i

in his interview

{a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4} 

x is 4, and y is 3,

therefore with the index `` n ''

i = (n - (n % 4)) / 4;
j = n % 4;

Now you can calculate the new index with i, j, x, y.

Good luck.

-2
source

All Articles