Min-Max Normalize

MediumStatisticsMathArray

Description

Given an array nums whose values are not all equal, scale every value to the range [0, 1] using (x - min) / (max - min). Round each result to 4 decimal places.

Examples

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

Rescaling so the minimum maps to 0 and the maximum to 1 gives [0,0.25,0.5,0.75,1].

Input:nums = [10,20,30]
Output:[0,0.5,1]
Explanation:

Rescaling so the minimum maps to 0 and the maximum to 1 gives [0,0.5,1].

Input:nums = [3,6,9]
Output:[0,0.5,1]
Explanation:

Rescaling so the minimum maps to 0 and the maximum to 1 gives [0,0.5,1].

Constraints

  • 2 ≤ nums.length ≤ 10⁴
  • max(nums) > min(nums)

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.