Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

Data Structures and Algorithms Questions

13 / 13

Smallest Range

Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once).

After this process, we have some array B.

Return the smallest possible difference between the maximum value of B and the minimum value of B.

Example:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]

Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]

Constraints:

  • 1 <= len(A)<= 10000
  • 0 <= A[i] <= 10000
  • 0 <= K <= 10000
INSTRUCTIONS

  1. Write your code inside a function named smallest_range
  2. There are no partial marks for the question.
  3. Your function must return the output, it should not print the output.
  4. To execute a block on the right-side coding panel, please press 'shift'+ 'enter' inside the block.
  5. Your code should work for all permitted possible values(check Constraints) of A and K

Complete the below code in the right side coding panel

def smallest_range(A: list, K: int) -> int:
    # your code goes here
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...