Click4Ai

18.

Easy

Write a function normalize(arr) that normalizes an array to the range [0, 1] using min-max scaling.

Formula: normalized_value = (value - min) / (max - min)

Example:

Input: arr = [10, 20, 30, 40, 50]

Output: [0.0, 0.25, 0.5, 0.75, 1.0]

Constraints:

  • Array length: 2 <= len(arr) <= 1000
  • Array has at least two distinct values
  • Return a list of floats rounded to 4 decimal places
  • Test Cases

    Test Case 1
    Input: [10, 20, 30, 40, 50]
    Expected: [0.0, 0.25, 0.5, 0.75, 1.0]
    Test Case 2
    Input: [0, 100]
    Expected: [0.0, 1.0]
    Test Case 3
    Input: [1, 2, 3]
    Expected: [0.0, 0.5, 1.0]
    + 2 hidden test cases