Empty Game
Practice
4.3 (7 votes)
Algorithms
Greedy algorithms
Basics of greedy algorithms
Problem
51% Success 1117 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of length \(N\). In one move, select an integer \(X\) and do one of the following operations:

  1. Choose one index \(i (1 <= i <= N)\) such that \(|A[i] - X| <= 1\) and remove \(A[i]\).
  2. Choose two indices \(i\) and \(j (1 <= i, j <= N, i ≠ j) \) such that \(|A[i] - X| <= 1\) and \(|A[j] - X| <= 1\) and remove \(A[i]\) and \(A[j]\) from the array.

Find the minimum number of moves required to make the array empty.

Input format:

  • The first line contains an integer \(T\) denoting the number of test cases.
  • The first line of each test case contains an integer \(N\) denoting the length of the array.
  • The second line of each test case contains \(N\) space-separated elements of the array \(A\).

Output format:

For each test case, print the minimum moves needed to make the array empty.

Constraints:

\(1 <= T <= 10\)

\(1 <= N <= 10^5\)

\(1 <= A[i] <= 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:20
19 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:20
34 votes
Tags:
AlgorithmsEasyGreedy Algorithms
Points:20
66 votes
Tags:
AlgorithmsGreedy AlgorithmsLOOP constructString Manipulation