J. Smallest Difference

Codeforces
IDCF10185J
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You are given an array a consists of n elements, find the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1. The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases. The first line of each test case consist of an integer n (2 ≤ n ≤ 104), in which n is size of the array a The a line follow containing n elements a1, a2, ..., an (1 ≤ ai ≤ 104), giving the array a. For each test case, print a single line containing the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1. ## Input The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.The first line of each test case consist of an integer n (2 ≤ n ≤ 104), in which n is size of the array aThe a line follow containing n elements a1, a2, ..., an (1 ≤ ai ≤ 104), giving the array a. ## Output For each test case, print a single line containing the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1. [samples]
**Definitions** Let $ n \in \mathbb{Z} $ be the number of cadets. Let $ S = (s_1, s_2, \dots, s_n) $ be the sequence of scores, where $ s_i $ is the score of the $ i $-th cadet (in finishing order). **Constraints** 1. $ 1 \leq n \leq 10^5 $ 2. $ 0 \leq s_i \leq 10^6 $ for all $ i \in \{1, \dots, n\} $ **Objective** For each cadet $ i \in \{1, \dots, n\} $, compute the grade $ g_i $ as: $$ g_i = n - \left| \left\{ j \in \{1, \dots, i-1\} \mid s_j \geq s_i \right\} \right| $$
API Response (JSON)
{
  "problem": {
    "name": "J. Smallest Difference",
    "description": {
      "content": "You are given an array a consists of n elements, find the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1. Th",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10185J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given an array a consists of n elements, find the maximum number of elements you can select from the array such that the absolute difference between any two of the chosen elements is  ≤ 1.\n\nTh...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of cadets.  \nLet $ S = (s_1, s_2, \\dots, s_n) $ be the sequence of scores, where $ s_i $ is the score of the $ i $-th cadet (in finishing order...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments