Closer
Practice
4.3 (3 votes)
Basics of greedy algorithms
Algorithms
Greedy algorithms
Problem
85% Success 880 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of \(N\) integers. You can perform following operations :

  • Choose two distinct indices \(i\) and \(j\), increment \(A_i\) by \(1\) and decrement \(A_j\) by \(1\)

Find the minimum number of operations you should perform such that absolute difference between any two elements is at most \(K\) 

 

Input Format:

The First line contains two integers \(N\) and \(K\) .

The second line contains the \(N\) elements of array \(A\) .

 

Output Format

Print the minimum number of operations.

 

Constraints

\(1 \leq N \leq 10^5 \\ 1 \leq K \leq 10^9 \\ 1 \leq A_i \leq 10^5\)

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:30
9 votes
Tags:
DatastructuresGreedy AlgorithmsBasics of Greedy AlgorithmsAlgorithms
Points:30
20 votes
Tags:
ApprovedData StructuresGreedy AlgorithmsMediumOpenPriority queue
Points:30
4 votes
Tags:
Greedy algorithmAlgorithmsArraysGreedy AlgorithmsBasics of Greedy Algorithms