Home > Mobile >  Get identical Array from increments to 2 values
Get identical Array from increments to 2 values

Time:01-29

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).

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.

CodePudding user response:

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.

  •  Tags:  
  • Related