given a sorted array of distinct integers, what is the minimum number of steps required to make the integers contiguous? Here the condition is that: in a step , only one element can be changed and can be either increased or decreased by 1 . For example, if we have 2,4,5,6 then '2' can be made '3' thus making the elements contiguous(3,4,5,6) .Hence the minimum steps here is 1 . Similarly for the array: 2,4,5,8:

Step 1: '2' can be made '3'

Step 2: '8' can be made '7'

Step 3: '7' can be made '6'

Thus the sequence now is 3,4,5,6 and the number of steps is 3.

I tried as follows but am not sure if its correct?

//n is the number of elements in array a
int count=a[n-1]-a[0]-1;
for(i=1;i<=n-2;i++)
{
count--;
}
printf("%d\n",count);

7 Answers
7

The intuitive guess is that the "center" of the optimal sequence will be the arithmetic average, but this is not the case. Let's find the correct solution with some vector math:

Part 1: Assuming the first number is to be left alone (we'll deal with this assumption later), calculate the differences, so 1 12 3 14 5 16-1 2 3 4 5 6 would yield 0 -10 0 -10 0 -10.

sidenote: Notice that a "contiguous" array by your implied definition would be an increasing arithmetic sequence with difference 1. (Note that there are other reasonable interpretations of your question: some people may consider 5 4 3 2 1 to be contiguous, or 5 3 1 to be contiguous, or 1 2 3 2 3 to be contiguous. You also did not specify if negative numbers should be treated any differently.)

theorem: The contiguous numbers must lie between the minimum and maximum number. [proof left to reader]

Part 2: Now returning to our example, assuming we took the 30 steps (sum(abs(0 -10 0 -10 0 -10))=30) required to turn 1 12 3 14 5 16 into 1 2 3 4 5 6. This is one correct answer. But 0 -10 0 -10 0 -10+c is also an answer which yields an arithmetic sequence of difference 1, for any constant c. In order to minimize the number of "steps", we must pick an appropriate c. In this case, each time we increase or decrease c, we increase the number of steps by N=6 (the length of the vector). So for example if we wanted to turn our original sequence 1 12 3 14 5 16 into 3 4 5 6 7 8 (c=2), then the differences would have been 2 -8 2 -8 2 -8, and sum(abs(2 -8 2 -8 2 -8))=30.

Now this is very clear if you could picture it visually, but it's sort of hard to type out in text. First we took our difference vector. Imagine you drew it like so:

4|
3| *
2| * |
1| | | *
0+--+--+--+--+--*
-1| |
-2| *

We are free to "shift" this vector up and down by adding or subtracting 1 from everything. (This is equivalent to finding c.) We wish to find the shift which minimizes the number of | you see (the area between the curve and the x-axis). This is NOT the average (that would be minimizing the standard deviation or RMS error, not the absolute error). To find the minimizing c, let's think of this as a function and consider its derivative. If the differences are all far away from the x-axis (we're trying to make 101 112 103 114 105 116), it makes sense to just not add this extra stuff, so we shift the function down towards the x-axis. Each time we decrease c, we improve the solution by 6. Now suppose that one of the *s passes the x axis. Each time we decrease c, we improve the solution by 5-1=4 (we save 5 steps of work, but have to do 1 extra step of work for the * below the x-axis). Eventually when HALF the *s are past the x-axis, we can NO LONGER IMPROVE THE SOLUTION (derivative: 3-3=0). (In fact soon we begin to make the solution worse, and can never make it better again. Not only have we found the minimum of this function, but we can see it is a global minimum.)

Thus the solution is as follows: Pretend the first number is in place. Calculate the vector of differences. Minimize the sum of the absolute value of this vector; do this by finding the median OF THE DIFFERENCES and subtracting that off from the differences to obtain an improved differences-vector. The sum of the absolute value of the "improved" vector is your answer. This is O(N) The solutions of equal optimality will (as per the above) always be "adjacent". A unique solution exists only if there are an odd number of numbers; otherwise if there are an even number of numbers, AND the median-of-differences is not an integer, the equally-optimal solutions will have difference-vectors with corrective factors of any number between the two medians.

*(we obtain this by repeating step 2 with our new target, or by adding 5 to each number of the previous difference... but since you only care about the sum, we'd just add 8*5 (vector length times correct factor) to the previously calculated sum)

Alternatively, we could have also taken -6 or -7 to be our correction factor. Let's say we took -7...

then the new goal would have been 2 3 4 5 6 7 8 9+7=9 10 11 12 13 14 15 16, and the new differences would have been 7 7 7 2 1 0 0 -84

this would have meant we'd need to do 7+7+7+2+1+0+0+84=108 steps, the same as above

If you simulate this yourself, can see the number of steps becomes >108 as we take offsets further away from the range [-5,-7].

It turns out that for arrays of distinct integers, this is equivalent to a simpler solution: picking one of the (up to 2) medians, assuming it doesn't move, and moving other numbers accordingly. This simpler method often gives incorrect answers if you have any duplicates, but the OP didn't ask that, so that would be a simpler and more elegant solution. Additionally we can use the proof I've given in this solution to justify the "assume the median doesn't move" solution as follows: the corrective factor will always be in the center of the array (i.e. the median of the differences will be from the median of the numbers). Thus any restriction which also guarantees this can be used to create variations of this brainteaser.

1. Note that the question already states that the input is sorted. While this doesn't affect the correctness of your answer, it does mean your example data doesn't conform. 2. Can you elaborate on why the arithmetic mean will minimize the area under the graph? That's not clear.
–
WeebleFeb 19 '12 at 13:15

@Weeble: I've provided an example where the input is sorted. Also nice catch! Indeed I had a bug in the proof, so the arithmetic mean didn't minimize the area under the graph; the new answer proves that the median of the differences with 1,2,3,4,... minimizes this, and justifies it. You inserted your comment right as I temporarily deleted my answer to fix that very issue. =)
–
ninjageckoFeb 19 '12 at 14:15

Looks like we were both thinking along the same lines. I just posted an answer that I think is equivalent to your corrected answer.
–
WeebleFeb 19 '12 at 14:20

Get one of the medians of all the numbers. As the numbers are already sorted, this shouldn't be a big deal. Assume that median does not move. Then compute the total cost of moving all the numbers accordingly. This should give the answer.

At first I thought this answer was wrong, then I thought it was right, now unfortunately I think it's wrong. In my answer, I believe I proved that we must take the differences between the input and 1 2 3 4 5 ..., and look at the median of that sequence (rather than the input sequence). For example if we had 0 1 2 3 3, your answer would claim that 3+2+1+0+0= 6 is the answer, and I'd claim 1 1 1 1 0 when we shift it properly becomes 0 0 0 0 1, with a cost of 1, is the answer. Please tell me if I'm mistaken.
–
ninjageckoFeb 19 '12 at 13:25

If we had 0 1 2 3 3 then the median is 2. We dont have to move 0, 1 and the second last 3. Only movement will be for last number 3. I should have been specific about this: Movement of a number should take into account the numbers between it and the median.
–
Tejas PatilFeb 19 '12 at 14:49

@TejasP: Sorry I messed up my example. If you consider the example I use in my question of [2, 3, 4, 10, 14, 14, 15, 100], your answer yields 124, when the answer should be 108 I think? Assuming you actually mean what @fishinear claims of sum{i}(|median + (i-n/2) - a[i]|), that happens to yield the same result in my case, but only if you consider the average of the two medians to be the median (using either one or the other fails). I believe it fails in the case of 2 61 63 100 100 100 100 where it claims 171.5 steps (half a step?), while my method claims 165 steps.
–
ninjageckoFeb 21 '12 at 15:12

@ninjagecko As you may have noticed, the OP stated a "sorted array of distinct integers". None of your examples show distinct integers. But I agree that your algorithm is more general in the sense that it also takes duplicates into account. BTW, the algorithm I showed comes up with 174 steps for your last example. It can never produce half steps, because everything is integers.
–
fishinearFeb 21 '12 at 19:44

Write a function that returns the differences of an element to the previous and to the next element, i.e. (xn – xn–1, xn+1 – xn).

If the difference to the previous element is > 1, you would have to increase all previous elements by xn – xn–1 – 1. That is, the number of necessary steps would increase by the number of previous elements × (xn – xn–1 – 1). Let's call this number a.

If the difference to the next element is >1, you would have to decrease all subsequent elements by xn+1 – xn – 1. That is, the number of necessary steps would increase by the number of subsequent elements × (xn+1 – xn – 1). Let's call this number b.

If a < b, then increase all previous elements until they are contiguous to the current element. If a > b, then decrease all subsequent elements until they are contiguous to the current element. If a = b, it doesn't matter which of these two actions is chosen.

Add up the number of steps taken in the previous step (by increasing the total number of necessary steps by either a or b), and repeat until all elements are contiguous.

Because the input array is already ordered and distinct, it is strictly increasing. Because of this, it can be shown that the differences will always be non-increasing.

If we change the target by increasing it by 1, the cost will change. Each position in which the difference is currently positive or zero will incur an increase in cost by 1. Each positive in which the difference is currently negative will yield a decrease in cost by 1:

Conversely, if we decrease the target by 1, each position in which the difference is currently positive will yield a decrease in cost by 1, while each position in which the difference is zero or negative will incur an increase in cost by 1:

In order to find the optimal values for the target array, we must find a target such that any change (increment or decrement) will not decrease the cost. Note that an increment of the target can only decrease the cost when there are more positions with negative difference than there are with zero or positive difference. A decrement can only decrease the cost when there are more positions with a positive difference than with a zero or negative difference.

Here are some example distributions of difference signs. Remember that the differences array is non-increasing, so positives always have to be first and negatives last:

Observe that if one of the central elements (marked C) is zero, the target must be optimal. In such a circumstance, at best any increment or decrement will not change the cost, but it may increase it. This result is important, because it gives us a trivial solution. We pick a target such that a[n/2] remains unchanged. There may be other possible targets that yield the same cost, but there are definitely none that are better. Here's the original code modified to calculate this cost:

You can not do it by iterating once on the array, that's for sure.
You need first to check the difference between each two numbers, for example:2,7,8,9 can be 2,3,4,5 with 18 steps or 6,7,8,9 with 4 steps.
Create a new array with the difference like so: for 2,7,8,9 it wiil be 4,1,1. Now you can decide whether to increase or decrease the first number.

Brute force approach O(N*M)

If one draws a line through each point in the array a then y0 is a value where each line starts at index 0. Then the answer is the minimum among number of steps reqired to get from a to every line that starts at y0, in Python: