Min-Max Normalization

EasyMachine LearningMathArray

Description

Given an array of numbers, rescale every value to the [0, 1] range using min-max normalization: subtract the minimum and divide by (maximum - minimum). If all values are equal, return an array of zeros. Round each result to 4 decimal places.

Examples

Input:[2,4,6,8]
Output:[0,0.3333,0.6667,1]
Explanation:

The smallest value 2 maps to 0 and the largest 8 maps to 1, while every other value scales linearly between them by subtracting the minimum and dividing by the spread.

Input:[5,10,15]
Output:[0,0.5,1]
Explanation:

The smallest value 5 maps to 0 and the largest 15 maps to 1, while every other value scales linearly between them by subtracting the minimum and dividing by the spread.

Input:[1,2,3,4,5]
Output:[0,0.25,0.5,0.75,1]
Explanation:

The smallest value 1 maps to 0 and the largest 5 maps to 1, while every other value scales linearly between them by subtracting the minimum and dividing by the spread.

Constraints

  • 1 ≤ array length ≤ 10⁴
  • -10⁴ ≤ value ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.