|
|
std::copy( array1, array1 + arraysize, array2 );
|
|
|
|
|
|
[0]: 0 [1]: 0 [2]: +1 (element 2 (ie: 55) moved to element 3 and element 2 is set to 0) [3]: +1 (element 3 (ie: 33) moved to element 4) [4]: +2 (element 4 (ie: 5) moved to element 6 and element 5 is set to 0) [5]: +2 (element 5 (ie: 99) moved to element 7) [6]: -4 (element 6 (ie: 0) moved to element 2) [7]; -2 (element 7 (ie: 0) moved to element 5) |
int main() { int a[8] = {1, 3, 0, 55, 33, 0, 5, 99}; int b[8] = {1, 3, 55, 33, 5, 99, 0, 0}; int shift[8]; int index = 0,track = 0; int morethanone = 0; for (int i=0; i<8/*Number of array element*/; i++) { if(a[i] != b[i]) { for(int j=0;j<8;j++) { if(b[i] == a[j]) { shift[index++] = j - i; morethanone++; if(morethanone > 1) track++; if(track > 1) shift[index - morethanone] = j - i; } } if(morethanone > 1) { index = index - morethanone + 1; } morethanone = 0; } else shift[index++] = 0; } for(int i=0;i<8;i++) { cout << i << " "<<shift[i]<<endl; } getchar(); return 0; } |
HiteshVaghani1 |
|
|
|
|
|
|
|
|