Maximum Operation Count
Practice
2.7 (3 votes)
Greedy algorithm
Greedy algorithms
Algorithms
Basics of greedy algorithms
Datastructures
Problem
89% Success 1543 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

We have given an array \(nums\) of integers of size \(N\) and a positive integer \(K\). We can perform as many operations on the array till it has at least two elements. 
The operation is as follows: you can pick any two numbers from the array and remove both if the difference between them is at least \(K\).
Find the maximum number of times we can perform this operation on the array.

Input format

  • The first line contains two space-separated integers \(N\) and \(K\).
  • The second line contains \(N\) space-separated integers denoting array \(nums\).

Output format

  • Print the maximum possible number of operations.

Constraints

  • \(1 \leq N \leq 10^6 \)
  • \( 1 \leq K, nums[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:30
2 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:30
5 votes
Tags:
Bit ManipulationAlgorithmsGreedy AlgorithmsBasics of Greedy Algorithms
Points:30
2 votes
Tags:
AlgorithmsGreedy AlgorithmsBasics of Greedy AlgorithmsGreedy algorithm