Minimum increment decrement operations to make array non increasing - arrays

Given an array a, your task is to convert it into a non-increasing
form such that we can either increment or decrement the array value by
1 in minimum changes possible.
Examples :
Input : a[] = {3, 1, 2, 1} Output : 1 Explanation : We can convert the
array into 3 1 1 1 by changing 3rd element of array i.e. 2 into its
previous integer 1 in one step hence only one step is required.
Input : a[] = {3, 1, 5, 1} Output : 4 We need to decrease 5 to 1 to
make array sorted in non-increasing order.
Input : a[] = {1, 5, 5, 5} Output : 4 We need to increase 1 to 5.
This is the problem: https://www.geeksforgeeks.org/minimum-incrementdecrement-to-make-array-non-increasing/
The solution given is wrong. It is failing for this test case
{1, 2, 3, 4, 5}. The answer should be 6 with all array elements converted into 3. But the code gives 4 as the output. I don't think it is a greedy algorithm problem. What should be the approach.

The code given in gfg is correct, you must have missed the part where they have pushed the element twice, one time inside the if and other outside the if condition
P.S. I am also looking for the logic behind it.

Related

What do these constraints mean?

Can anyone help me understand this coding problem assignment?
I have an array of numbers, where each number appears twice except for one, and I need to identify which is the number that only appears once.
E.g.
const num_list = [8, 6, 3, 2, 4, 2, 3, 4, 5, 8, 7, 7, 6]
Answer: 5
The thing I'm confused about though is the constraints given for the problem are:
2 <= num_list[i] <= 100000
3 <= i <= 10,000
In particular the second constraint given - What does 'i' refer to here? Is it just stating the minimum number of elements that will be in the array (there are multiple test cases with different arrays as input)? Or does it mean that if I iterate over the array I can only start iterating from index 3 of the array onwards?
Thanks in advance

Unlimited changes to get an equal array

To clarify at the outset, this is not a homework, I was asked this question at a recent interview and drew a blank.
So I've the following array,
{1, 6, 3, 2, 9}
A change is step which increments any element by 1 and decrements any other element by 1. Thus 1 change could be like,
{2, 5, 3, 2, 9}
I'm allowed to make unlimited such changes, till I get maximum number of equal elements, thus the given array could become
{3, 3, 3, 3, 7} or {3, 4, 4, 4, 4}
Beyond this point more changes will not get any more elements equal. The question is thus, making unlimited changes, what is the maximum number of elements that can be made equal.
Thus the answer for the above array is 4. (Note there are two cases, in either case though the answer is 4)
Another example will be the array,
{1, 4, 1}
In which case we can make changes to get to
{2, 2, 2}
Thus the answer in this case is 3.
Can someone help me with an approach to get started. I'm still drawing a blank.
This seems to be a mathematical problem rather than a computer related one. Since every "Change" increments one element and decrements another, the sum of all the values in the array is constant.
This means that you can get all n elements of the array identical if and only if the sum of all elements can be evenly divided by n. Otherwise one of the elements must take another value to get n-1 equal elements.
By the way, your answers {3, 3, 3, 3, 7} and {3, 4, 4, 4, 4} (sum of 19) are not solutions to your previous state of {1, 6, 3, 2, 9} (sum of 21).

Sum values using Arrays and INDEX

I have the following sample sheet:
1/A B C D E F G H I J
2
3 Points 8 4 2 1
4
5 Values 1 2 3 4 4 3 1 2
I'm trying to sum the 'Points' based upon the array index from the 'Values'.
My expected result from this is: 30
Here is my formula:
{=SUM(INDEX($C$3:$F$3,1,C5:J5))}
For some reason though, this only returns the first value of the array, rather than the entire sum.
To clarify, the C# version would be something like:
var points = new int[] { 8, 4, 2, 1 };
var values = new int[] { 2, 4, 3, 1, 2, 4, 2 };
var result = (from v in values
select points[v - 1]).Sum(); // -1 as '4' will crash, but in Excel '4' is fine
Edit: Adding further clarifying example
Another example to clarify:
Points is the array. The 'values' represents the index of the array to sum.
The example above is the same as:
=SUM(8, 4, 2, 1, 1, 2, 8, 4)
INDEX will never take its row or column parameters from arrays and then perform multiple times within one array formula contained in one cell. For this OFFSET will be needed.
Either
{=SUM(N(OFFSET($C$3,,C5:J5-1)))}
as an array formula.
Or
=SUMPRODUCT(N(OFFSET($C$3,,C5:J5-1)))
as an implicit array formula without the need for [Ctrl]+[Shift]+[Enter].

Vectorizing Matlab replace array values from start to end

I have an array in which I want to replace values at a known set of indices with the value immediately preceding it. As an example, my array might be
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
and the indices of values to be replaced by previous values might be
y = [2, 3, 8];
I want this replacement to occur from left to right, or else start to finish. That is, the value at index 2 should be replaced by the value at index 1, before the value at index 3 is replaced by the value at index 2. The result using the arrays above should be
[1, 1, 1, 4, 5, 6, 7, 7, 9, 0]
However, if I use the obvious method to achieve this in Matlab, my result is
>> x(y) = x(y-1)
x =
1 1 2 4 5 6 7 7 9 0
Hopefully you can see that this operation was performed right to left and the value at index 3 was replaced by the value at index 2, then 2 was replaced by 1.
My question is this: Is there some way of achieving my desired result in a simple way, without brute force looping over the arrays or doing something time consuming like reversing the arrays around?
Well, practically this is a loop but the order is number of consecutive index elements
while ~isequal(x(y),x(y-1))
x(y)=x(y-1)
end
Using nancumsum you can achieve a fully vectorized version. Nevertheless, for most cases the solution karakfa provided is probably one to prefer. Only for extreme cases with long sequences in y this code is faster.
c1=[0,diff(y)==1];
c1(c1==0)=nan;
shift=nancumsum(c1,2,4);
y(~isnan(shift))=y(~isnan(shift))-shift(~isnan(shift));
x(y)=x(y-1)

interpolation of array inside a array

When I was trying to print a specific element in array, I have mistakenly typed the name of the same array inside [ ] as element and got some output as shown. I thought that it is taking the size of the array and printing that number of characters, which was proven to be wrong by the output.
#array = (0..10, 12);
print "#array[#array]";
prints
Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
0 1 2 3 4 5 6 7 8 9 10
I went one step ahead and edited code as shown below
print "#array[#array[#array]]";
output as: `0 1 2 3 4 5 6 7 8 9 10 0` Use of uninitialized value in join or string at
/home/VAR121/Program/Practise_Perl/Arrays.pl line 9.
Now I tried to put a number inside the second array instead of again giving array name as below.
print "#array[#array[1,2,3]]";
output as
1 2 3
But no warning message this time.
What it is trying to print? and What is the reason behind this behavior.
You're creating an array slice. Start out by understanding that your array contains elements 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12.
That's a total of 12 elements (there is no value 11, but at index 11 you are storing the value 12).
Now when you use this construct: #array[#array] you're taking a slice, and requesting the values stored in indices 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and 12. There is no element with an index of 12 in your array. The eleventh element has the value "12".
#array[] is array slice notation. For example:
my #array = (qw/apple banana cucumber date/)
#array[1,2] will return a list of (banana, cucumber) (index 1 and index 2 of #array).
Now, #array[#array] tries to take the values of the inner #array and use them as indexes of the outer #array. In your example, #array[#array] is equivalent to #array[0..10, 12]. Since one of the values of the inner array is 12, and the outer array has no index of 12, you get an undefined value warning.

Resources