Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get identical Array from increments to 2 values

I want to find the minimum number to be subtracted to get from an array like this

[8, 10, 5]

to an array like this

[3, 3, 3]

I am only allowed to change the values of TWO indexes (in this case either index 0 and 1 OR index 1 and 2). I am only allowed to subtract values and have to subtract the same value over both indexes(ex. If I wanted to subtract 5 I’d have to do it on two indexes, either index 0 and 1 or index 2 and 3).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit: Clarification #1: There is no "base value". Just that all indexes are identical through the lowest number of subtraction values (not operations, but values subtracted.)

Clarification #2: At any given time, I am only allowed to subtract the same value from 2 indexes, and only 2

I am not sure of how to tackle this problem, any smallest hints and advice would be greatly appreciated.

>Solution :

If you want to make all the values identical, that means your lowest and highest value should be equal.
for example, in your array

arr[] = {8,5,10}; 

You need to first sort it.

 arr[] = {5,8,10};

Now, you see that the difference between the highest and lowest is 5,
therefore you subtract 5 from the 2 maximum elements

 arr[] = {5,3,5};

then again you sort it

arr[] = {3,5,5};

now the difference between maximum and minimum 2, therefore you subtract it from the maximum 2 elements present in your array .
and your array becomes this

  arr[] = {3,3,3};

The idea is to bring the maximum number closest to the smallest number and that happens if you subtract the difference between them from the maximum number.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading