Operation with X
Practice
4 (21 votes)
Algorithms
Sorting
Greedy algorithms
Basics of greedy algorithms
Problem
92% Success 3541 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

We have an array \(Arr \) of \(N\) integer points and an integer \(X\). At each element \(Arr[i]\), we will either add \(X\) or subtract \(X\). We have to operate in such a way, that the absolute difference between the maximum value and the minimum value in the array is minimal.

Find the minimum absolute difference between the maximum value obtained by the operation.

Input format

  • The first line contains two space-separated integers \(N\) and \(X\), where \(N\) denotes the size of the array \(Arr \).
  • The second line contains \(N\) space-separated integers denoting array \(Arr \).

Output format

Print the minimum value obtained by the operation.

Constraints

  • \(1 \leq N \leq 2 \times 10^5\)
  • \(0 \leq X \leq10^6\)
  • \(-10^6 \leq Arr[i] \leq 10^6\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
6 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:30
58 votes
Tags:
AlgorithmsEasyMonthly-ContestReady
Points:50
11 votes
Tags:
AlgorithmsGreedy Algorithms