Is this possible to convert one array to another given array?

1.1k Views Asked by At

you are given two arrays having n elements ,

like for n=4,suppose

array1={1,2,3,4}

array2={2,1,4,5}

convert array 1 to array2 performing operation minimum number of time . Also state if transformation is not possible.I want a general condition for any two given arrays.

OPERATION- chose a ith element and decrement it by one and increment a all other elements by 1.

3

There are 3 best solutions below

16
On

Hint: what happens to the sum of the elements under your operation? What does that tell you about whether this is possible?

2
On

Just one: $$ \left\{1 + 1,2 - 1,3 + 1,4 + 1\right\} = \left\{2,1,4,5\right\} $$

Let's $\vec{e}_{1} \equiv \left\{1,0,0,\ldots\right\}$, $\vec{e}_{2} \equiv \left\{0,1,0,\ldots\right\}, \ldots$. $\vec{r}_{i}$ and $\vec{r}_{f}$ are the initial and final arrays, respectively. Then, after $n$ iterations, we'll have: $$ \vec{r}_{f} = n\left(\vec{r}_{i} + \sum_{\ell = 1}^{4}\vec{e}_{\ell}\right) - 2\sum_{\left\{j\right\}}\vec{e}_{j} $$ where $\left\{j\right\}$ is the set of $n$ indexes we choose along the iteration procedure. Since $\displaystyle{\vec{R} = \vec{r}_{i} + \sum_{\ell = 1}^{4}\vec{e}_{\ell}}$ is known, we have the condition: $$ \vec{r}_{f} = n\vec{R} - 2\sum_{\left\{j\right\}}\vec{e}_{j} $$ It shows that the result is independent of the order we choose $j$ in the $\left\{j\right\}$ set.

1
On

Your operation can be separated into 2 commutative operations:

  1. increase everything by 1
  2. decrease a chosen element by 2

First, figure out how many times that operation was applied by finding q such that q*(n-2) = the difference in the sum of elements. Obviously if q is not a natural number it's impossible.

Next, increase everything in array1 by q, let's call this array1'. If the difference between each element in array1' and array2 is a negative multiple of 2, it is possible. Otherwise it's not.