task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. There are N positive integers arranged in a circle. Now, the i-th number is A_i. Takahashi wants the i-th number to be B_i. For this objective, he will repeatedly perform the following operation: * Choose an integer i such that 1 \leq i \leq N. * Let a, b, c be the (i-1)-th, i-th, and (i+1)-th numbers, respectively. Replace the i-th number with a+b+c. Here the 0-th number is the N-th number, and the (N+1)-th number is the 1-st number. Determine if Takahashi can achieve his objective. If the answer is yes, find the minimum number of operations required. Constraints * 3 \leq N \leq 2 \times 10^5 * 1 \leq A_i, B_i \leq 10^9 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N B_1 B_2 ... B_N Output Print the minimum number of operations required, or `-1` if the objective cannot be achieved. Examples Input 3 1 1 1 13 5 7 Output 4 Input 4 1 2 3 4 2 3 4 5 Output -1 Input 5 5 6 5 2 1 9817 1108 6890 4343 8704 Output 25
{"inputs": ["3\n1 1 1\n13 1 7", "3\n2 1 1\n13 5 7", "3\n2 1 1\n13 1 7", "3\n2 1 1\n13 2 7", "3\n2 1 1\n13 2 0", "3\n1 1 1\n13 2 0", "3\n1 1 1\n13 3 0", "3\n1 1 1\n13 4 0"], "outputs": ["4\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n"]}
366
166
coding
Solve the programming task below in a Python markdown code block. Given an array $a$, consisting of $n$ integers, find: $$\max\limits_{1 \le i < j \le n} LCM(a_i,a_j),$$ where $LCM(x, y)$ is the smallest positive integer that is divisible by both $x$ and $y$. For example, $LCM(6, 8) = 24$, $LCM(4, 12) = 12$, $LCM(2, 3) = 6$. -----Input----- The first line contains an integer $n$ ($2 \le n \le 10^5$) — the number of elements in the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) — the elements of the array $a$. -----Output----- Print one integer, the maximum value of the least common multiple of two elements in the array $a$. -----Examples----- Input 3 13 35 77 Output 1001 Input 6 1 2 4 8 16 32 Output 32
{"inputs": ["2\n2 5\n", "2\n1 1\n", "2\n1 1\n", "2\n2 5\n", "2\n3 5\n", "3\n13 35 77\n", "4\n12 9 1 8\n", "4\n12 9 1 8\n"], "outputs": ["10", "1", "1\n", "10\n", "15\n", "1001", "72", "72\n"]}
282
121
coding
Solve the programming task below in a Python markdown code block. Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them. Recently, she was presented with an array $a$ of the size of $10^9$ elements that is filled as follows: $a_1 = -1$ $a_2 = 2$ $a_3 = -3$ $a_4 = 4$ $a_5 = -5$ And so on ... That is, the value of the $i$-th element of the array $a$ is calculated using the formula $a_i = i \cdot (-1)^i$. She immediately came up with $q$ queries on this array. Each query is described with two numbers: $l$ and $r$. The answer to a query is the sum of all the elements of the array at positions from $l$ to $r$ inclusive. Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer. Help her find the answers! -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^3$) — the number of the queries. Each of the next $q$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le 10^9$) — the descriptions of the queries. -----Output----- Print $q$ lines, each containing one number — the answer to the query. -----Example----- Input 5 1 3 2 5 5 5 4 4 2 3 Output -2 -2 -5 4 -1 -----Note----- In the first query, you need to find the sum of the elements of the array from position $1$ to position $3$. The sum is equal to $a_1 + a_2 + a_3 = -1 + 2 -3 = -2$. In the second query, you need to find the sum of the elements of the array from position $2$ to position $5$. The sum is equal to $a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$. In the third query, you need to find the sum of the elements of the array from position $5$ to position $5$. The sum is equal to $a_5 = -5$. In the fourth query, you need to find the sum of the elements of the array from position $4$ to position $4$. The sum is equal to $a_4 = 4$. In the fifth query, you need to find the sum of the elements of the array from position $2$ to position $3$. The sum is equal to $a_2 + a_3 = 2 - 3 = -1$.
{"inputs": ["1\n682736 1001010010\n", "1\n580408 1001010010\n", "1\n1042324 1001010010\n", "1\n4162847 1001001010\n", "1\n1133909 1001010010\n", "1\n6379767 1001001010\n", "1\n1133909 1001010011\n", "1\n6379767 1001101010\n"], "outputs": ["500846373\n", "500795209\n", "501026167\n", "498419082\n", "499938051\n", "497310622\n", "-501071960\n", "497360622\n"]}
664
284
coding
Solve the programming task below in a Python markdown code block. In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks. More precisely, two different numbers $a$ and $b$ are friends if $gcd(a,b)$, $\frac{a}{gcd(a,b)}$, $\frac{b}{gcd(a,b)}$ can form sides of a triangle. Three numbers $a$, $b$ and $c$ can form sides of a triangle if $a + b > c$, $b + c > a$ and $c + a > b$. In a group of numbers, a number is lonely if it doesn't have any friends in that group. Given a group of numbers containing all numbers from $1, 2, 3, ..., n$, how many numbers in that group are lonely? -----Input----- The first line contains a single integer $t$ $(1 \leq t \leq 10^6)$ - number of test cases. On next line there are $t$ numbers, $n_i$ $(1 \leq n_i \leq 10^6)$ - meaning that in case $i$ you should solve for numbers $1, 2, 3, ..., n_i$. -----Output----- For each test case, print the answer on separate lines: number of lonely numbers in group $1, 2, 3, ..., n_i$. -----Example----- Input 3 1 5 10 Output 1 3 3 -----Note----- For first test case, $1$ is the only number and therefore lonely. For second test case where $n=5$, numbers $1$, $3$ and $5$ are lonely. For third test case where $n=10$, numbers $1$, $5$ and $7$ are lonely.
{"inputs": ["3\n1 5 7\n", "3\n1 5 5\n", "3\n1 5 10\n", "3\n1 5 10\n", "6\n30 432 8 193 7 1\n", "6\n30 432 8 243 7 1\n", "6\n30 216 8 243 7 1\n", "6\n41 216 8 243 8 1\n"], "outputs": ["1\n3\n4\n", "1\n3\n3\n", "1\n3\n3\n", "1\n3\n3\n", "8\n76\n4\n39\n4\n1\n", "8\n76\n4\n48\n4\n1\n", "8\n42\n4\n48\n4\n1\n", "11\n42\n4\n48\n4\n1\n"]}
396
229
coding
Solve the programming task below in a Python markdown code block. You are given $n$ strings $s_1, s_2, \ldots, s_n$ consisting of lowercase Latin letters. In one operation you can remove a character from a string $s_i$ and insert it to an arbitrary position in a string $s_j$ ($j$ may be equal to $i$). You may perform this operation any number of times. Is it possible to make all $n$ strings equal? -----Input----- The first line contains $t$ ($1 \le t \le 10$): the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 1000$): the number of strings. $n$ lines follow, the $i$-th line contains $s_i$ ($1 \le \lvert s_i \rvert \le 1000$). The sum of lengths of all strings in all test cases does not exceed $1000$. -----Output----- If it is possible to make the strings equal, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can output each character in either lowercase or uppercase. -----Example----- Input 4 2 caa cbb 3 cba cba cbb 4 ccab cbac bca acbcc 4 acb caf c cbafc Output YES NO YES NO -----Note----- In the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings "ca" and "cbab" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to "cab". In the second test case, it is impossible to make all $n$ strings equal.
{"inputs": ["1\n2\nz\ny\n", "1\n2\na\ne\n", "1\n2\nz\ny\n", "1\n2\na\ne\n", "1\n1\nz\ny\n", "1\n2\nxx\nz\n", "1\n2\naa\nb\n", "1\n2\naa\na\n"], "outputs": ["NO\n", "NO\n", "NO\n", "NO\n", "YES\n", "NO\n", "NO\n", "NO\n"]}
421
118
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given the root of a binary tree with n nodes. Each node is assigned a unique value from 1 to n. You are also given an array queries of size m. You have to perform m independent queries on the tree where in the ith query you do the following: Remove the subtree rooted at the node with the value queries[i] from the tree. It is guaranteed that queries[i] will not be equal to the value of the root. Return an array answer of size m where answer[i] is the height of the tree after performing the ith query. Note: The queries are independent, so the tree returns to its initial state after each query. The height of a tree is the number of edges in the longest simple path from the root to some node in the tree.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,3,4,2,None,6,5,None,None,None,None,None,7]), queries = [4]) == [2]\n assert candidate(root = tree_node([5,8,9,2,1,3,7,4,6]), queries = [3,2,4,8]) == [3,2,3,2]\n\n\ncheck(Solution().treeQueries)"}
265
105
coding
Solve the programming task below in a Python markdown code block. There is a fair going on in Chefland. Chef wants to visit the fair along with his N friends. Chef manages to collect K passes for the fair. Will Chef be able to enter the fair with all his N friends? A person can enter the fair using one pass, and each pass can be used by only one person. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of a single line containing two space-separated integers N, K. ------ Output Format ------ For each test case, print on a new line YES if Chef will be able to enter the fair with all his N friends and NO otherwise. You may print each character of the string in either uppercase or lowercase (for example, the strings yEs, yes, Yes, and YES will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100$ ----- Sample Input 1 ------ 4 5 8 6 3 2 2 1 2 ----- Sample Output 1 ------ YES NO NO YES ----- explanation 1 ------ Test case $1$: Chef needs $5$ passes for his friends and one pass for himself and he collected $8$ passes. Thus he will be able to enter the fair with all his friends. Test case $2$: Chef needs $6$ passes for his friends and one pass for himself while he collected only $3$ passes. Thus he will not be able to enter the fair with all his friends, only three of them can enter the fair. Test case $3$: Chef needs $2$ passes for his friends and one pass for himself while he collected only $2$ passes. Thus either Chef or one of his friends can't enter the fair. Test case $4$: Chef needs a total of $2$ passes, one for himself and one for his friend. He collected $2$ passes. Thus he will be able to enter the fair with his friend.
{"inputs": ["4\n5 8\n6 3\n2 2\n1 2\n"], "outputs": ["YES\nNO\nNO\nYES\n"]}
446
36
coding
Solve the programming task below in a Python markdown code block. For an array $b$ of length $m$ we define the function $f$ as $ f(b) = \begin{cases} b[1] & \quad \text{if } m = 1 \\ f(b[1] \oplus b[2],b[2] \oplus b[3],\dots,b[m-1] \oplus b[m]) & \quad \text{otherwise,} \end{cases} $ where $\oplus$ is bitwise exclusive OR. For example, $f(1,2,4,8)=f(1\oplus2,2\oplus4,4\oplus8)=f(3,6,12)=f(3\oplus6,6\oplus12)=f(5,10)=f(5\oplus10)=f(15)=15$ You are given an array $a$ and a few queries. Each query is represented as two integers $l$ and $r$. The answer is the maximum value of $f$ on all continuous subsegments of the array $a_l, a_{l+1}, \ldots, a_r$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 5000$) — the length of $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2^{30}-1$) — the elements of the array. The third line contains a single integer $q$ ($1 \le q \le 100\,000$) — the number of queries. Each of the next $q$ lines contains a query represented as two integers $l$, $r$ ($1 \le l \le r \le n$). -----Output----- Print $q$ lines — the answers for the queries. -----Examples----- Input 3 8 4 1 2 2 3 1 2 Output 5 12 Input 6 1 2 4 8 16 32 4 1 6 2 5 3 4 1 2 Output 60 30 12 3 -----Note----- In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment. In second sample, optimal segment for first query are $[3,6]$, for second query — $[2,5]$, for third — $[3,4]$, for fourth — $[1,2]$.
{"inputs": ["3\n8 4 1\n2\n2 3\n1 2\n", "3\n6 4 1\n2\n2 3\n1 2\n", "3\n8 4 1\n2\n2 3\n2 2\n", "3\n8 0 1\n2\n2 3\n2 2\n", "3\n8 1 1\n2\n2 3\n2 2\n", "3\n8 4 1\n2\n1 3\n1 2\n", "3\n8 4 1\n2\n1 3\n2 2\n", "3\n8 0 1\n2\n2 2\n2 2\n"], "outputs": ["5\n12\n", "5\n6\n", "5\n4\n", "1\n0\n", "1\n1\n", "12\n12\n", "12\n4\n", "0\n0\n"]}
592
218
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. ------ Sereja and Arrays ------ Sereja have an array that consist of n integers a1, a2, ..., an (0 ≤ ai ≤ 1). Sereja can make next operation: fix some integer i (1 ≤ i ≤ n - k + 1) subtract 1 from values: ai, ai + 1, ..., ai + k - 1Sereja call array a good if it is possible to make some operations, that he can and get array that contain only zeros. Now Sereja interested in next question: how many good arrays a with length n exist? ------ Input ------ First line contain integer T — number of testcases. T tests follow. Each testcase is given by two integers n and k. ------ Constraints ------ $ 1 ≤ T ≤ 10 $ $ 1 ≤ k ≤ n ≤ 105 $ ------ Output ------ For each testcase output answer modulo 109 + 7. ------ Note ------ Test #0-1(25 points) n ≤ 15 Test #2(25 points) n ≤ 100 Test #3(50 points) n ≤ 100000 ----- Sample Input 1 ------ 3 3 3 5 2 5 1 ----- Sample Output 1 ------ 2 8 32
{"inputs": ["3\n3 3\n5 2\n5 1"], "outputs": ["2\n8\n32"]}
370
29
coding
Solve the programming task below in a Python markdown code block. Snuke loves puzzles. Today, he is working on a puzzle using S- and c-shaped pieces. In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below: Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces. Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces. -----Constraints----- - 1 ≤ N,M ≤ 10^{12} -----Input----- The input is given from Standard Input in the following format: N M -----Output----- Print the answer. -----Sample Input----- 1 6 -----Sample Output----- 2 Two Scc groups can be created as follows: - Combine two c-shaped pieces into one S-shaped piece - Create two Scc groups, each from one S-shaped piece and two c-shaped pieces
{"inputs": ["1 6\n", "12345 678901\n"], "outputs": ["2\n", "175897\n"]}
208
40
coding
Solve the programming task below in a Python markdown code block. Paul is an excellent coder and sits high on the CW leaderboard. He solves kata like a banshee but would also like to lead a normal life, with other activities. But he just can't stop solving all the kata!! Given an array (x) you need to calculate the Paul Misery Score. The values are worth the following points: kata = 5 Petes kata = 10 life = 0 eating = 1 The Misery Score is the total points gained from the array. Once you have the total, return as follows: <40 = 'Super happy!'<70 >=40 = 'Happy!'<100 >=70 = 'Sad!'\>=100 = 'Miserable!' Also feel free to reuse/extend the following starter code: ```python def paul(x): ```
{"functional": "_inputs = [[['life', 'eating', 'life']], [['life', 'Petes kata', 'Petes kata', 'Petes kata', 'eating']], [['Petes kata', 'Petes kata', 'eating', 'Petes kata', 'Petes kata', 'eating']]]\n_outputs = [['Super happy!'], ['Super happy!'], ['Happy!']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(paul(*i), o[0])"}
193
220
coding
Solve the programming task below in a Python markdown code block. **Introduction**  Little Petya very much likes sequences. However, recently he received a sequence as a gift from his mother.  Petya didn't like it at all! He decided to make a single replacement. After this replacement, Petya would like to the sequence in increasing order.  He asks himself: What is the lowest possible value I could have got after making the replacement and sorting the sequence? **About the replacement**  Choose exactly one element from the sequence and replace it with another integer > 0. You are **not allowed** to replace a number with itself, or to change no number at all. **Task**  Find the lowest possible sequence after performing a valid replacement, and sorting the sequence. **Input:**  Input contains sequence with `N` integers. All elements of the sequence > 0. The sequence will never be empty. **Output:**  Return sequence with `N` integers — which includes the lowest possible values of each sequence element, after the single replacement and sorting has been performed. **Examples**: ``` ([1,2,3,4,5]) => [1,1,2,3,4] ([4,2,1,3,5]) => [1,1,2,3,4] ([2,3,4,5,6]) => [1,2,3,4,5] ([2,2,2]) => [1,2,2] ([42]) => [1] ``` Also feel free to reuse/extend the following starter code: ```python def sort_number(a): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 5]], [[4, 2, 1, 3, 5]], [[2, 3, 4, 5, 6]], [[2, 2, 2]], [[42]], [[5, 6, 1, 2, 3, 1, 3, 45, 7, 1000000000]], [[1, 1, 1]], [[1]], [[134]]]\n_outputs = [[[1, 1, 2, 3, 4]], [[1, 1, 2, 3, 4]], [[1, 2, 3, 4, 5]], [[1, 2, 2]], [[1]], [[1, 1, 1, 2, 3, 3, 5, 6, 7, 45]], [[1, 1, 2]], [[2]], [[1]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sort_number(*i), o[0])"}
365
366
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a numeric string num, representing a very large palindrome. Return the smallest palindrome larger than num that can be created by rearranging its digits. If no such palindrome exists, return an empty string "". A palindrome is a number that reads the same backward as forward.   Please complete the following python code precisely: ```python class Solution: def nextPalindrome(self, num: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(num = \"1221\") == \"2112\"\n assert candidate(num = \"32123\") == \"\"\n assert candidate(num = \"45544554\") == \"54455445\"\n\n\ncheck(Solution().nextPalindrome)"}
103
81
coding
Solve the programming task below in a Python markdown code block. Krishnakant is standing at $(0,0)$ in the Cartesian plane. He wants to go to the point $(x,y)$ in the same plane using only horizontal and vertical moves of $1$ unit. There are many ways of doing this, and he is writing down all such ways. Each way comprises of few ${H}$ moves and few $V}$ moves. i.e. moves in horizontal and vertical direction respectively. For example, if Krishnakant wants to go to point $(2,2)$ from point $(0,0)$, ${HVHV}$ is one of the possible ways. Given the value of ${K}$, he wants to know lexicographically $K^{{th}}$ smallest way of going to $(x,y)$ from $(0,0)$. Input Format The first line contains an integer ${T}$ , i.e., number of test cases. Next ${T}$ lines will contain integers ${x}$,$y$ and ${K}$. Output Format For each test case, print lexicographically $K^{{th}}$ smallest path. Constraints $1\leq T\leq1000000$ $1\leq x\leq10$ $1\leq y\leq10$ $0\leq K<\text{number of paths}$ Sample Input 2 2 2 2 2 2 3 Sample Output HVVH VHHV Explanation All the paths of going to $(2,2)$ from $(0,0)$ in lexicographically increasing order: $0.HHVV$ $1.HVHV$ $2.HVVH$ $3.VHHV$ $4.VHVH$ $5.VVHH$
{"inputs": ["2\n2 2 2\n2 2 3\n"], "outputs": ["HVVH\nVHHV\n"]}
393
32
coding
Solve the programming task below in a Python markdown code block. ## Task In your favorite game, you must shoot a target with a water-gun to gain points. Each target can be worth a different amount of points. You are guaranteed to hit every target that you try to hit. You cannot hit consecutive targets though because targets are only visible for one second (one at a time) and it takes you a full second to reload your water-gun after shooting (you start the game already loaded). Given an array `vals` with the order of each target's point value, determine the maximum number of points that you can win. ## Example For `vals = [1, 2, 3, 4]`, the result should be `6`. your optimal strategy would be to let the first one pass and shoot the second one with value 2 and the 4th one with value 4 thus: `vals[1](2) + vals[3](4) = 6` For `vals = [5, 5, 5, 5, 5]`, the result should be `15`. your optimal strategy would be to shoot the 1st, 3rd and 5th value: `5 + 5 + 5 = 15` You haven't shoot the 2nd, 4th value because you are reloading your water-gun after shooting other values. Note that the value can be zero or negative, don't shoot them ;-) For `vals = [0, 0, -1, -1]`, the result should be `0`. For `vals = [5, -2, -9, -4]`, the result should be `5`. Shoot the first one is enough. ## Input/Output - `[input]` integer array `vals` The point values (negative or non-negative) of the targets (in order of appearance). - `[output]` an integer The maximum number of points that you can score. Also feel free to reuse/extend the following starter code: ```python def target_game(values): ```
{"functional": "_inputs = [[[1, 2, 3, 4]], [[1, 3, 1]], [[5, 5, 5, 5, 5]], [[36, 42, 93, 29, 0, 33, 15, 84, 14, 24, 81, 11]], [[73, 80, 40, 86, 14, 96, 10, 56, 61, 84, 82, 36, 85]], [[11, 82, 47, 48, 80, 35, 73, 99, 86, 32, 32]], [[26, 54, 36, 35, 63, 58, 31, 80, 59, 61, 34, 54, 62, 73, 89, 7, 98, 91, 78]], [[0, 0, -1, -1]], [[1, 0, 0, 1]], [[5, -2, -9, -4]]]\n_outputs = [[6], [3], [15], [327], [490], [353], [615], [0], [2], [5]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(target_game(*i), o[0])"}
447
477
coding
Solve the programming task below in a Python markdown code block. We have N bricks arranged in a row from left to right. The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it. Among them, you can break at most N-1 bricks of your choice. Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it. Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 200000 - 1 \leq a_i \leq N -----Input----- Input is given from Standard Input in the following format: N a_1 a_2 ... a_N -----Output----- Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable. -----Sample Input----- 3 2 1 2 -----Sample Output----- 1 If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.
{"inputs": ["1\n2", "1\n0", "1\n4", "1\n5", "1\n3", "1\n6", "1\n7", "1\n1"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "0"]}
305
78
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are playing a game involving a circular array of non-zero integers nums. Each nums[i] denotes the number of indices forward/backward you must move if you are located at index i: If nums[i] is positive, move nums[i] steps forward, and If nums[i] is negative, move nums[i] steps backward. Since the array is circular, you may assume that moving forward from the last element puts you on the first element, and moving backwards from the first element puts you on the last element. A cycle in the array consists of a sequence of indices seq of length k where: Following the movement rules above results in the repeating index sequence seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ... Every nums[seq[j]] is either all positive or all negative. k > 1 Return true if there is a cycle in nums, or false otherwise.   Please complete the following python code precisely: ```python class Solution: def circularArrayLoop(self, nums: List[int]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,-1,1,2,2]) == True\n assert candidate(nums = [-1,2]) == False\n assert candidate(nums = [-2,1,-1,-2,-2]) == False\n\n\ncheck(Solution().circularArrayLoop)"}
240
72
coding
Solve the programming task below in a Python markdown code block. Given an array of bird sightings where every element represents a bird type id, determine the id of the most frequently sighted type. If more than 1 type has been spotted that maximum amount, return the smallest of their ids. Example $arr=[1,1,2,2,3]$ There are two each of types $1$ and $2$, and one sighting of type $3$. Pick the lower of the two types seen twice: type $1$. Function Description Complete the migratoryBirds function in the editor below. migratoryBirds has the following parameter(s): int arr[n]: the types of birds sighted Returns int: the lowest type id of the most frequently sighted birds Input Format The first line contains an integer, $n$, the size of $\textbf{arr}$. The second line describes $\textbf{arr}$ as $n$ space-separated integers, each a type number of the bird sighted. Constraints $5\leq n\leq2\times10^5$ It is guaranteed that each type is $1$, $2$, $3$, $4$, or $5$. Sample Input 0 6 1 4 4 4 5 3 Sample Output 0 4 Explanation 0 The different types of birds occur in the following frequencies: Type $1$: $1$ bird Type $2$: $0$ birds Type $3$: $1$ bird Type $4$: $3$ birds Type $5$: $1$ bird The type number that occurs at the highest frequency is type $4$, so we print $4$ as our answer. Sample Input 1 11 1 2 3 4 5 4 3 2 1 3 4 Sample Output 1 3 Explanation 1 The different types of birds occur in the following frequencies: Type $1$: $2$ Type $2$: $2$ Type $3$: $3$ Type $4$: $3$ Type $5$: $1$ Two types have a frequency of $3$, and the lower of those is type $3$.
{"inputs": ["6\n1 4 4 4 5 3\n", "11\n1 2 3 4 5 4 3 2 1 3 4\n"], "outputs": ["4\n", "3\n"]}
484
57
coding
Solve the programming task below in a Python markdown code block. We have an array of length $n$. Initially, each element is equal to $0$ and there is a pointer located on the first element. We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $1$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $1$. Then move it to the previous element. But there is one additional rule. After we are done, the pointer has to be on the first element. You are given an array $a$. Determine whether it's possible to obtain $a$ after some operations or not. -----Input----- The first line contains a single integer $t$ $(1\le t\le 1000)$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ $(1\le n\le 2 \cdot 10^5)$ — the size of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print "Yes" (without quotes) if it's possible to obtain $a$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). -----Examples----- Input 7 2 1 0 4 2 -1 -1 0 4 1 -4 3 0 4 1 -1 1 -1 5 1 2 3 4 -10 7 2 -1 1 -2 0 0 0 1 0 Output No Yes No No Yes Yes Yes -----Note----- In the first test case we can obtain the array after some operations, but the pointer won't be on the first element. One way of obtaining the array in the second test case is shown below. $\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$
{"inputs": ["1\n1\n1\n", "1\n2\n2 2\n", "1\n2\n-800372697 800372697\n", "7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0\n"], "outputs": ["No\n", "No\n", "No\n", "No\nYes\nNo\nNo\nYes\nYes\nYes\n"]}
677
152
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest such sequence). Return [m,n]. If there are no such m and n (e.g. the array is already sorted), return [-1, -1]. Please complete the following python code precisely: ```python class Solution: def subSort(self, array: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate([1,2,4,7,10,11,7,12,6,7,16,18,19]) == [3,9]\n\n\ncheck(Solution().subSort)"}
126
62
coding
Solve the programming task below in a Python markdown code block. Methodius received an email from his friend Polycarp. However, Polycarp's keyboard is broken, so pressing a key on it once may cause the corresponding symbol to appear more than once (if you press a key on a regular keyboard, it prints exactly one symbol). For example, as a result of typing the word "hello", the following words could be printed: "hello", "hhhhello", "hheeeellllooo", but the following could not be printed: "hell", "helo", "hhllllooo". Note, that when you press a key, the corresponding symbol must appear (possibly, more than once). The keyboard is broken in a random manner, it means that pressing the same key you can get the different number of letters in the result. For each word in the letter, Methodius has guessed what word Polycarp actually wanted to write, but he is not sure about it, so he asks you to help him. You are given a list of pairs of words. For each pair, determine if the second word could be printed by typing the first one on Polycarp's keyboard. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 10^5$) — the number of pairs to check. Further input contains $n$ descriptions of pairs. The first line of each description contains a single non-empty word $s$ consisting of lowercase Latin letters. The second line of the description contains a single non-empty word $t$ consisting of lowercase Latin letters. The lengths of both strings are not greater than $10^6$. It is guaranteed that the total length of all words $s$ in the input is not greater than $10^6$. Also, it is guaranteed that the total length of all words $t$ in the input is not greater than $10^6$. -----Output----- Output $n$ lines. In the $i$-th line for the $i$-th pair of words $s$ and $t$ print YES if the word $t$ could be printed by typing the word $s$. Otherwise, print NO. -----Examples----- Input 4 hello hello hello helloo hello hlllloo hello helo Output YES YES NO NO Input 5 aa bb codeforces codeforce polycarp poolycarpp aaaa aaaab abcdefghijklmnopqrstuvwxyz zabcdefghijklmnopqrstuvwxyz Output NO NO YES NO NO
{"inputs": ["4\nhello\nhello\nhello\nhelloo\nhello\nhlllloo\nhello\nhelo\n", "4\nhello\nhello\nhello\nhelloo\nhemlo\nhlllloo\nhello\nhelo\n", "4\nhello\nhello\nolleh\nhelloo\nhemlo\nhlllloo\nhello\nhelo\n", "4\nhello\nhdllo\nolkeh\nhelmoo\nhemlo\nhlllloo\nhello\nhelo\n", "4\nhello\nhello\nolleh\nhelmoo\nhemlo\nhlllloo\nhello\nhelo\n", "4\nhello\nhello\nolkeh\nhelmoo\nhemlo\nhlllloo\nhello\nhelo\n", "4\nlehlo\nhdllo\nolkeh\nhelmoo\nhemlo\nhlllloo\nhello\nhelo\n", "4\nlehlo\nhdllo\nolkeh\nhelmoo\nhemlo\nhlllmoo\nhello\nhelo\n"], "outputs": ["YES\nYES\nNO\nNO\n", "YES\nYES\nNO\nNO\n", "YES\nNO\nNO\nNO\n", "NO\nNO\nNO\nNO\n", "YES\nNO\nNO\nNO\n", "YES\nNO\nNO\nNO\n", "NO\nNO\nNO\nNO\n", "NO\nNO\nNO\nNO\n"]}
546
304
coding
Solve the programming task below in a Python markdown code block. We have a sequence A of N non-negative integers. Compute the sum of \prod _{i = 1} ^N \dbinom{B_i}{A_i} over all sequences B of N non-negative integers whose sum is at most M, and print it modulo (10^9 + 7). Here, \dbinom{B_i}{A_i}, the binomial coefficient, denotes the number of ways to choose A_i objects from B_i objects, and is 0 when B_i < A_i. -----Constraints----- - All values in input are integers. - 1 \leq N \leq 2000 - 1 \leq M \leq 10^9 - 0 \leq A_i \leq 2000 -----Input----- Input is given from Standard Input in the following format: N M A_1 A_2 \ldots A_N -----Output----- Print the sum of \prod _{i = 1} ^N \dbinom{B_i}{A_i}, modulo (10^9 + 7). -----Sample Input----- 3 5 1 2 1 -----Sample Output----- 8 There are four sequences B such that \prod _{i = 1} ^N \dbinom{B_i}{A_i} is at least 1: - B = \{1, 2, 1\}, where \prod _{i = 1} ^N \dbinom{B_i}{A_i} = \dbinom{1}{1} \times \dbinom{2}{2} \times \dbinom{1}{1} = 1; - B = \{2, 2, 1\}, where \prod _{i = 1} ^N \dbinom{B_i}{A_i} = \dbinom{2}{1} \times \dbinom{2}{2} \times \dbinom{1}{1} = 2; - B = \{1, 3, 1\}, where \prod _{i = 1} ^N \dbinom{B_i}{A_i} = \dbinom{1}{1} \times \dbinom{3}{2} \times \dbinom{1}{1} = 3; - B = \{1, 2, 2\}, where \prod _{i = 1} ^N \dbinom{B_i}{A_i} = \dbinom{1}{1} \times \dbinom{2}{2} \times \dbinom{2}{1} = 2. The sum of these is 1 + 2 + 3 + 2 = 8.
{"inputs": ["1 1\n0\n", "1 1\n1\n", "1 1\n2\n", "1 1\n2000\n", "3 5\n1 2 1\n", "1 1999\n2000\n", "1 1000000000\n0\n", "10 998244353\n31 41 59 26 53 58 97 93 23 84\n"], "outputs": ["2\n", "1\n", "0\n", "0\n", "8\n", "0\n", "1000000001\n", "642612171\n"]}
618
178
coding
Solve the programming task below in a Python markdown code block. An Arithmetic Progression is defined as one in which there is a constant difference between the consecutive terms of a given series of numbers. You are provided with consecutive elements of an Arithmetic Progression. There is however one hitch: exactly one term from the original series is missing from the set of numbers which have been given to you. The rest of the given series is the same as the original AP. Find the missing term. You have to write a function that receives a list, list size will always be at least 3 numbers. The missing term will never be the first or last one. ## Example ```python find_missing([1, 3, 5, 9, 11]) == 7 ``` PS: This is a sample question of the facebook engineer challenge on interviewstreet. I found it quite fun to solve on paper using math, derive the algo that way. Have fun! Also feel free to reuse/extend the following starter code: ```python def find_missing(sequence): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 6, 7, 8, 9]]]\n_outputs = [[5]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_missing(*i), o[0])"}
221
175
coding
Solve the programming task below in a Python markdown code block. It is a complicated version of problem F1. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 10$). -----Output----- For each test case output on a separate line $x$ — the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 -----Note----- None
{"inputs": ["1\n688 10\n", "1\n701 10\n", "1\n5828 1\n", "1\n9772 1\n", "1\n4955 2\n", "1\n2388 2\n", "1\n2388 4\n", "1\n2388 1\n"], "outputs": ["688\n", "701\n", "6666\n", "9999\n", "4994\n", "2422\n", "2388\n", "3333\n"]}
421
148
coding
Solve the programming task below in a Python markdown code block. After a lot of hard work, Goyal has finally completed his app. But before he launches it, he needs to test it. His friends are helping him on this. Whenever a friend uses the app, his name is recorded in the logs. At the end of the day, Goyal makes a "Hall of Fame" by number of times a person has used his app, and promises to give a treat to the leader. Things are getting out of control as his friends are trying hard to make it to the top on the hall of fame. The server is barely able to handle this amount of traffic as Goyal has lots of friends testing his app. Hence, he is facing problems preparing the hall of fame. Help him prepare it with your world-renowned programming skills. Input Format: The first line contains number of entries in the log. The next line contains space separated entries, each denoting the name of the friend who used his app. Output Format: Output the hall of fame as "name number_of_times_used" sorted in decreasing order of number_of_times_used. In case of a tie, order by time. Constraints: 1 ≤ Number of entries ≤ 100 SAMPLE INPUT 6 Mohit Karan Mohit Mohit Ajay Karan SAMPLE OUTPUT Mohit 3 Karan 2 Ajay 1
{"inputs": ["7\nArjun Mohit Karan Mohit Mohit Ajay Karan"], "outputs": ["Mohit 3\nKaran 2\nArjun 1\nAjay 1"]}
290
45
coding
Solve the programming task below in a Python markdown code block. Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students from the same group were more successful and celebrated after the exam. Some of them celebrated in the BugDonalds restaurant, some of them — in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn't pass the exam didn't celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by $A$ students, BeaverKing — by $B$ students and $C$ students visited both restaurants. Vasya also knows that there are $N$ students in his group. Based on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn't, how many students in his group didn't pass the exam. Can you help him so he won't waste his valuable preparation time? -----Input----- The first line contains four integers — $A$, $B$, $C$ and $N$ ($0 \leq A, B, C, N \leq 100$). -----Output----- If a distribution of $N$ students exists in which $A$ students visited BugDonalds, $B$ — BeaverKing, $C$ — both of the restaurants and at least one student is left home (it is known that Vasya didn't pass the exam and stayed at home), output one integer — amount of students (including Vasya) who did not pass the exam. If such a distribution does not exist and Vasya made a mistake while determining the numbers $A$, $B$, $C$ or $N$ (as in samples 2 and 3), output $-1$. -----Examples----- Input 10 10 5 20 Output 5 Input 2 2 0 4 Output -1 Input 2 2 2 1 Output -1 -----Note----- The first sample describes following situation: $5$ only visited BugDonalds, $5$ students only visited BeaverKing, $5$ visited both of them and $5$ students (including Vasya) didn't pass the exam. In the second sample $2$ students only visited BugDonalds and $2$ only visited BeaverKing, but that means all $4$ students in group passed the exam which contradicts the fact that Vasya didn't pass meaning that this situation is impossible. The third sample describes a situation where $2$ students visited BugDonalds but the group has only $1$ which makes it clearly impossible.
{"inputs": ["2 2 0 4\n", "2 2 2 1\n", "6 7 5 8\n", "6 7 5 9\n", "6 7 5 7\n", "0 0 0 0\n", "1 1 0 4\n", "5 5 3 1\n"], "outputs": ["-1", "-1", "-1", "1", "-1", "-1", "2", "-1"]}
620
111
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an m x n integer matrix grid, and three integers row, col, and color. Each value in the grid represents the color of the grid square at that location. Two squares are called adjacent if they are next to each other in any of the 4 directions. Two squares belong to the same connected component if they have the same color and they are adjacent. The border of a connected component is all the squares in the connected component that are either adjacent to (at least) a square not in the component, or on the boundary of the grid (the first or last row or column). You should color the border of the connected component that contains the square grid[row][col] with color. Return the final grid.   Please complete the following python code precisely: ```python class Solution: def colorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,1],[1,2]], row = 0, col = 0, color = 3) == [[3,3],[3,2]]\n assert candidate(grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3) == [[1,3,3],[2,3,3]]\n assert candidate(grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2) == [[2,2,2],[2,1,2],[2,2,2]]\n\n\ncheck(Solution().colorBorder)"}
211
170
coding
Solve the programming task below in a Python markdown code block. Everybody know that you passed to much time awake during night time... Your task here is to define how much coffee you need to stay awake after your night. You will have to complete a function that take an array of events in arguments, according to this list you will return the number of coffee you need to stay awake during day time. **Note**: If the count exceed 3 please return 'You need extra sleep'. The list of events can contain the following: - You come here, to solve some kata ('cw'). - You have a dog or a cat that just decide to wake up too early ('dog' | 'cat'). - You just watch a movie ('movie'). - Other events can be present and it will be represent by arbitrary string, just ignore this one. Each event can be downcase/lowercase, or uppercase. If it is downcase/lowercase you need 1 coffee by events and if it is uppercase you need 2 coffees. Also feel free to reuse/extend the following starter code: ```python def how_much_coffee(events): ```
{"functional": "_inputs = [[[]], [['cw']], [['CW']], [['cw', 'CAT']], [['cw', 'CAT', 'DOG']], [['cw', 'CAT', 'cw=others']]]\n_outputs = [[0], [1], [2], [3], ['You need extra sleep'], [3]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(how_much_coffee(*i), o[0])"}
238
209
coding
Solve the programming task below in a Python markdown code block. Nauuo is a girl who loves playing cards. One day she was playing cards but found that the cards were mixed with some empty ones. There are $n$ cards numbered from $1$ to $n$, and they were mixed with another $n$ empty cards. She piled up the $2n$ cards and drew $n$ of them. The $n$ cards in Nauuo's hands are given. The remaining $n$ cards in the pile are also given in the order from top to bottom. In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile. Nauuo wants to make the $n$ numbered cards piled up in increasing order (the $i$-th card in the pile from top to bottom is the card $i$) as quickly as possible. Can you tell her the minimum number of operations? -----Input----- The first line contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of numbered cards. The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0\le a_i\le n$) — the initial cards in Nauuo's hands. $0$ represents an empty card. The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($0\le b_i\le n$) — the initial cards in the pile, given in order from top to bottom. $0$ represents an empty card. It is guaranteed that each number from $1$ to $n$ appears exactly once, either in $a_{1..n}$ or $b_{1..n}$. -----Output----- The output contains a single integer — the minimum number of operations to make the $n$ numbered cards piled up in increasing order. -----Examples----- Input 3 0 2 0 3 0 1 Output 2 Input 3 0 2 0 1 0 3 Output 4 Input 11 0 0 0 5 0 0 0 4 0 0 11 9 2 6 0 8 1 7 0 3 0 10 Output 18 -----Note----- Example 1 We can play the card $2$ and draw the card $3$ in the first operation. After that, we have $[0,3,0]$ in hands and the cards in the pile are $[0,1,2]$ from top to bottom. Then, we play the card $3$ in the second operation. The cards in the pile are $[1,2,3]$, in which the cards are piled up in increasing order. Example 2 Play an empty card and draw the card $1$, then play $1$, $2$, $3$ in order.
{"inputs": ["1\n1\n0\n", "1\n0\n1\n", "1\n0\n1\n", "1\n1\n0\n", "2\n0 2\n0 1\n", "2\n0 1\n0 2\n", "2\n0 0\n2 1\n", "2\n0 0\n1 2\n"], "outputs": ["1", "0", "0\n", "1\n", "1", "3", "4", "0"]}
647
112
coding
Solve the programming task below in a Python markdown code block. An array is defined to be `inertial`if the following conditions hold: ``` a. it contains at least one odd value b. the maximum value in the array is even c. every odd value is greater than every even value that is not the maximum value. ``` eg:- ``` So [11, 4, 20, 9, 2, 8] is inertial because a. it contains at least one odd value [11,9] b. the maximum value in the array is 20 which is even c. the two odd values (11 and 9) are greater than all the even values that are not equal to 20 (the maximum), i.e., [4, 2, 8] ``` Write a function called `is_inertial` that accepts an integer array and returns `true` if the array is inertial; otherwise it returns `false`. Also feel free to reuse/extend the following starter code: ```python def is_inertial(arr): ```
{"functional": "_inputs = [[[]], [[581, -384, 140, -287]], [[11, 4, 20, 9, 2, 8]]]\n_outputs = [[False], [False], [True]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_inertial(*i), o[0])"}
238
202
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s and an array of strings words. You should add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in words. If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag. If two substrings wrapped by bold tags are consecutive, you should combine them. Return s after adding the bold tags.   Please complete the following python code precisely: ```python class Solution: def addBoldTag(self, s: str, words: List[str]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abcxyz123\", words = [\"abc\",\"123\"]) == \"<b>abc</b>xyz<b>123</b>\"\n assert candidate(s = \"aaabbb\", words = [\"aa\",\"b\"]) == \"<b>aaabbb</b>\"\n\n\ncheck(Solution().addBoldTag)"}
139
95
coding
Solve the programming task below in a Python markdown code block. There are $n$ red balls kept on the positive $X$ axis, and $m$ blue balls kept on the positive $Y$ axis. You are given the positions of the balls. For each $i$ from $1$ to $n$, the $i$-th red ball has the coordinates $(x_i, 0)$, where $x_i$ is a positive integer. For each $i$ from $1$ to $m$, the $i$-th blue ball has the coordinates $(0, y_i)$, where $ y_i$ is a positive integer. It is given that all $x_i$ are distinct. Also, all $y_i$ are distinct. At the time $t= 0$, for each $i$ from $1$ to $n$, the $i^{\text{th}}$ red ball is thrown towards positive $Y$ axis with a speed of $u_i$( that is, with velocity vector $(0, u_i)$). Simultaneously (at time $t = 0$), for each $i$ from $1$ to $m$, the $i^{\text{th}}$ blue ball is thrown towards positive $X$ axis with a speed of $v_i$ (that is, with velocity vector $(v_i, 0)$). Two balls are said to collide if they are at the same position at the same time. When two balls collide, they disappear, and hence no longer collide with other balls. (See sample examples for clarification). Find the total number of collisions the balls will have. -----Input----- - First line contains $n$ and $m$, the number of red balls, and the number of blue balls, respectively. - $i^{\text{th}}$ of the next $n$ lines contains two space separated integers $x_i$ and $u_i$, the position and speed of the $i^{\text{th}}$ red ball respectively - $i^{\text{th}}$ of the next $m$ lines contains two space separated integers $y_i$ and $v_i$, the position and speed of the $i^{\text{th}}$ blue ball respectively -----Output----- Print the number of collisions. -----Constraints----- - $1 \le n, m \le 10^5$ - $1 \le x_i, u_i, y_i, v_i \le 10^9$ - for all $1 \le i < j \le n, x_i \neq x_j$ - for all $1 \le i < j \le m, y_i \neq y_j$ -----Example Input 1----- 1 1 1 2 2 1 -----Example Output 1----- 1 -----Example Input 2----- 1 2 1 2 2 1 1 2 -----Example Output 2----- 1 -----Explanation----- Example case 1: The balls collide at t = 1, at the coordinates (1, 2). Example case 2: The red ball and the second blue ball collide at time 0.5 at coordinates (1, 1). Note that the first blue ball would have collided with the red ball at t = 1 (like in sample input # 1), if the second blue ball wasn't present. But since the red ball disappears at t = 0.5, its collision with first blue ball doesn't happen. The total number of collisions is 1.
{"inputs": ["1 1\n1 2\n2 1\n", "1 2\n1 2\n2 1\n1 2\n"], "outputs": ["1", "1"]}
754
44
coding
Solve the programming task below in a Python markdown code block. When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a $2 \times n$ grid where each row is a permutation of the numbers $1,2,3,\ldots,n$. The goal of Little Alawn's puzzle is to make sure no numbers on the same column or row are the same (we'll call this state of the puzzle as solved), and to achieve this he is able to swap the numbers in any column. However, after solving the puzzle many times, Little Alawn got bored and began wondering about the number of possible solved configurations of the puzzle he could achieve from an initial solved configuration only by swapping numbers in a column. Unfortunately, Little Alawn got stuck while trying to solve this harder problem, so he was wondering if you could help him with it. Find the answer modulo $10^9+7$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$). The next two lines of each test case describe the initial state of the puzzle grid. Each line will be a permutation of the numbers $1,2,3,\ldots,n$ and the numbers in each column and row will be pairwise distinct. It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output a single integer, the number of possible solved configurations of the puzzle Little Alawn can achieve from an initial solved configuration only by swapping numbers in a column. As the answer can be very large, please output it modulo $10^9+7$. The answer for each test case should be on a separate line. -----Examples----- Input 2 4 1 4 2 3 3 2 1 4 8 2 6 5 1 4 3 7 8 3 8 7 5 1 2 4 6 Output 2 8 -----Note----- The two possible puzzle configurations for example $1$ are: $[1,4,2,3]$ in the first row and $[3,2,1,4]$ in the second; $[3,2,1,4]$ in the first row and $[1,4,2,3]$ in the second.
{"inputs": ["2\n4\n1 4 2 3\n3 2 1 4\n8\n2 6 5 1 4 3 7 8\n3 8 7 5 1 2 4 6\n", "2\n4\n1 4 2 3\n3 2 1 4\n8\n2 6 5 1 4 3 7 8\n3 8 7 5 1 2 4 6\n"], "outputs": ["2\n8\n", "\n2\n8\n"]}
574
131
coding
Solve the programming task below in a Python markdown code block. Chef wants to rent a car to travel to his restaurant which is N kilometers away. He can either rent a petrol car or a diesel car. One litre of petrol costs X rupees and one litre of diesel costs Y rupees. Chef can travel A kilometers with one litre of petrol and B kilometers with one litre of diesel. Chef can buy petrol and diesel in any amount, not necessarily integer. For example, if X = 95, Chef can buy half a litre of petrol by paying 95/2 = 47.5 rupees. Which car should the chef rent in order to minimize the cost of his travel? ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains 5 space-separated integers, N, X, Y, A, and B - distance that chef needs to travel, the per litre prices of petrol and diesel and the distance chef can travel using one litre of petrol and diesel respectively. ------ Output Format ------ For each test case, output on a new line PETROL if the cost of travel is less using petrol car, DIESEL if the cost of travel is less using diesel car or ANY if cost of travel is same in both the cases. You may print each character of the string in either uppercase or lowercase (for example, the strings PETrol, petrol, Petrol, and PETROL will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N,x,y,a,b ≤ 100$ ----- Sample Input 1 ------ 4 20 10 8 2 4 50 12 12 4 3 40 3 15 8 40 21 9 9 2 9 ----- Sample Output 1 ------ DIESEL PETROL ANY DIESEL ----- explanation 1 ------ Test case $1$: The cost of travelling by the petrol car will be $100$ rupees while that of using the diesel car will be $40$ rupees. Hence, diesel car is better. Test case $2$: The cost of travelling by the petrol car will be $150$ rupees while that of using the diesel car will be $200$ rupees. Hence, petrol car is better. Test case $3$: The cost of travel for petrol and diesel car will be $15$ rupees. Test case $4$: The cost of travelling by the petrol car will be $94.5$ rupees while that of using the diesel car will be $21$ rupees. Hence, diesel car is better.
{"inputs": ["4\n20 10 8 2 4\n50 12 12 4 3\n40 3 15 8 40\n21 9 9 2 9\n"], "outputs": ["DIESEL\nPETROL\nANY\nDIESEL\n"]}
589
74
coding
Solve the programming task below in a Python markdown code block. Most of the time when rounding a given number, it is customary to round to some multiple of a power of 10. However, there is no reason why we cannot use another multiple to do our rounding to. For example, you could round to the nearest multiple of 7, or the nearest multiple of 3. Given an integer N and an integer B, round N to the nearest value which is a multiple of B. If N is exactly halfway between two multiples of B, return the larger value. Input:- First line contains number of test cases and each test case two interger values N and B. Ouput:- Print the desired output. SAMPLE INPUT 3 5 10 4 10 100 3 SAMPLE OUTPUT 10 0 99 Explanation Testcase 3:-100 is closer to 99 than 102.
{"inputs": ["26\n5 10\n4 10\n100 3\n123456 7\n49 7\n158854 50\n822992 101\n691238 345\n682373 49\n643378 53\n328697 62\n446656 228\n368684 130\n530193 371\n864163 325\n708578 207\n915093 485\n711860 28\n234554 366\n309089 25\n595700 393\n10 6\n9 19\n4 10\n11 2\n11 3"], "outputs": ["10\n0\n99\n123459\n49\n158850\n822948\n691380\n682374\n643367\n328724\n446652\n368680\n530159\n864175\n708561\n915195\n711872\n234606\n309100\n595788\n12\n0\n0\n12\n12"]}
201
379
coding
Solve the programming task below in a Python markdown code block. A strange grid has been recovered from an old book. It has $5$ columns and infinite number of rows. The bottom row is considered as the first row. First few rows of the grid are like this: .............. .............. 20 22 24 26 28 11 13 15 17 19 10 12 14 16 18 1 3 5 7 9 0 2 4 6 8 The grid grows upwards forever! Your task is to find the integer in $\textbf{C}$^{th} column in $\textbf{r}$^{th} row of the grid. Input Format There will be two integers r and c separated by a single space. Constraints $1\leq r\leq2*10^9$ $1\leq c\leq5$ Rows are indexed from bottom to top and columns are indexed from left to right. Output Format Output the answer in a single line. Sample Input 6 3 Sample Output 25 Explanation The number in the 6^{th} row and 3^{rd} column is 25.
{"inputs": ["6 3\n"], "outputs": ["25\n"]}
286
17
coding
Solve the programming task below in a Python markdown code block. You are given an array $a$ consisting of $n$ integers. The array is sorted if $a_1 \le a_2 \le \dots \le a_n$. You want to make the array $a$ sorted by applying the following operation exactly once: choose an integer $x$, then for every $i \in [1, n]$, replace $a_i$ by $|a_i - x|$. Find any value of $x$ that will make the array sorted, or report that there is no such value. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^8$). Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print any integer $x$ ($0 \le x \le 10^9$) that makes the array sorted. It can be shown that if such an integer $x$ exists, there is at least one such integer between $0$ and $10^9$. If there is no such integer, then print $-1$. If there are multiple suitable values of $x$, print any of them. -----Examples----- Input 8 5 5 3 3 3 5 4 5 3 4 5 8 1 2 3 4 5 6 7 8 6 10 5 4 3 2 1 3 3 3 1 3 42 43 42 2 100000000 99999999 6 29613295 52036613 75100585 78027446 81409090 73215 Output 4 -1 0 42 2 -1 100000000 40741153 -----Note----- In the first test case, after using $x = 4$, the array becomes $[1, 1, 1, 1, 1]$. In the third test case, after using $x = 0$, the array becomes $[1, 2, 3, 4, 5, 6, 7, 8]$. In the fourth test case, after using $x = 42$, the array becomes $[32, 37, 38, 39, 40, 41]$.
{"inputs": ["2\n2\n1 2\n2\n1 2\n", "2\n3\n1 2 3\n3\n1 2 3\n", "8\n5\n5 3 3 3 5\n4\n5 3 4 5\n8\n1 2 3 4 5 6 7 8\n6\n10 5 4 3 2 1\n3\n3 3 1\n3\n42 43 42\n2\n100000000 99999999\n6\n29613295 52036613 75100585 78027446 81409090 73215\n"], "outputs": ["0\n0\n", "0\n0\n", "4\n-1\n0\n8\n2\n-1\n100000000\n40741153\n"]}
676
241
coding
Solve the programming task below in a Python markdown code block. Petya organized a strange birthday party. He invited $n$ friends and assigned an integer $k_i$ to the $i$-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are $m$ unique presents available, the $j$-th present costs $c_j$ dollars ($1 \le c_1 \le c_2 \le \ldots \le c_m$). It's not allowed to buy a single present more than once. For the $i$-th friend Petya can either buy them a present $j \le k_i$, which costs $c_j$ dollars, or just give them $c_{k_i}$ dollars directly. Help Petya determine the minimum total cost of hosting his party. -----Input----- The first input line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \leq n, m \leq 3 \cdot 10^5$) — the number of friends, and the number of unique presents available. The following line contains $n$ integers $k_1, k_2, \ldots, k_n$ ($1 \leq k_i \leq m$), assigned by Petya to his friends. The next line contains $m$ integers $c_1, c_2, \ldots, c_m$ ($1 \le c_1 \le c_2 \le \ldots \le c_m \le 10^9$) — the prices of the presents. It is guaranteed that sum of values $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of values $m$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case output a single integer — the minimum cost of the party. -----Examples----- Input 2 5 4 2 3 4 3 2 3 5 12 20 5 5 5 4 3 2 1 10 40 90 160 250 Output 30 190 Input 1 1 1 1 1 Output 1 -----Note----- In the first example, there are two test cases. In the first one, Petya has $5$ friends and $4$ available presents. Petya can spend only $30$ dollars if he gives $5$ dollars to the first friend. A present that costs $12$ dollars to the second friend. A present that costs $5$ dollars to the third friend. A present that costs $3$ dollars to the fourth friend. $5$ dollars to the fifth friend. In the second one, Petya has $5$ and $5$ available presents. Petya can spend only $190$ dollars if he gives A present that costs $10$ dollars to the first friend. A present that costs $40$ dollars to the second friend. $90$ dollars to the third friend. $40$ dollars to the fourth friend. $10$ dollars to the fifth friend.
{"inputs": ["1\n1 1\n1\n1\n", "1\n1 1\n1\n0\n", "1\n1 1\n1\n1\n", "2\n5 4\n2 3 4 3 2\n3 5 6 20\n5 5\n5 5 3 2 1\n7 40 90 160 250\n", "2\n5 4\n2 2 4 3 2\n3 5 6 20\n5 5\n5 5 3 2 1\n7 40 90 160 423\n", "2\n5 4\n2 4 4 1 2\n4 5 12 20\n5 5\n5 4 1 3 1\n7 40 90 91 160\n", "2\n5 4\n2 3 4 3 2\n3 5 9 20\n5 5\n5 5 3 2 1\n7 40 90 160 423\n", "2\n5 4\n2 4 4 2 2\n4 5 12 20\n5 5\n5 4 1 1 1\n7 40 90 91 160\n"], "outputs": ["1\n", "0\n", "\n1\n", "24\n184\n", "23\n184\n", "23\n151\n", "27\n184\n", "24\n68\n"]}
738
384
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.   Please complete the following python code precisely: ```python class Solution: def largest1BorderedSquare(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[1,1,1],[1,0,1],[1,1,1]]) == 9\n assert candidate(grid = [[1,1,0,0]]) == 1\n\n\ncheck(Solution().largest1BorderedSquare)"}
106
69
coding
Solve the programming task below in a Python markdown code block. Take a sentence (string) and reverse each word in the sentence. Do not reverse the order of the words, just the letters in each word. If there is punctuation, it should be interpreted as a regular character; no special rules. If there is spacing before/after the input string, leave them there. String will not be empty. ## Examples ``` "Hi mom" => "iH mom" " A fun little challenge! " => " A nuf elttil !egnellahc " ``` Also feel free to reuse/extend the following starter code: ```python def reverser(sentence): ```
{"functional": "_inputs = [['How now brown cow'], ['racecar'], ['Hi mom'], [' '], [' '], ['go away'], ['I like noodles'], ['The red pen wrote on the wall'], ['Green trucks drive fast'], ['Pink trucks drive slow']]\n_outputs = [['woH won nworb woc'], ['racecar'], ['iH mom'], [' '], [' '], ['og yawa'], ['I ekil seldoon'], ['ehT der nep etorw no eht llaw'], ['neerG skcurt evird tsaf'], ['kniP skcurt evird wols']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(reverser(*i), o[0])"}
140
276
coding
Solve the programming task below in a Python markdown code block. Gru wants to distribute $N$ bananas to $K$ minions on his birthday. Gru does not like to just give everyone the same number of bananas, so instead, he wants to distribute bananas in such a way that each minion gets a $distinct$ amount of bananas. That is, no two minions should get the same number of bananas. Gru also loves $gcd$. The higher the $gcd$, the happier Gru and the minions get. So help Gru in distributing the bananas in such a way that each Minion gets a distinct amount of bananas and gcd of this distribution is highest possible. Output this maximum gcd. If such a distribution is not possible output $-1$. Note: You have to distribute $all$ $N$ bananas. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase consists of a single line of input, which has two integers: $N, K$. -----Output:----- For each testcase, output in a single line the maximum gcd or -1. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N, K \leq 10^9$ -----Sample Input:----- 1 6 3 -----Sample Output:----- 1 -----EXPLANATION:----- The only possible distribution is $[1, 2, 3]$. So the answer is 1.
{"inputs": ["1\n6 3"], "outputs": ["1"]}
319
16
coding
Solve the programming task below in a Python markdown code block. Tim is visiting his grandma for two days and is bored due to the lack of the electricity over there. That's why he starts to play with grandma's colorful candle collection. He aligned the $N$ candles from left to right. The $\boldsymbol{i}$th candle from the left has the height $\boldsymbol{H}_i$ and the color $C_i$, an integer ranged from 1 to a given $\mbox{K}$, the number of colors. Now he stares at the sequence of candles and wonders, how many strictly increasing ( in height ) colorful subsequences are there? A subsequence is considered as colorful if every of the $\mbox{K}$ colors appears at least one times in the subsequence. As the number of subsequences fulfilling the requirement can be large, print the result modulo $10^9+7$. Input Format On the first line you will be given $N$ and $\mbox{K}$, then $N$ lines will follow. On the $\boldsymbol{i}$th line you will be given two integers $\boldsymbol{H}_i$ and $C_i$. Constraints $1\leq N\leq5\cdot10^4$ $1\leq C_i\leq K\leq7$ $1\leq H_i\leq5\cdot10^4$ Output Format Print the number of strictly increasing colorful subsequences modulo $10^9+7$. Sample Input 4 3 1 1 3 2 2 2 4 3 Sample Output 2 Explanation In the first sample the only two valid subsequences are (1, 2, 4) and (1, 3, 4).
{"inputs": ["4 3\n1 1\n3 2\n2 2\n4 3\n"], "outputs": ["2\n"]}
385
32
coding
Solve the programming task below in a Python markdown code block. JJ loves playing with averages. He has an array A of length N. He wants to divide the array A into two non-empty subsets P and Q such that the value of mean(P) + mean(Q) is as large as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this maximum value of mean(P) + mean(Q). As a reminder, the mean of a subset X of size M is defined as: mean(X) = \dfrac{\sum_{i = 1}^{M} X_{i}}{M}. For example, mean([3, 1, 4, 5]) = \frac{3 + 1 + 4 + 5}{4} = 3.25. ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the largest value of mean(P) + mean(Q). Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}. Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{\max{(1, |b|)}} ≤ 10^{-6}. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 1000$ $1 ≤ A_{i} ≤ 10^{6}$ ----- Sample Input 1 ------ 2 2 4 5 5 2 2 2 2 2 ----- Sample Output 1 ------ 9.000000 4.000000 ----- explanation 1 ------ *Test case-1:* We can divide the two elements into two non-empty subsets $P$ and $Q$ as follows: $P = [4]$, $Q = [5]$. Therefore, $mean(P) + mean(Q) = 9$. *Test case-2:* In whatever way we divide the elements of the array, mean of both the subsets will always be $2$. Therefore, $mean(P) + mean(Q) = 4$.
{"inputs": ["2\n2\n4 5\n5\n2 2 2 2 2\n"], "outputs": ["9.000000\n4.000000\n"]}
540
48
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given the root of a binary tree and a positive integer k. The level sum in the tree is the sum of the values of the nodes that are on the same level. Return the kth largest level sum in the tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1. Note that two nodes are on the same level if they have the same distance from the root.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([5,8,9,2,1,3,7,4,6]), k = 2) == 13\n assert candidate(root = tree_node([1,2,None,3]), k = 1) == 3\n\n\ncheck(Solution().kthLargestLevelSum)"}
194
83
coding
Solve the programming task below in a Python markdown code block. You are given an array of $n$ non-negative integers $a_1, a_2, \ldots, a_n$. You can make the following operation: choose an integer $x \geq 2$ and replace each number of the array by the remainder when dividing that number by $x$, that is, for all $1 \leq i \leq n$ set $a_i$ to $a_i mod x$. Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^5$) — the length of the array. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$) where $a_i$ is the $i$-th element of the array. The sum of $n$ for all test cases is at most $2 \cdot 10^5$. -----Output----- For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer). -----Examples----- Input 4 4 2 5 6 8 3 1 1 1 5 4 1 7 0 8 4 5 9 17 5 Output YES YES NO YES -----Note----- In the first test case, one can apply the operation with $x = 3$ to obtain the array $[2, 2, 0, 2]$, and then apply the operation with $x = 2$ to obtain $[0, 0, 0, 0]$. In the second test case, all numbers are already equal. In the fourth test case, applying the operation with $x = 4$ results in the array $[1, 1, 1, 1]$.
{"inputs": ["4\n4\n2 5 6 8\n3\n1 1 1\n5\n4 1 7 0 8\n4\n5 9 17 5\n"], "outputs": ["YES\nYES\nNO\nYES\n"]}
541
61
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, return the number of all the arithmetic subsequences of nums. A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, [1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences. For example, [1, 1, 2, 5, 7] is not an arithmetic sequence. A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array. For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10]. The test cases are generated so that the answer fits in 32-bit integer.   Please complete the following python code precisely: ```python class Solution: def numberOfArithmeticSlices(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,4,6,8,10]) == 7\n assert candidate(nums = [7,7,7,7,7]) == 16\n\n\ncheck(Solution().numberOfArithmeticSlices)"}
244
64
coding
Solve the programming task below in a Python markdown code block. Fennec and Snuke are playing a board game. On the board, there are N cells numbered 1 through N, and N-1 roads, each connecting two cells. Cell a_i is adjacent to Cell b_i through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree. Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn: - Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black. - Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white. A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally. -----Constraints----- - 2 \leq N \leq 10^5 - 1 \leq a_i, b_i \leq N - The given graph is a tree. -----Input----- Input is given from Standard Input in the following format: N a_1 b_1 : a_{N-1} b_{N-1} -----Output----- If Fennec wins, print Fennec; if Snuke wins, print Snuke. -----Sample Input----- 7 3 6 1 2 3 1 7 4 5 7 1 4 -----Sample Output----- Fennec For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.
{"inputs": ["4\n1 4\n4 3\n2 3", "4\n1 2\n4 2\n2 3", "4\n1 4\n4 3\n2 1", "4\n1 4\n4 3\n2 2", "4\n1 4\n4 2\n2 2", "4\n1 3\n4 2\n2 3", "4\n1 4\n4 2\n4 2", "4\n1 3\n4 2\n2 1"], "outputs": ["Snuke\n", "Fennec\n", "Snuke\n", "Snuke\n", "Snuke\n", "Snuke\n", "Snuke\n", "Fennec\n"]}
411
174
coding
Solve the programming task below in a Python markdown code block. According to the regulations of Berland's army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn't differ much, their heights can differ by at most d centimeters. Captain Bob has n soldiers in his detachment. Their heights are a1, a2, ..., an centimeters. Some soldiers are of the same height. Bob wants to know, how many ways exist to form a reconnaissance unit of two soldiers from his detachment. Ways (1, 2) and (2, 1) should be regarded as different. Input The first line contains two integers n and d (1 ≤ n ≤ 1000, 1 ≤ d ≤ 109) — amount of soldiers in Bob's detachment and the maximum allowed height difference respectively. The second line contains n space-separated integers — heights of all the soldiers in Bob's detachment. These numbers don't exceed 109. Output Output one number — amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn't exceed d. Examples Input 5 10 10 20 50 60 65 Output 6 Input 5 1 55 30 29 31 55 Output 6
{"inputs": ["6 10\n4 6 4 1 9 3\n", "6 10\n4 3 4 1 9 3\n", "6 10\n4 3 4 2 9 3\n", "6 10\n4 3 2 2 9 3\n", "6 10\n4 3 0 2 9 3\n", "6 10\n5 6 4 1 9 3\n", "5 1\n24 30 18 9 76\n", "5 1\n24 28 18 9 76\n"], "outputs": ["30\n", "30\n", "30\n", "30\n", "30\n", "30\n", "0\n", "0\n"]}
281
198
coding
Solve the programming task below in a Python markdown code block. Our world is one-dimensional, and ruled by two empires called Empire A and Empire B. The capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y. One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control. If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out. - X < Z \leq Y - x_1, x_2, ..., x_N < Z - y_1, y_2, ..., y_M \geq Z Determine if war will break out. -----Constraints----- - All values in input are integers. - 1 \leq N, M \leq 100 - -100 \leq X < Y \leq 100 - -100 \leq x_i, y_i \leq 100 - x_1, x_2, ..., x_N \neq X - x_i are all different. - y_1, y_2, ..., y_M \neq Y - y_i are all different. -----Input----- Input is given from Standard Input in the following format: N M X Y x_1 x_2 ... x_N y_1 y_2 ... y_M -----Output----- If war will break out, print War; otherwise, print No War. -----Sample Input----- 3 2 10 20 8 15 13 16 22 -----Sample Output----- No War The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement. - X = 10 < 16 \leq 20 = Y - 8, 15, 13 < 16 - 16, 22 \geq 16
{"inputs": ["1 1 -100 100\n-99\n99\n", "1 1 -100 100\n99\n-99\n", "3 2 10 40\n8 1 15\n16 22", "3 2 10 40\n8 1 15\n16 23", "3 0 10 40\n8 1 15\n16 23", "0 0 10 40\n8 1 15\n16 23", "3 3 10 40\n8 1 15\n16 23", "3 0 10 61\n8 1 15\n16 23"], "outputs": ["No War\n", "War\n", "No War\n", "No War\n", "No War\n", "No War\n", "No War\n", "No War\n"]}
467
231
coding
Solve the programming task below in a Python markdown code block. There is a group of N friends who wish to enroll in a course together. The course has a maximum capacity of M students that can register for it. If there are K other students who have already enrolled in the course, determine if it will still be possible for all the N friends to do so or not. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - Each test case consists of a single line containing three integers N, M and K - the size of the friend group, the capacity of the course and the number of students already registered for the course. ------ Output Format ------ For each test case, output Yes if it will be possible for all the N friends to register for the course. Otherwise output No. You may print each character of Yes and No in uppercase or lowercase (for example, yes, yEs, YES will be considered identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ M ≤ 100$ $0 ≤ K ≤ M$ ----- Sample Input 1 ------ 3 2 50 27 5 40 38 100 100 0 ----- Sample Output 1 ------ Yes No Yes ----- explanation 1 ------ Test Case 1: The $2$ friends can enroll in the course as it has enough seats to accommodate them and the $27$ other students at the same time. Test Case 2: The course does not have enough seats to accommodate the $5$ friends and the $38$ other students at the same time.
{"inputs": ["3\n2 50 27\n5 40 38\n100 100 0\n"], "outputs": ["Yes\nNo\nYes\n"]}
360
44
coding
Solve the programming task below in a Python markdown code block. Hofstadter sequences are a family of related integer sequences, among which the first ones were described by an American professor Douglas Hofstadter in his book Gödel, Escher, Bach. ### Task Today we will be implementing the rather chaotic recursive sequence of integers called Hofstadter Q. The Hofstadter Q is defined as: As the author states in the aforementioned book:It is reminiscent of the Fibonacci definition in that each new value is a sum of two previous values-but not of the immediately previous two values. Instead, the two immediately previous values tell how far to count back to obtain the numbers to be added to make the new value. The function produces the starting sequence: `1, 1, 2, 3, 3, 4, 5, 5, 6 . . .` Test info: 100 random tests, n is always positive Good luck! Also feel free to reuse/extend the following starter code: ```python def hofstadter_Q(n): ```
{"functional": "_inputs = [[1], [3], [7], [10], [100], [1000]]\n_outputs = [[1], [2], [5], [6], [56], [502]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(hofstadter_Q(*i), o[0])"}
231
196
coding
Solve the programming task below in a Python markdown code block. ##Overview Write a helper function that takes in a Time object and converts it to a more human-readable format. You need only go up to '_ weeks ago'. ```python to_pretty(0) => "just now" to_pretty(40000) => "11 hours ago" ``` ##Specifics - The output will be an amount of time, t, included in one of the following phrases: "just now", "[t] seconds ago", "[t] minutes ago", "[t] hours ago", "[t] days ago", "[t] weeks ago". - You will have to handle the singular cases. That is, when t = 1, the phrasing will be one of "a second ago", "a minute ago", "an hour ago", "a day ago", "a week ago". - The amount of time is always rounded down to the nearest integer. For example, if the amount of time is actually 11.73 hours ago, the return value will be "11 hours ago". - Only times in the past will be given, with the range "just now" to "52 weeks ago" Also feel free to reuse/extend the following starter code: ```python def to_pretty(seconds): ```
{"functional": "_inputs = [[0], [1], [30], [90], [180], [4000], [40000], [140000], [400000], [1000000], [10000000]]\n_outputs = [['just now'], ['a second ago'], ['30 seconds ago'], ['a minute ago'], ['3 minutes ago'], ['an hour ago'], ['11 hours ago'], ['a day ago'], ['4 days ago'], ['a week ago'], ['16 weeks ago']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(to_pretty(*i), o[0])"}
278
273
coding
Solve the programming task below in a Python markdown code block. problem There are n cards with one integer from 1 to n and one blank card. Of these n + 1 cards, k cards are given, but 1 ≤ k ≤ n. You can write one integer from 1 to n on a blank card. I want to make a continuous sequence of integers as long as possible with just a given card. Write a program that outputs the maximum length of a contiguous sequence of integers that can be made from a given card when the given card is entered. input The input consists of multiple datasets. Each dataset is given in the following format. The input ends on a line containing two zeros. On the first line, two integers n (1 ≤ n ≤ 100000) and k (1 ≤ k ≤ n) are written in this order, separated by one blank. The following k line contains one integer. Written one by one, representing the integers written on the given k cards. Blank cards are represented by 0. Of the scoring data, 40% of the points are 1 ≤ n ≤ 1000, 1 ≤ k ≤ 500, 20% of the points are 1 ≤ n ≤ 60000, 1 ≤ k ≤ 50000. Satisfy 1 ≤ n ≤ 100000, 1 ≤ k ≤ 100000. The number of datasets does not exceed 5. output Outputs an integer on one line for each dataset. Examples Input 7 5 6 2 4 7 1 7 5 6 2 0 4 7 0 0 Output 2 4 Input None Output None
{"inputs": ["7 5\n6\n2\n4\n7\n1\n7 5\n5\n2\n0\n4\n7\n0 0", "7 5\n6\n2\n4\n7\n1\n7 5\n5\n2\n0\n1\n7\n0 0", "9 5\n6\n2\n4\n7\n1\n7 5\n5\n2\n1\n4\n7\n0 0", "7 5\n6\n2\n3\n7\n1\n7 5\n6\n2\n0\n4\n7\n0 0", "7 5\n6\n2\n4\n7\n1\n7 5\n5\n2\n0\n4\n6\n0 0", "9 5\n6\n2\n4\n3\n1\n7 5\n5\n2\n1\n4\n7\n0 0", "9 5\n6\n3\n4\n7\n0\n7 5\n5\n4\n0\n0\n7\n0 0", "7 5\n6\n2\n4\n7\n1\n8 5\n5\n2\n0\n1\n7\n0 0"], "outputs": ["2\n4\n", "2\n3\n", "2\n2\n", "3\n4\n", "2\n5\n", "4\n2\n", "5\n4\n", "2\n3\n"]}
382
318
coding
Solve the programming task below in a Python markdown code block. You are given a grid with $R$ rows (numbered $1$ through $R$) and $C$ columns (numbered $1$ through $C$). Initially, each cell of this grid is either empty, contains an ant or an anteater. Each ant is moving in a fixed direction: up, down, left or right. The anteaters do not move. The movement of ants happens in discrete steps. For example, when an ant is in the cell in the $i$-th row and $j$-th column at some point in time (in some step) and it is moving down, then in the next step, it enters the cell in the $(i+1)$-th row and $j$-th column. Two ants meet each other when they enter the same cell at the same point in time (in the same step). When ants meet, they do not interact in any way and keep moving in their fixed directions. If an ant reaches an anteater, that anteater eats the ant, so the ant completely disappears. If an ant attempts to leave the grid, it also disappears. When two ants enter a cell containing an anteater at the same time, they are eaten before they could meet. Calculate the total number of pairs of ants that meet each other. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $R$ and $C$. - Each of the following $R$ lines contains a single string with length $C$. For each valid $i, j$, the $j$-th character in the $i$-th string is: - '#' if the cell in the $i$-th row and $j$-th column of the grid contains an anteater - 'U', 'D', 'L' or 'R' if this cell contains an ant moving up, down, left or right respectively - '-' if this cell is empty -----Output----- For each test case, print a single line containing one integer — the number of pairs of ants that meet. -----Constraints----- - $1 \le T \le 10$ - $1 \le R, C \le 50$ - each string contains only characters 'U', 'D', 'L', 'R', '#' and '-' -----Example Input----- 10 3 3 R-- --- --U 1 4 R--R 2 2 -- -- 1 4 R--L 1 4 -R-L 1 4 -R#L 3 3 R-D -#- R-U 3 3 R-D --- R#U 3 3 -D- R-L -U- 1 7 RLLLLLL -----Example Output----- 1 0 0 0 1 0 3 2 6 3
{"inputs": ["10\n3 3\nR--\n---\n--U\n1 4\nR--R\n2 2\n--\n--\n1 4\nR--L\n1 4\n-R-L\n1 4\n-R#L\n3 3\nR-D\n-#-\nR-U\n3 3\nR-D\n---\nR#U\n3 3\n-D-\nR-L\n-U-\n1 7\nRLLLLLL\n"], "outputs": ["1\n0\n0\n0\n1\n0\n3\n2\n6\n3"]}
645
136
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a positive integer n. Let even denote the number of even indices in the binary representation of n with value 1. Let odd denote the number of odd indices in the binary representation of n with value 1. Note that bits are indexed from right to left in the binary representation of a number. Return the array [even, odd].   Please complete the following python code precisely: ```python class Solution: def evenOddBit(self, n: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(n = 17) == [2,0]\n assert candidate(n = 2) == [0,1]\n\n\ncheck(Solution().evenOddBit)"}
120
51
coding
Solve the programming task below in a Python markdown code block. There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters. The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task. Input The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters. Output In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters. In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work. Examples Input 3 3 14 12 10 Output 2 2 1 2 2 3 Input 2 0 10 10 Output 2 1 1 2 Input 4 5 8 19 10 13 Output 2 1 3 4
{"inputs": ["1 1\n1\n", "1 2\n1\n", "2 1\n9 13\n", "2 2\n9 13\n", "3 3\n3 8 6\n", "2 0\n17 10\n", "2 0\n17 13\n", "2 1\n17 13\n"], "outputs": ["1 1\n1 1\n", "1 1\n1 1\n", "1 2\n1 1\n2 2\n", "1 2\n1 1\n2 2\n", "2 1\n2 3\n", "1 2\n1 1\n2 2\n", "1 2\n1 1\n2 2\n", "1 2\n1 1\n2 2\n"]}
526
192
coding
Solve the programming task below in a Python markdown code block. This contest is `CODEFESTIVAL`, which can be shortened to the string `CF` by deleting some characters. Mr. Takahashi, full of curiosity, wondered if he could obtain `CF` from other strings in the same way. You are given a string s consisting of uppercase English letters. Determine whether the string `CF` can be obtained from the string s by deleting some characters. Constraints * 2 ≤ |s| ≤ 100 * All characters in s are uppercase English letters (`A`-`Z`). Input The input is given from Standard Input in the following format: s Output Print `Yes` if the string `CF` can be obtained from the string s by deleting some characters. Otherwise print `No`. Examples Input CODEFESTIVAL Output Yes Input FESTIVALCODE Output No Input CF Output Yes Input FCF Output Yes
{"inputs": ["FC", "DF", "EF", "FD", "FE", "GE", "GD", "ED"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
211
62
coding
Solve the programming task below in a Python markdown code block. Joi has an array $a$ of $n$ positive integers. Koxia wants you to determine whether there exists a positive integer $x > 0$ such that $\gcd(a_i+x,a_j+x)=1$ for all $1 \leq i < j \leq n$. Here $\gcd(y, z)$ denotes the greatest common divisor (GCD) of integers $y$ and $z$. -----Input----- Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains an integer $n$ ($2 \leq n \leq 100$) — the size of the array. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq {10}^{18}$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$. -----Output----- For each test case, output "YES" (without quotes) if there exists a positive integer $x$ such that $\gcd(a_i+x,a_j+x)=1$ for all $1 \leq i < j \leq n$, and "NO" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. -----Examples----- Input 2 3 5 7 10 3 3 3 4 Output YES NO -----Note----- In the first test case, we can set $x = 4$. This is valid because: When $i=1$ and $j=2$, $\gcd(a_i+x,a_j+x)=\gcd(5+4,7+4)=\gcd(9,11)=1$. When $i=1$ and $j=3$, $\gcd(a_i+x,a_j+x)=\gcd(5+4,10+4)=\gcd(9,14)=1$. When $i=2$ and $j=3$, $\gcd(a_i+x,a_j+x)=\gcd(7+4,10+4)=\gcd(11,14)=1$. In the second test case, any choice of $x$ makes $\gcd(a_1 + x, a_2 + x) = \gcd(3+x,3+x)=3+x$. Therefore, no such $x$ exists.
{"inputs": ["2\n3\n5 7 10\n3\n3 3 4\n", "1\n2\n4294967296 4294967296\n"], "outputs": ["YES\nNO\n", "NO\n"]}
593
65
coding
Solve the programming task below in a Python markdown code block. Chef has $N$ small boxes arranged on a line from $1$ to $N$. For each valid $i$, the weight of the $i$-th box is $W_i$. Chef wants to bring them to his home, which is at the position $0$. He can hold any number of boxes at the same time; however, the total weight of the boxes he's holding must not exceed K at any time, and he can only pick the ith box if all the boxes between Chef's home and the ith box have been either moved or picked up in this trip. Therefore, Chef will pick up boxes and carry them home in one or more round trips. Find the smallest number of round trips he needs or determine that he cannot bring all boxes home. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $W_1, W_2, \ldots, W_N$. -----Output----- For each test case, print a single line containing one integer ― the smallest number of round trips or $-1$ if it is impossible for Chef to bring all boxes home. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, K \le 10^3$ - $1 \le W_i \le 10^3$ for each valid $i$ -----Example Input----- 4 1 1 2 2 4 1 1 3 6 3 4 2 3 6 3 4 3 -----Example Output----- -1 1 2 3 -----Explanation----- Example case 1: Since the weight of the box higher than $K$, Chef can not carry that box home in any number of the round trip. Example case 2: Since the sum of weights of both boxes is less than $K$, Chef can carry them home in one round trip. Example case 3: In the first round trip, Chef can only pick up the box at position $1$. In the second round trip, he can pick up both remaining boxes at positions $2$ and $3$. Example case 4: Chef can only carry one box at a time, so three round trips are required.
{"inputs": ["4\n1 1\n2\n2 4\n1 1\n3 6\n3 4 2\n3 6\n3 4 3"], "outputs": ["-1\n1\n2\n3"]}
529
53
coding
Solve the programming task below in a Python markdown code block. The life goes up and down, just like nice sequences. Sequence t_1, t_2, ..., t_{n} is called nice if the following two conditions are satisfied: t_{i} < t_{i} + 1 for each odd i < n; t_{i} > t_{i} + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not. Bear Limak has a sequence of positive integers t_1, t_2, ..., t_{n}. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements t_{i} and t_{j} in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different. -----Input----- The first line of the input contains one integer n (2 ≤ n ≤ 150 000) — the length of the sequence. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 150 000) — the initial sequence. It's guaranteed that the given sequence is not nice. -----Output----- Print the number of ways to swap two elements exactly once in order to get a nice sequence. -----Examples----- Input 5 2 8 4 7 7 Output 2 Input 4 200 150 100 50 Output 1 Input 10 3 2 1 4 1 4 1 4 1 4 Output 8 Input 9 1 2 3 4 5 6 7 8 9 Output 0 -----Note----- In the first sample, there are two ways to get a nice sequence with one swap: Swap t_2 = 8 with t_4 = 7. Swap t_1 = 2 with t_5 = 7. In the second sample, there is only one way — Limak should swap t_1 = 200 with t_4 = 50.
{"inputs": ["2\n5 1\n", "2\n5 1\n", "2\n5 2\n", "2\n5 4\n", "2\n10 10\n", "2\n10 10\n", "5\n2 8 4 7 7\n", "5\n1 1 1 4 3\n"], "outputs": ["1\n", "1", "1\n", "1\n", "0\n", "0", "2\n", "1\n"]}
559
116
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer n. There is a complete binary tree with 2n - 1 nodes. The root of that tree is the node with the value 1, and every node with a value val in the range [1, 2n - 1 - 1] has two children where: The left node has the value 2 * val, and The right node has the value 2 * val + 1. You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, solve the following problem: Add an edge between the nodes with values ai and bi. Find the length of the cycle in the graph. Remove the added edge between nodes with values ai and bi. Note that: A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once. The length of a cycle is the number of edges visited in the cycle. There could be multiple edges between two nodes in the tree after adding the edge of the query. Return an array answer of length m where answer[i] is the answer to the ith query.   Please complete the following python code precisely: ```python class Solution: def cycleLengthQueries(self, n: int, queries: List[List[int]]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, queries = [[5,3],[4,7],[2,3]]) == [4,5,3]\n assert candidate(n = 2, queries = [[1,2]]) == [2]\n\n\ncheck(Solution().cycleLengthQueries)"}
295
72
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, you have two types of operation: Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists). Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (if exists). Your task is to minimize the length of s by performing the above operations zero or more times. Return an integer denoting the length of the minimized string.   Please complete the following python code precisely: ```python class Solution: def minimizedStringLength(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"aaabc\") == 3\n assert candidate(s = \"cbbd\") == 3\n assert candidate(s = \"dddaaa\") == 2\n\n\ncheck(Solution().minimizedStringLength)"}
156
61
coding
Solve the programming task below in a Python markdown code block. Error Handling is very important in coding and seems to be overlooked or not implemented properly. #Task Your task is to implement a function which takes a string as input and return an object containing the properties vowels and consonants. The vowels property must contain the total count of vowels {a,e,i,o,u}, and the total count of consonants {a,..,z} - {a,e,i,o,u}. Handle invalid input and don't forget to return valid ones. #Input The input is any random string. You must then discern what are vowels and what are consonants and sum for each category their total occurrences in an object. However you could also receive inputs that are not strings. If this happens then you must return an object with a vowels and consonants total of 0 because the input was NOT a string. Refer to the Example section for a more visual representation of which inputs you could receive and the outputs expected. :) Example: ```python Input: get_count('test') Output: {vowels:1,consonants:3} Input: get_count('tEst') Output: {vowels:1,consonants:3} Input get_count(' ') Output: {vowels:0,consonants:0} Input get_count() Output: {vowels:0,consonants:0} ``` C# A Counter class has been put in the preloaded section taking two parameters Vowels and Consonants this must be the Object you return! Also feel free to reuse/extend the following starter code: ```python def get_count(words=''): ```
{"functional": "_inputs = [['Test'], ['Here is some text'], ['To be a Codewarrior or not to be'], ['To Kata or not to Kata'], ['aeiou'], ['TEst'], ['HEre Is sOme text'], [['To Kata or not to Kata']], [None], ['Test '], ['Here is some text '], [' '], [{'jjjjj': 'jjjjj'}]]\n_outputs = [[{'vowels': 1, 'consonants': 3}], [{'vowels': 6, 'consonants': 8}], [{'vowels': 12, 'consonants': 13}], [{'vowels': 8, 'consonants': 9}], [{'vowels': 5, 'consonants': 0}], [{'vowels': 1, 'consonants': 3}], [{'vowels': 6, 'consonants': 8}], [{'vowels': 0, 'consonants': 0}], [{'vowels': 0, 'consonants': 0}], [{'vowels': 1, 'consonants': 3}], [{'vowels': 6, 'consonants': 8}], [{'vowels': 0, 'consonants': 0}], [{'vowels': 0, 'consonants': 0}]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_count(*i), o[0])"}
350
446
coding
Solve the programming task below in a Python markdown code block. For a set $S$ of integers, perform a sequence of the following operations. Note that each value in $S$ must be unique. * insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation. * find($x$): Report the number of $x$ in $S$ (0 or 1). Constraints * $1 \leq q \leq 200,000$ * $0 \leq x \leq 1,000,000,000$ Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $x$ or 1 $x$ where the first digits 0 and 1 represent insert and find operations respectively. Output For each insert operation, print the number of elements in $S$. For each find operation, print the number of specified elements in $S$. Example Input 7 0 1 0 2 0 3 0 2 0 4 1 3 1 10 Output 1 2 3 3 4 1 0
{"inputs": ["7\n1 0\n0 1\n0 3\n0 0\n1 0\n0 7\n1 0", "7\n1 0\n0 1\n1 2\n0 0\n1 0\n0 7\n1 0", "7\n0 0\n0 2\n0 3\n0 2\n0 4\n0 3\n1 4", "7\n1 0\n0 1\n0 2\n0 0\n1 0\n1 7\n1 0", "7\n0 2\n0 2\n0 1\n0 0\n1 4\n1 3\n0 0", "7\n0 0\n1 3\n1 0\n0 0\n0 4\n1 3\n1 9", "7\n1 0\n0 2\n1 2\n0 0\n1 0\n0 7\n1 0", "7\n0 0\n0 2\n0 1\n0 0\n0 0\n1 0\n1 2"], "outputs": ["0\n1\n2\n3\n1\n4\n1\n", "0\n1\n0\n2\n1\n3\n1\n", "1\n2\n3\n3\n4\n4\n1\n", "0\n1\n2\n3\n1\n0\n1\n", "1\n1\n2\n3\n0\n0\n3\n", "1\n0\n1\n1\n2\n0\n0\n", "0\n1\n1\n2\n1\n3\n1\n", "1\n2\n3\n3\n3\n1\n1\n"]}
286
382
coding
Solve the programming task below in a Python markdown code block. A number is simply made up of digits. The number 1256 is made up of the digits 1, 2, 5, and 6. For 1256 there are 24 distinct permutations of the digits: 1256, 1265, 1625, 1652, 1562, 1526, 2156, 2165, 2615, 2651, 2561, 2516, 5126, 5162, 5216, 5261, 5621, 5612, 6125, 6152, 6251, 6215, 6521, 6512. Your goal is to write a program that takes a number, n, and returns the average value of all distinct permutations of the digits in n. Your answer should be rounded to the nearest integer. For the example above the return value would be 3889. * n will never be negative A few examples: ```python permutation_average(2) return 2 permutation_average(25) >>> 25 + 52 = 77 >>> 77 / 2 = 38.5 return 39 * permutation_average(20) >>> 20 + 02 = 22 >>> 22 / 2 = 11 return 11 permutation_average(737) >>> 737 + 377 + 773 = 1887 >>> 1887 / 3 = 629 return 629 ``` Note: Your program should be able to handle numbers up to 6 digits long ~~~if:python \* Python version 3 and above uses Banker Rounding so the expected values for those tests would be 3888 and 38 respectively ~~~ ~~~if-not:python \* ignore these marks, they're for Python only ~~~ Also feel free to reuse/extend the following starter code: ```python def permutation_average(n): ```
{"functional": "_inputs = [[2], [25], [737]]\n_outputs = [[2], [38], [629]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(permutation_average(*i), o[0])"}
522
173
coding
Solve the programming task below in a Python markdown code block. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are: 2332 110011 54322345 For a given number `num`, write a function to test if it's a numerical palindrome or not and return a boolean (true if it is and false if not). ```if-not:haskell Return "Not valid" if the input is not an integer or less than `0`. ``` ```if:haskell Return `Nothing` if the input is less than `0` and `Just True` or `Just False` otherwise. ``` Other Kata in this Series: Numerical Palindrome #1 Numerical Palindrome #1.5 Numerical Palindrome #2 Numerical Palindrome #3 Numerical Palindrome #3.5 Numerical Palindrome #4 Numerical Palindrome #5 Also feel free to reuse/extend the following starter code: ```python def palindrome(num): ```
{"functional": "_inputs = [[1221], [110011], [1456009006541], [123322], [1], [152], [9999], ['ACCDDCCA'], ['@14AbC'], ['1221'], [-450]]\n_outputs = [[True], [True], [True], [False], [True], [False], [True], ['Not valid'], ['Not valid'], ['Not valid'], ['Not valid']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(palindrome(*i), o[0])"}
237
260
coding
Solve the programming task below in a Python markdown code block. In this Kata, you will write a function `doubles` that will remove double string characters that are adjacent to each other. For example: `doubles('abbcccdddda') = 'aca'`, because, from left to right: ```Haskell a) There is only one 'a' on the left hand side, so it stays. b) The 2 b's disappear because we are removing double characters that are adjacent. c) Of the 3 c's, we remove two. We are only removing doubles. d) The 4 d's all disappear, because we first remove the first double, and again we remove the second double. e) There is only one 'a' at the end, so it stays. ``` Two more examples: `doubles('abbbzz') = 'ab'` and `doubles('abba') = ""`. In the second example, when we remove the b's in `'abba'`, the double `a` that results is then removed. The strings will contain lowercase letters only. More examples in the test cases. Good luck! Also feel free to reuse/extend the following starter code: ```python def doubles(s): ```
{"functional": "_inputs = [['abbbzz'], ['zzzzykkkd'], ['abbcccdddda'], ['vvvvvoiiiiin'], ['rrrmooomqqqqj'], ['xxbnnnnnyaaaaam'], ['qqqqqqnpppgooooonpppppqmmmmmc'], ['qqqqqwwwx'], ['jjjfzzzzzzsddgrrrrru'], ['jjjjjfuuuutgggggqppdaaas'], ['iiiiibllllllyqqqqqbiiiiiituuf'], ['mmmmmmuzzqllllmqqqp']]\n_outputs = [['ab'], ['ykd'], ['aca'], ['voin'], ['rmomj'], ['bnyam'], ['npgonpqmc'], ['qwx'], ['jfsgru'], ['jftgqdas'], ['ibyqbtf'], ['uqmqp']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(doubles(*i), o[0])"}
265
332
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed string array words. Two strings are similar if they consist of the same characters. For example, "abca" and "cba" are similar since both consist of characters 'a', 'b', and 'c'. However, "abacba" and "bcfd" are not similar since they do not consist of the same characters. Return the number of pairs (i, j) such that 0 <= i < j <= word.length - 1 and the two strings words[i] and words[j] are similar.   Please complete the following python code precisely: ```python class Solution: def similarPairs(self, words: List[str]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"aba\",\"aabb\",\"abcd\",\"bac\",\"aabc\"]) == 2\n assert candidate(words = [\"aabb\",\"ab\",\"ba\"]) == 3\n assert candidate(words = [\"nba\",\"cba\",\"dba\"]) == 0\n\n\ncheck(Solution().similarPairs)"}
163
81
coding
Solve the programming task below in a Python markdown code block. Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task. Consider two infinite sets of numbers. The first set consists of odd positive numbers ($1, 3, 5, 7, \ldots$), and the second set consists of even positive numbers ($2, 4, 6, 8, \ldots$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. The ten first written numbers: $1, 2, 4, 3, 5, 7, 9, 6, 8, 10$. Let's number the numbers written, starting with one. The task is to find the sum of numbers with numbers from $l$ to $r$ for given integers $l$ and $r$. The answer may be big, so you need to find the remainder of the division by $1000000007$ ($10^9+7$). Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem. -----Input----- The first line contains two integers $l$ and $r$ ($1 \leq l \leq r \leq 10^{18}$) — the range in which you need to find the sum. -----Output----- Print a single integer — the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 1 3 Output 7 Input 5 14 Output 105 Input 88005553535 99999999999 Output 761141116 -----Note----- In the first example, the answer is the sum of the first three numbers written out ($1 + 2 + 4 = 7$). In the second example, the numbers with numbers from $5$ to $14$: $5, 7, 9, 6, 8, 10, 12, 14, 16, 18$. Their sum is $105$.
{"inputs": ["1 3\n", "1 1\n", "1 1\n", "1 3\n", "5 14\n", "1 15\n", "1 15\n", "1 28\n"], "outputs": ["7", "1", "1\n", "7\n", "105", "135", "135\n", "434\n"]}
600
94
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are asked to cut off all the trees in a forest for a golf event. The forest is represented as an m x n matrix. In this matrix: 0 means the cell cannot be walked through. 1 represents an empty cell that can be walked through. A number greater than 1 represents a tree in a cell that can be walked through, and this number is the tree's height. In one step, you can walk in any of the four directions: north, east, south, and west. If you are standing in a cell with a tree, you can choose whether to cut it off. You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value at its cell becomes 1 (an empty cell). Starting from the point (0, 0), return the minimum steps you need to walk to cut off all the trees. If you cannot cut off all the trees, return -1. Note: The input is generated such that no two trees have the same height, and there is at least one tree needs to be cut off.   Please complete the following python code precisely: ```python class Solution: def cutOffTree(self, forest: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(forest = [[1,2,3],[0,0,4],[7,6,5]]) == 6\n assert candidate(forest = [[1,2,3],[0,0,0],[7,6,5]]) == -1\n assert candidate(forest = [[2,3,4],[0,0,5],[8,7,6]]) == 6\n\n\ncheck(Solution().cutOffTree)"}
272
107
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef Dobby loves playing games, especially games related to numbers. So, today Bhuvan gave him a new game. The description of the game goes as follows : Consider an array A of N non-negative integers. You can do the following move any number of times, pick two numbers from the array (x, y), such that x ≥ y and convert them into (x - y, 2 * y). The aim of the game is to make the array contains exactly (N - 1) zeros. If Dobby achieves this, after any number of steps, he wins the game. Bhuvan wants to know the odds to choose an initial array satisfying for every index i, the condition 0 ≤ A[i] ≤ B[i] where B is given in input and Dobby wins the game with such an array. So please help him counting the number of such arrays, since the answer can be very large, output the answer modulo 10^{9} + 7. ------ Input section ------ The first line contains N, denoting the number of elements in the array. Next line contains N space separated integers, denoting the elements of the array. ------ Output section ------ Output the number of possible winning arrays modulo 10^{9} + 7. ------ Input constraints ------ 2 ≤ N ≤ 50 0 ≤ B[i] ≤ 50, where B[i] denotes the i^{th} element of the array. ------ Sample Input ------ 3 2 0 1 ------ Sample Output ------ 4 ------ Explanation ------ The possible winning arrays are (1, 0, 0), (0, 0, 1), (2, 0, 0) and (1, 0, 1). Let us consider why array (2, 0, 1) is losing. The only move is 2 pick the pair (2, 1). After applying the operation, we get the pair (1, 2). Thus, only the order of elements in the array change and we are in a situation of deadlock. Thus, Chef Dobby can't convert the array to contain 2 zeros.
{"inputs": ["3\n2 0 1"], "outputs": ["4"]}
479
18
coding
Solve the programming task below in a Python markdown code block. #Sorting on planet Twisted-3-7 There is a planet... in a galaxy far far away. It is exactly like our planet, but it has one difference: #The values of the digits 3 and 7 are twisted. Our 3 means 7 on the planet Twisted-3-7. And 7 means 3. Your task is to create a method, that can sort an array the way it would be sorted on Twisted-3-7. 7 Examples from a friend from Twisted-3-7: ``` [1,2,3,4,5,6,7,8,9] -> [1,2,7,4,5,6,3,8,9] [12,13,14] -> [12,14,13] [9,2,4,7,3] -> [2,7,4,3,9] ``` There is no need for a precheck. The array will always be not null and will always contain at least one number. You should not modify the input array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata! Also feel free to reuse/extend the following starter code: ```python def sort_twisted37(arr): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]], [[12, 13, 14]], [[9, 2, 4, 7, 3]]]\n_outputs = [[[1, 2, 7, 4, 5, 6, 3, 8, 9]], [[12, 14, 13]], [[2, 7, 4, 3, 9]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sort_twisted37(*i), o[0])"}
305
259
coding
Solve the programming task below in a Python markdown code block. Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to make it a palindrome. A palindrome is a string that reads the same forward and backward. For example, "noon", "testset" and "a" are all palindromes, while "test" and "kitayuta" are not. You can choose any lowercase English letter, and insert it to any position of s, possibly to the beginning or the end of s. You have to insert a letter even if the given string is already a palindrome. If it is possible to insert one lowercase English letter into s so that the resulting string will be a palindrome, print the string after the insertion. Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one palindrome that can be obtained, you are allowed to print any of them. -----Input----- The only line of the input contains a string s (1 ≤ |s| ≤ 10). Each character in s is a lowercase English letter. -----Output----- If it is possible to turn s into a palindrome by inserting one lowercase English letter, print the resulting string in a single line. Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one solution, any of them will be accepted. -----Examples----- Input revive Output reviver Input ee Output eye Input kitayuta Output NA -----Note----- For the first sample, insert 'r' to the end of "revive" to obtain a palindrome "reviver". For the second sample, there is more than one solution. For example, "eve" will also be accepted. For the third sample, it is not possible to turn "kitayuta" into a palindrome by just inserting one letter.
{"inputs": ["a\n", "z\n", "a\n", "z\n", "ee\n", "iq\n", "iq\n", "ee\n"], "outputs": ["aa\n", "zz\n", "aa\n", "zz\n", "eee\n", "qiq\n", "qiq\n", "eee"]}
403
71
coding
Solve the programming task below in a Python markdown code block. Given N and M Dexter wants to know how many pairs a,b(1 ≤ a < b ≤N) are there such that (a+b) is divisible by M. For example when N=4 and M=3, there are 2 possible pairs the sum of which is divisible by M and they are (1,2) and (2,4). ------ Input ------ First line of input contains T(≤100000) which is the number of test cases. Each of the next T lines contains two integers N(1 ≤ N ≤ 10^{9}) and M(2 ≤ M ≤ 10^{9}). ------ Output ------ Output one line per testcase, the number of pairs (a,b) as described before. ----- Sample Input 1 ------ 3 2 3 4 3 1 6 ----- Sample Output 1 ------ 1 2 0
{"inputs": ["3\n2 3\n4 3\n1 6"], "outputs": ["1\n2\n0"]}
205
28
coding
Solve the programming task below in a Python markdown code block. You're given an array $a$. You should repeat the following operation $k$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0. -----Input----- The first line contains integers $n$ and $k$ $(1 \le n,k \le 10^5)$, the length of the array and the number of operations you should perform. The second line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ $(1 \le a_i \le 10^9)$, the elements of the array. -----Output----- Print the minimum non-zero element before each operation in a new line. -----Examples----- Input 3 5 1 2 3 Output 1 1 1 0 0 Input 4 2 10 3 5 3 Output 3 2 -----Note----- In the first sample: In the first step: the array is $[1,2,3]$, so the minimum non-zero element is 1. In the second step: the array is $[0,1,2]$, so the minimum non-zero element is 1. In the third step: the array is $[0,0,1]$, so the minimum non-zero element is 1. In the fourth and fifth step: the array is $[0,0,0]$, so we printed 0. In the second sample: In the first step: the array is $[10,3,5,3]$, so the minimum non-zero element is 3. In the second step: the array is $[7,0,2,0]$, so the minimum non-zero element is 2.
{"inputs": ["2 5\n2 3\n", "2 5\n2 3\n", "2 5\n4 3\n", "2 1\n4 3\n", "3 5\n1 2 3\n", "3 7\n1 2 3\n", "3 1\n6 1 7\n", "3 1\n6 6 7\n"], "outputs": ["2\n1\n0\n0\n0\n", "2\n1\n0\n0\n0\n", "3\n1\n0\n0\n0\n", "3\n", "1\n1\n1\n0\n0\n", "1\n1\n1\n0\n0\n0\n0\n", "1\n", "6\n"]}
407
170
coding
Solve the programming task below in a Python markdown code block. # Task let F(N) be the sum square of digits of N. So: `F(1) = 1, F(3) = 9, F(123) = 14` Choose a number A, the sequence {A0, A1, ...} is defined as followed: ``` A0 = A A1 = F(A0) A2 = F(A1) ... ``` if A = 123, we have: ``` 123 → 14(1 x 1 + 2 x 2 + 3 x 3) → 17(1 x 1 + 4 x 4) → 50(1 x 1 + 7 x 7) → 25(5 x 5 + 0 x 0) → 29(2 x 2 + 5 x 5) → 85(2 x 2 + 9 x 9) → 89(8 x 8 + 5 x 5) --- → 145(8 x 8 + 9 x 9) |r → 42(1 x 1 + 4 x 4 + 5 x 5) |e → 20(4 x 4 + 2 x 2) |p → 4(2 x 2 + 0 x 0) |e → 16(4 x 4) |a → 37(1 x 1 + 6 x 6) |t → 58(3 x 3 + 7 x 7) | → 89(5 x 5 + 8 x 8) --- → ...... ``` As you can see, the sequence repeats itself. Interestingly, whatever A is, there's an index such that from it, the sequence repeats again and again. Let `G(A)` be the minimum length of the repeat sequence with A0 = A. So `G(85) = 8` (8 number : `89,145,42, 20,4,16,37,58`) Your task is to find G(A) and return it. # Input/Output - `[input]` integer `a0` the A0 number - `[output]` an integer the length of the repeat sequence Also feel free to reuse/extend the following starter code: ```python def repeat_sequence_len(n): ```
{"functional": "_inputs = [[1], [85], [810], [812], [818], [833]]\n_outputs = [[1], [8], [8], [8], [1], [1]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(repeat_sequence_len(*i), o[0])"}
591
195
coding
Solve the programming task below in a Python markdown code block. Harvey Specter has agreed to take Mike Ross to a meeting filled with brilliant scientists at NSA Headquarters. But, as always, it's not going to be easy for Mike. He has to solve a puzzle given by Harvey. Harvey gives two numbers N and K and defines a set A. A = { x : x is a natural number ≤ N } (i.e), A = {1,2,3,4,5,6,...., N} Mike has to find the total number of pairs of elements A[i] and A[j] belonging to the given set, such that, i < j and their sum is divisible by K Input Format An integer T followed by T lines, each containing a pair of space separated integers N and K. Output Format T integers on separate lines. Each integer denotes the answer corresponding to that test case. Constraints 1<=T<=100 K<=N<=10^{9} 1<=K<=10000 Sample Input 2 10 4 7 3 Sample Output 10 7 Explanation For the 1^{st} test case, there are 10 pairs whose sum is divisible by 4. (1,3), (1,7), (2,6), (2,10), (3,5), (3,9), (4,8), (5,7), (6,10) and (7,9) For the 2^{nd} test case, there are 7 pairs whose sum is divisible by 3. (1,2), (1,5), (2,4), (2,7), (3,6), (4,5) and (5,7)
{"inputs": ["2\n10 4\n7 3\n"], "outputs": ["10\n7\n"]}
385
26
coding
Solve the programming task below in a Python markdown code block. Don Drumphet lives in a nice neighborhood, but one of his neighbors has started to let his house go. Don Drumphet wants to build a wall between his house and his neighbor’s, and is trying to get the neighborhood association to pay for it. He begins to solicit his neighbors to petition to get the association to build the wall. Unfortunately for Don Drumphet, he cannot read very well, has a very limited attention span, and can only remember two letters from each of his neighbors’ names. As he collects signatures, he insists that his neighbors keep truncating their names until two letters remain, and he can finally read them. Your code will show Full name of the neighbor and the truncated version of the name as an array. If the number of the characters in name is less than or equal to two, it will return an array containing only the name as is" Also feel free to reuse/extend the following starter code: ```python def who_is_paying(name): ```
{"functional": "_inputs = [['Mexico'], ['Melania'], ['Melissa'], ['Me'], [''], ['I']]\n_outputs = [[['Mexico', 'Me']], [['Melania', 'Me']], [['Melissa', 'Me']], [['Me']], [['']], [['I']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(who_is_paying(*i), o[0])"}
216
200
coding
Solve the programming task below in a Python markdown code block. Problem Yamano Mifune Gakuen's 1st grade G group is a class where female students carrying misfortune gather. They face various challenges every day with the goal of being happy. In their class, they can take practical happiness courses as part of the test of happiness. From Monday to Friday, there are classes from 1st to Nth, and there are M courses that can be taken. Subject i starts from the ai period of the day of the week di (di = 0, 1, 2, 3, 4 corresponds to Monday, Tuesday, Wednesday, Thursday, and Friday, respectively), and is performed in consecutive ki frames. The degree of happiness obtained when taking the course is ti. Each student is free to choose up to L subjects so that they do not overlap each other. How do you choose the subject to get the highest level of happiness? Please find the maximum value of happiness that can be obtained from the information of the given subject. Constraints * 2 ≤ N ≤ 8 * 0 ≤ M ≤ 300 * 0 ≤ L ≤ min (N × 5, M) * 0 ≤ di ≤ 4 * 1 ≤ ai ≤ N * 1 ≤ ki * ai + ki --1 ≤ N * 1 ≤ ti ≤ 100 Input The input is given in the following format. N M L d1 a1 k1 t1 d2 a2 k2 t2 ... dM aM kM tM The first line is given three integers N, M, L separated by blanks. The four integers di, ai, ki, and ti are given on the 2nd to M + 1th lines, separated by blanks. Output Output the maximum value of the sum of happiness on one line. Examples Input 3 7 3 0 1 1 1 0 1 1 2 1 1 3 4 1 1 1 1 1 2 1 2 2 1 1 3 2 2 2 1 Output 9 Input 5 10 5 0 1 1 2 0 2 1 2 0 1 2 3 1 2 1 2 1 4 2 3 2 1 1 1 2 1 1 2 3 3 2 3 4 1 1 2 4 2 1 2 Output 13
{"inputs": ["3 7 3\n0 1 1 1\n0 1 1 2\n1 1 3 4\n1 1 1 1\n1 2 1 2\n3 1 1 3\n2 2 2 1", "3 7 3\n0 1 1 1\n1 1 1 2\n1 1 3 4\n1 1 1 1\n1 2 1 2\n2 1 1 3\n2 2 2 1", "3 7 3\n0 1 0 1\n0 1 1 1\n1 1 3 0\n1 1 1 1\n1 3 1 2\n3 1 1 3\n2 2 2 1", "3 7 3\n0 1 2 1\n1 1 1 2\n1 1 3 4\n1 1 1 1\n1 2 1 2\n3 1 1 2\n2 2 2 1", "3 7 3\n0 1 2 1\n1 2 1 2\n1 1 3 4\n1 1 1 1\n1 2 1 2\n1 1 1 2\n1 2 2 1", "4 7 1\n0 1 1 1\n0 1 1 2\n2 1 1 4\n1 1 1 1\n1 3 0 2\n3 1 1 3\n2 2 1 1", "3 7 3\n0 1 1 1\n0 1 1 2\n1 1 3 4\n1 1 1 1\n1 3 1 2\n3 1 1 3\n2 2 2 1", "3 7 3\n0 1 0 1\n0 1 1 2\n1 1 3 4\n1 1 1 1\n1 3 1 2\n3 1 1 3\n2 2 2 1"], "outputs": ["9\n", "8\n", "6\n", "7\n", "5\n", "4\n", "9\n", "9\n"]}
564
542
coding
Solve the programming task below in a Python markdown code block. You have a stripe of checkered paper of length $n$. Each cell is either white or black. What is the minimum number of cells that must be recolored from white to black in order to have a segment of $k$ consecutive black cells on the stripe? If the input data is such that a segment of $k$ consecutive black cells already exists, then print 0. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next, descriptions of $t$ test cases follow. The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2\cdot10^5$). The second line consists of the letters 'W' (white) and 'B' (black). The line length is $n$. It is guaranteed that the sum of values $n$ does not exceed $2\cdot10^5$. -----Output----- For each of $t$ test cases print an integer — the minimum number of cells that need to be repainted from white to black in order to have a segment of $k$ consecutive black cells. -----Examples----- Input 4 5 3 BBWBW 5 5 BBWBW 5 1 BBWBW 1 1 W Output 1 2 0 1 -----Note----- In the first test case, $s$="BBWBW" and $k=3$. It is enough to recolor $s_3$ and get $s$="BBBBW". This string contains a segment of length $k=3$ consisting of the letters 'B'. In the second test case of the example $s$="BBWBW" and $k=5$. It is enough to recolor $s_3$ and $s_5$ and get $s$="BBBBB". This string contains a segment of length $k=5$ consisting of the letters 'B'. In the third test case of the example $s$="BBWBW" and $k=1$. The string $s$ already contains a segment of length $k=1$ consisting of the letters 'B'.
{"inputs": ["1\n20 2\nWWWBBBWBWBWBWBWBWBWB\n", "4\n5 3\nBBWBW\n5 5\nBBWBW\n5 1\nBBWBW\n1 1\nW\n"], "outputs": ["0\n", "1\n2\n0\n1\n"]}
489
74
coding
Solve the programming task below in a Python markdown code block. The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower. He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i. To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar. How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different. Constraints * 1 \leq N \leq 10^5 * 1 \leq A_i \leq 10^9(1\leq i\leq N) * 1 \leq B_i \leq 10^9(1\leq i\leq N) * 1 \leq C_i \leq 10^9(1\leq i\leq N) * All input values are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N B_1 ... B_N C_1 ... C_N Output Print the number of different altars that Ringo can build. Examples Input 2 1 5 2 4 3 6 Output 3 Input 3 1 1 1 2 2 2 3 3 3 Output 27 Input 6 3 14 159 2 6 53 58 9 79 323 84 6 2643 383 2 79 50 288 Output 87
{"inputs": ["2\n1 5\n2 4\n2 6", "2\n2 5\n2 4\n2 6", "2\n0 5\n2 4\n2 6", "2\n0 5\n2 4\n2 5", "2\n0 5\n2 4\n2 3", "2\n1 5\n2 4\n3 6", "2\n0 10\n2 4\n2 3", "2\n0 12\n2 4\n2 3"], "outputs": ["2\n", "1\n", "2\n", "2\n", "1\n", "3", "1\n", "1\n"]}
477
159
coding
Solve the programming task below in a Python markdown code block. You are given a set of `n` segments on the axis `Ox`, each segment has integer endpoints between `0` and `m` inclusive.  Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers li and ri — coordinates of the left and of the right endpoints.  Consider all integer points between `0` and `m` inclusive. Your task is to print all such points that don't belong to any segment. The point x belongs to the segment `[l;r]` if and only if `l ≤ x ≤ r`. **Input:**  `m` — the upper bound for coordinates;  array of coordinates li and ri `0 ≤ li ≤ ri ≤ m` — the endpoints of the `i`-th segment. Segments may intersect, overlap or even coincide with each other. **Output:**  All points from `0` to `m` that don't belong to any segment. **Examples:** ```python segments(5, [(2,2),(1,2),(5,5)]) => [0,3,4] segments(7, [(0,7)]) => [] ``` Also feel free to reuse/extend the following starter code: ```python def segments(m, a): ```
{"functional": "_inputs = [[7, [[0, 7]]], [2, []], [0, []], [0, [[0, 0]]]]\n_outputs = [[[]], [[0, 1, 2]], [[0]], [[]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(segments(*i), o[0])"}
284
195
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Ash is trying to get visa to Chefland. For the visa to be approved, he needs to satisfy the following three criteria: Solve at least $x_{1}$ problems on Codechef. Have at least $y_{1}$ current rating on Codechef. Make his last submission at most $z_{1}$ months ago. You are given the number of problems solved by Chef ($x_{2}$), his current rating ($y_{2}$) and the information that he made his last submission $z_{2}$ months ago. Determine whether he will get the visa. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains six space-separated integers $x_{1}$, $x_{2}$, $y_{1}$, $y_{2}$, $z_{1}$ and $z_{2}$. ------ Output ------ For each test case, print a single line containing the string "YES" if Chef will get the visa or "NO" if he will not. You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 5,000$ $20 ≤ x_{1}, x_{2} ≤ 50$ $1,900 ≤ y_{1}, y_{2} ≤ 2,100$ $1 ≤ z_{1}, z_{2} ≤ 6$ ----- Sample Input 1 ------ 4 20 50 2100 1900 6 6 50 20 1900 1900 5 5 20 20 1900 1900 1 6 27 27 1920 1920 3 3 ----- Sample Output 1 ------ NO NO NO YES ----- explanation 1 ------ Example case 1: Chef's rating is less than the minimum required rating. Example case 2: Chef has solved a smaller number of problems than the minimum required number. Example case 3: Chef's last submission was made before the allowed period. Example case 4: All three conditions are met.
{"inputs": ["4\n20 50 2100 1900 6 6\n50 20 1900 1900 5 5\n20 20 1900 1900 1 6\n27 27 1920 1920 3 3"], "outputs": ["NO\nNO\nNO\nYES"]}
562
98
coding
Solve the programming task below in a Python markdown code block. Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists of n steps. On the i-th step Greg removes vertex number x_{i} from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex. Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex x_{i}, then Greg wants to know the value of the following sum: $\sum_{v, u, v \neq u} d(i, v, u)$. Help Greg, print the value of the required sum before each step. -----Input----- The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph. Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line a_{ij} (1 ≤ a_{ij} ≤ 10^5, a_{ii} = 0) represents the weight of the edge that goes from vertex i to vertex j. The next line contains n distinct integers: x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ n) — the vertices that Greg deletes. -----Output----- Print n integers — the i-th number equals the required sum before the i-th step. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. -----Examples----- Input 1 0 1 Output 0 Input 2 0 5 4 0 1 2 Output 9 0 Input 4 0 3 1 1 6 0 400 1 2 4 0 1 1 1 1 0 4 1 2 3 Output 17 23 404 0
{"inputs": ["1\n0\n1\n", "1\n0\n1\n", "2\n0 5\n4 0\n1 2\n", "2\n0 5\n7 0\n1 2\n", "2\n0 4\n7 0\n1 2\n", "2\n0 9\n7 0\n1 2\n", "2\n0 1\n7 0\n1 2\n", "2\n0 5\n4 0\n1 2\n"], "outputs": ["0 ", "0 \n", "9 0 ", "12 0", "11 0", "16 0", "8 0", "9 0 \n"]}
510
161
coding
Solve the programming task below in a Python markdown code block. Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money. Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have. Example. $m=6$ $cost=[1,3,4,5,6]$ The two flavors that cost $\mbox{I}$ and $5$ meet the criteria. Using $\mbox{1}$-based indexing, they are at indices $\mbox{1}$ and $\begin{array}{c}4\end{array}$. Function Description Complete the icecreamParlor function in the editor below. icecreamParlor has the following parameter(s): int m: the amount of money they have to spend int cost[n]: the cost of each flavor of ice cream Returns int[2]: the indices of the prices of the two flavors they buy, sorted ascending Input Format The first line contains an integer, $\boldsymbol{\boldsymbol{t}}$, the number of trips to the ice cream parlor. The next $\boldsymbol{\boldsymbol{t}}$ sets of lines each describe a visit. Each trip is described as follows: The integer $m$, the amount of money they have pooled. The integer $n$, the number of flavors offered at the time. $n$ space-separated integers denoting the cost of each flavor: $cost[cost[1],cost[2],...,cost[n]]$. Note: The index within the cost array represents the flavor of the ice cream purchased. Constraints $1\leq t\leq50$ $2\leq m\leq10^4$ $2\leq n\leq10^4$ $1\leq c o s t[i]\leq10^4$, ∀ $i\in[1,n]$ There will always be a unique solution. Sample Input STDIN Function ----- -------- 2 t = 2 4 k = 4 5 cost[] size n = 5 1 4 5 3 2 cost = [1, 4, 5, 3, 2] 4 k = 4 4 cost[] size n = 4 2 2 4 3 cost=[2, 2,4, 3] Sample Output 1 4 1 2 Explanation Sunny and Johnny make the following two trips to the parlor: The first time, they pool together $m=4$ dollars. Of the five flavors available that day, flavors $\mbox{1}$ and $\begin{array}{c}4\end{array}$ have a total cost of $1+3=4$. The second time, they pool together $m=4$ dollars. Of the four flavors available that day, flavors $\mbox{1}$ and $2$ have a total cost of $2+2=4$.
{"inputs": ["2\n4\n5\n1 4 5 3 2\n4 \n4\n2 2 4 3\n"], "outputs": ["1 4\n1 2\n"]}
668
47
coding
Solve the programming task below in a Python markdown code block. $Neha$ is given a number $N$. She always looks for special thing , this time she is looking for $Special$ $Number$ and $Partial$ $Special$ $Number$. A $Special$ $Number$ is a number whose product of its digits is equal to number itself i.e. $N $, and in this number there is no digit $1$. $Partial$ $Special$ is a number having all the condition same as $Special$ except that it can also have digit $1$ in it .Neha have to count the number of $Special$ and $Partial$ $Special$ $Numbers $for a given $N$ . She is not so good in programming , so go and help her. -----Input:----- - Integers $N$ is taken as input from input stream. -----Output:----- - Print the number of $Special$ and $Partial$ $Special$ $Numbers $for a given $N$. -----Constraints----- - $1 \leq N \leq 100$ numbers to be count will be less then 10^6 -----Sample Input:----- 3 -----Sample Output:----- 1 20 -----EXPLANATION:----- There are only one natural numbers, the product of the digits of which is 3 :- {3}. There are 20 natural numbers with digit 1 , whose product of the digits is 3 :-{13, 31, 113 ,131 311 ,1113 ,1131 ,1311, 3111 ,11113, 11131, 11311 ,13111, 31111, 111113, 111131, 111311,113111, 131111 ,311111}
{"inputs": ["3"], "outputs": ["1 20"]}
439
15
coding
Solve the programming task below in a Python markdown code block. Chef is shopping for masks. In the shop, he encounters 2 types of masks: Disposable Masks — cost X but last only 1 day. Cloth Masks — cost Y but last 10 days. Chef wants to buy masks to last him 100 days. He will buy the masks which cost him the least. In case there is a tie in terms of cost, Chef will be eco-friendly and choose the cloth masks. Which type of mask will Chef choose? ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. Then the test cases follow. - Each test case consists of a single line of input, containing two space-separated integers X, Y. ------ Output Format ------ For each test case, if Chef buys the cloth masks print CLOTH, otherwise print DISPOSABLE. You may print each character of the string in uppercase or lowercase (for example, the strings cloth, clOTh, cLoTH, and CLOTH will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 5000$ $1 ≤ X < Y ≤ 100$ ----- Sample Input 1 ------ 4 10 100 9 100 88 99 1 11 ----- Sample Output 1 ------ Cloth Disposable Cloth Disposable ----- explanation 1 ------ Test case $1$: The cost of the disposable masks will be $10 \cdot 100 = 1000$, while the cost of the cloth masks will be $100 \cdot 10 = 1000$. Since the price is equal and Chef is eco-friendly, Chef will buy the cloth masks. Test case $2$: The cost of the disposable masks will be $9 \cdot 100 = 900$, while the cost of the cloth masks will be $100 \cdot 10 = 1000$. Since the price of disposable masks is less, Chef will buy the disposable masks. Test case $3$: The cost of the disposable masks will be $88 \cdot 100 = 8800$, while the cost of the cloth masks will be $99 \cdot 10 = 990$. Since the price of the cloth masks is less, Chef will buy the cloth masks. Test case $4$: The cost of the disposable masks will be $1 \cdot 100 = 100$, while the cost of the cloth masks will be $11 \cdot 10 = 110$. Since the price of disposable masks is less, Chef will buy the disposable masks.
{"inputs": ["4\n10 100\n9 100\n88 99\n1 11\n"], "outputs": ["Cloth\nDisposable\nCloth\nDisposable\n"]}
590
46
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. Substrings that occur multiple times are counted the number of times they occur.   Please complete the following python code precisely: ```python class Solution: def countBinarySubstrings(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"00110011\") == 6\n assert candidate(s = \"10101\") == 4\n\n\ncheck(Solution().countBinarySubstrings)"}
114
56
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Teams $A$ and $B$ are having a penalty shoot-out to decide the winner of their football match. Each team gets to take a shot $N$ times, and the team with the higher number of scored goals at the end wins. If the number of goals scored during the shoot-out by each team turns out to be the same even after all $2N$ shots, then they stop and agree that the result is a draw. The two teams take shots alternately — team $A$ shoots first, then team $B$ shoots, then team $A$ and so on until team $B$ takes its $N$-th shot (at which point each team has taken exactly $N$ shots). Even though all $2N$ shots are taken, the result of the shoot-out is often known earlier — e.g. if team $A$ has already scored $N-1$ goals and team $B$ has missed at least two shots, team $A$ is definitely the winner. For each shot, you know whether it was a goal or a miss. You need to find the earliest moment when the winner is known — the smallest $s$ ($0 ≤ s ≤ 2N$) such that after $s$ shots, the result of the shoot-out (whether team $A$ won, team $B$ won or the match is drawn) would be known even if we did not know the results of the remaining $2N-s$ shots. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains a string $S$ with length $2 \cdot N$, where for each valid $i$, the $i$-th character is '0' if the $i$-th shot was a miss or '1' if it was a goal. ------ Output ------ For each test case, print a single line containing one integer — the smallest $s$ such that after $s$ shots, the result of the shoot-out is known. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $S$ contains only characters '0' and '1' the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 2 3 101011 3 100001 ----- Sample Output 1 ------ 4 6 ----- explanation 1 ------ Example case 1: After four shots, team $A$ has scored $2$ goals and team $B$ has scored $0$ goals. Whatever happens afterwards, team $A$ is guaranteed to win, since even if team $B$ scored their last (and only) goal and team $A$ did not score their last goal, team $A$ would still win by $1$ goal. Example case 2: Team $A$ scores the first goal and after that, neither team scores any goals until the last shot. Up till then, there is always a possibility that the match could end in a draw if team $B$ scored a goal with its last shot, or in the victory of team $A$ if it was a miss. We can only guarantee the result after the last shot.
{"inputs": ["2\n3\n101011\n3\n100001"], "outputs": ["4\n6"]}
767
32
coding
Solve the programming task below in a Python markdown code block. Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks. There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each. What is the minimum amount of money with which he can buy M cans of energy drinks? It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks. -----Constraints----- - All values in input are integers. - 1 \leq N, M \leq 10^5 - 1 \leq A_i \leq 10^9 - 1 \leq B_i \leq 10^5 - B_1 + ... + B_N \geq M -----Input----- Input is given from Standard Input in the following format: N M A_1 B_1 A_2 B_2 \vdots A_N B_N -----Output----- Print the minimum amount of money with which Takahashi can buy M cans of energy drinks. -----Sample Input----- 2 5 4 9 2 4 -----Sample Output----- 12 With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.
{"inputs": ["2 3\n4 9\n2 4", "2 3\n4 9\n4 4", "2 6\n4 9\n4 4", "2 6\n3 9\n4 4", "2 6\n3 9\n2 4", "2 6\n3 9\n1 4", "2 3\n3 4\n1 6", "2 5\n3 4\n1 6"], "outputs": ["6\n", "12\n", "24\n", "18\n", "14\n", "10\n", "3\n", "5\n"]}
325
147
coding
Solve the programming task below in a Python markdown code block. You've purchased a ready-meal from the supermarket. The packaging says that you should microwave it for 4 minutes and 20 seconds, based on a 600W microwave. Oh no, your microwave is 800W! How long should you cook this for?! ___ # Input You'll be given 4 arguments: ## 1. needed power The power of the needed microwave. Example: `"600W"` ## 2. minutes The number of minutes shown on the package. Example: `4` ## 3. seconds The number of seconds shown on the package. Example: `20` ## 4. power The power of your microwave. Example: `"800W"` ___ # Output The amount of time you should cook the meal for formatted as a string. Example: `"3 minutes 15 seconds"` Note: the result should be rounded up. ``` 59.2 sec --> 60 sec --> return "1 minute 0 seconds" ``` ___ ## All comments/feedback/translations appreciated. Also feel free to reuse/extend the following starter code: ```python def cooking_time(needed_power, minutes, seconds, power): ```
{"functional": "_inputs = [['600W', 4, 20, '800W'], ['800W', 3, 0, '1200W'], ['100W', 8, 45, '50W'], ['7500W', 0, 5, '600W'], ['450W', 3, 25, '950W'], ['21W', 64, 88, '25W'], ['83W', 61, 80, '26W'], ['38W', 95, 22, '12W']]\n_outputs = [['3 minutes 15 seconds'], ['2 minutes 0 seconds'], ['17 minutes 30 seconds'], ['1 minutes 3 seconds'], ['1 minutes 38 seconds'], ['55 minutes 0 seconds'], ['199 minutes 0 seconds'], ['302 minutes 0 seconds']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(cooking_time(*i), o[0])"}
277
362
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. You have $C = 100,000$ cakes, numbered $1$ through $C$. Each cake has an integer height; initially, the height of each cake is $0$. There are $N$ operations. In each operation, you are given two integers $L$ and $R$, and you should increase by $1$ the height of each of the cakes $L, L+1, \ldots, R$. One of these $N$ operations should be removed and the remaining $N-1$ operations are then performed. Chef wants to remove one operation in such a way that after the remaining $N-1$ operations are performed, the number of cakes with height exactly $K$ is maximum possible. Since Chef is a bit busy these days, he has asked for your help. You need to find the maximum number of cakes with height exactly $K$ that can be achieved by removing one operation. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. Each of the next $N$ lines contains two space-separated integers $L$ and $R$ describing one operation. ------ Output ------ For each test case, print a single line containing one integer — the maximum possible number of cakes with height $K$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 10^{5}$ $1 ≤ K ≤ N$ $1 ≤ L ≤ R ≤ 10^{5}$ the sum of $N$ over all test cases does not exceed $10^{6}$ ----- Sample Input 1 ------ 1 3 2 2 6 4 9 1 4 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ Example case 1: Let's look at what happens after an operation is removed. - Removing operation $1$: The heights of cakes $4$ through $9$ increase by $1$. Then, the heights of cakes $1$ through $4$ increase by $1$. The resulting sequence of heights is $[1, 1, 1, 2, 1, 1, 1, 1, 1]$ (for cakes $1$ through $9$; the other cakes have heights $0$). The number of cakes with height $2$ is $1$. - Removing operation $2$: The resulting sequence of heights of cakes $1$ through $9$ is $[1, 2, 2, 2, 1, 1, 0, 0, 0]$. The number of cakes with height $2$ is $3$. - Removing operation $3$: The resulting sequence of heights of cakes $1$ through $9$ is $[0, 1, 1, 2, 2, 2, 1, 1, 1]$. The number of cakes with height $2$ is $3$. The maximum number of cakes with height $2$ is $3$.
{"inputs": ["1\n3 2\n2 6\n4 9\n1 4"], "outputs": ["3"]}
723
28
coding
Solve the programming task below in a Python markdown code block. The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one — from $1$ to $2$, the third one — from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) — the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^{18})$ — the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ — the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 -----Note----- Answers on queries from the first example are described in the problem statement.
{"inputs": ["1\n9124\n", "1\n9124\n", "1\n3621\n", "1\n4320\n", "1\n2836\n", "1\n6961\n", "1\n3722\n", "1\n6297\n"], "outputs": ["4\n", "4\n", "1\n", "2\n", "7\n", "3\n", "5\n", "2\n"]}
587
110
coding
Solve the programming task below in a Python markdown code block. Problem statement AOR Ika got a cabbage with $ N $ leaves. The leaves of this cabbage are numbered $ 1, \ ldots, N $ in order from the outside, and the dirtiness of the $ i $ th leaf is $ D_i $. The larger this value is, the worse the degree of dirt is. AOR Ika-chan decided to use the cabbage leaves for cooking, so she decided to select the dirty leaves to be discarded according to the following procedure. 1. Initialize the discard candidates to empty. 2. Focus on the outermost leaves that have not yet been examined. If all the items have been checked, the process ends. 3. If the leaf is dirty for $ A $ or more, add it to the discard candidate and return to 2. Otherwise it ends. However, as a result of this operation, I noticed that the number of leaves that can be used for cooking may be extremely reduced. Therefore, I decided to reconsider the leaves to be discarded after the above operation and perform the following operations. 1. If there are less than $ M $ leaves that are not candidates for disposal, proceed to 2. Otherwise it ends. 2. Focus on the innermost leaf that has not been examined yet among the leaves that are candidates for disposal. If there are no leaves that are candidates for disposal, the process ends. 3. If the leaf is less than $ B $, remove it from the discard candidates and return to 2. Otherwise, discard all the leaves remaining in the discard candidates and finish. When you perform these operations, find the number of leaves to be finally discarded. Input constraints $ 1 \ leq N \ leq 1000 $ $ 0 \ leq M \ leq N $ $ 1 \ leq A \ leq B \ leq 1000 $ $ 1 \ leq D_ {i} \ leq 1000 $ sample Sample input 1 5 3 6 9 9 7 5 3 1 Sample output 1 2 Discard the first and second sheets. Sample input 2 5 3 6 9 5 4 3 2 1 Sample output 2 0 Do not throw away from the first piece. Sample input 3 5 3 6 9 10 8 6 4 2 Sample output 3 1 I tried to throw away the third one, but I reconsidered and decided not to throw away the second and third ones. Sample input 4 5 3 6 9 5 10 8 6 4 Sample output 4 0 AOR Ika doesn't know that the second piece is dirty. Sample input 5 5 0 6 9 9 9 8 8 7 Sample output 5 Five I don't mind throwing everything away. input $ N \ M \ A \ B $ $ D_ {1} \ D_ {2} \ \ cdots \ D_ {N} $ output Output the number of leaves to be finally thrown away. Example Input 5 3 6 9 9 7 5 3 1 Output 2
{"inputs": ["5 3 6 9\n9 1 5 3 1", "5 3 6 9\n9 1 6 3 1", "8 3 6 9\n9 1 6 3 1", "2 3 6 9\n9 7 5 3 1", "5 3 6 8\n9 1 5 3 1", "5 3 6 9\n9 7 5 3 1", "9 3 1 9\n22 2 6 2 0", "8 3 6 9\n15 1 6 3 1"], "outputs": ["1\n", "1\n", "1\n", "0\n", "1\n", "2", "4\n", "1\n"]}
707
191
coding
Solve the programming task below in a Python markdown code block. With the college fest approaching soon, Manasa is following a strict dieting regime . Today, she just cannot resist her temptation for having a pizza. An inner conflict ensues, and she decides that she will have a pizza, only if she comes up with a solution to the problem stated below. Help her get the pizza for herself. Given a list L of N numbers, where L = { a_{1}, a_{2}, a_{3}, a_{4} .... , a_{N}} Find the value of M that is computed as described below. Input Format The first line contains an integer N i.e. size of the list L. The next line contains N space separated integers, each representing an element of the list L. Output Format Print the value of M _modulo (10^{9} + 7)_. Constraints 1 ≤ N ≤ 5100 0 ≤ a_{i} ≤ 10^{15} , where i ∈ [1 .. N] Sample Input 00 3 1 2 3 Sample Output 00 40392 Explanation There are 8 subsets of given set, S = {1,2,3} and L - S ={0} value of F(6) = 19601 S = {1,2} and L - S ={3} value of F(0) = 1 S = {1,3} and L - S ={2} value of F(2) = 17 S = {2,3} and L - S ={1} value of F(4) = 577 S = {1} and L - S ={2,3} value of F(4) = 577 S = {2} and L - S ={1,3} value of F(2) = 17 S = {3} and L - S ={1,2} value of F(0) = 1 S = {} and L - S ={1,2,3} value of F(6) = 19601 Adding all these values, we get M = 40392.
{"inputs": ["3\n1 2 3\n"], "outputs": ["40392\n"]}
496
24
coding
Solve the programming task below in a Python markdown code block. Initially, two non-touching spheres of radii R1 and R2 are lying in space at rest. Both of them are then given accelerations a1 and a2 respectively at time=0. Find whether they will ever come in contact. Their initial positions are represented as (x1,y1,z1) and (x2,y2,z2) respectively. Accelerations have respective components in 3D. They are represented as (a1_{i},a1_{j},a1_{k}) and (a2_{i},a2_{j},a2_{k}) respectively. Input Format The first line contains T, the number of test cases. Each test case consists of five lines, where the first line contains R1 and R2. The next two lines contain position and acceleration of the first sphere. The next two lines after this contain position and acceleration of the second sphere. All numbers in input are integers. Output Format For each test case, print YES, if the spheres come in contact. Otherwise, print NO (quotes for clarity). Constraints 1 ≤ T ≤ 10^{4} 1 ≤ R1, R2 ≤ 10^{2} -10^{2} ≤ x1, y1, z1 , x2 , y2 , z2 ≤ 10^{2} -10^{2} ≤ a1_{i} , a1_{j} , a1_{k} , a2_{i} , a2_{j} , a2_{k} ≤ 10^{2} Sample input 2 1 2 0 0 0 -1 0 0 4 0 0 1 0 0 1 2 0 0 0 100 0 0 4 0 0 0 0 0 Sample output NO YES Explanation For first testcase, both spheres go in opposite directions, so they'll never come in contact. For second testcase, second sphere is not moving while first sphere is accelerating towards the second sphere. So they come in contact.
{"inputs": ["2\n1 2\n0 0 0\n-1 0 0\n4 0 0\n1 0 0\n1 2\n0 0 0\n100 0 0\n4 0 0\n0 0 0\n"], "outputs": ["NO\nYES\n"]}
464
75
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of integers arr, replace each element with its rank. The rank represents how large the element is. The rank has the following rules: Rank is an integer starting from 1. The larger the element, the larger the rank. If two elements are equal, their rank must be the same. Rank should be as small as possible.   Please complete the following python code precisely: ```python class Solution: def arrayRankTransform(self, arr: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(arr = [40,10,20,30]) == [4,1,2,3]\n assert candidate(arr = [100,100,100]) == [1,1,1]\n assert candidate(arr = [37,12,28,9,100,56,80,5,12]) == [5,3,4,2,8,6,7,1,3]\n\n\ncheck(Solution().arrayRankTransform)"}
121
129
coding
Solve the programming task below in a Python markdown code block. Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Chef's planet is called Javad. A year on Javad has $N$ months numbered $1$ through $N$. For each valid $i$, the $i$-th month has $a_{i}$ days numbered $1$ through $a_{i}$. On Javad, years that are divisible by $4$ are leap years - the last month of each leap year is one day longer (it has $a_{N} + 1$ days). You are given Chef's birth date and the current date. Find the age of Chef in days, i.e. the number of days between Chef's birth date and the current date inclusive. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $a_{1}, a_{2}, \ldots, a_{N}$. The third line contains three space-separated positive integers $y_{b}$, $m_{b}$ and $d_{b}$ denoting the year, month and day of Chef's birth respectively. The fourth line contains three space-separated positive integers $y_{c}$, $m_{c}$ and $d_{c}$ denoting the current year, month and day respectively. ------ Output ------ For each test case, print a single line containing one integer — Chef's age in days. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 10,000$ $1 ≤ a_{i} ≤ 10,000$ for each valid $i$ $1 ≤ y_{b}, y_{c} ≤ 100,000$ $1 ≤ m_{b}, m_{c} ≤ N$ $d_{b}$ will be less than or equal to the days of the $m_{b}$ month of the $y_{b}$ year. $d_{c}$ will be less than or equal to the days of the $m_{c}$ month of the $y_{c}$ year. the current date is equal to or greater than Chef's birth date ------ Subtasks ------ Subtask #1 (10 points): $1 ≤ y_{b}, y_{c}, N ≤ 1,000$ $1 ≤ a_{i} ≤ 100$ for each valid $i$ Subtask #2 (90 points): original constraints ----- Sample Input 1 ------ 4 5 1 2 3 4 5 2 1 1 3 5 1 2 3 3 1 1 1 2 2 2 2 1 1 3 1 1 3 2 1 5 1 4 3 4 6 3 5 6 10 1 1 ----- Sample Output 1 ------ 26 11 2 112
{"inputs": ["4\n5\n1 2 3 4 5\n2 1 1\n3 5 1\n2\n3 3\n1 1 1\n2 2 2\n2\n1 1\n3 1 1\n3 2 1\n5\n1 4 3 4 6\n3 5 6\n10 1 1"], "outputs": ["26\n11\n2\n112"]}
699
107
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two 0-indexed integer permutations A and B of length n. A prefix common array of A and B is an array C such that C[i] is equal to the count of numbers that are present at or before the index i in both A and B. Return the prefix common array of A and B. A sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.   Please complete the following python code precisely: ```python class Solution: def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(A = [1,3,2,4], B = [3,1,2,4]) == [0,2,3,4]\n assert candidate(A = [2,3,1], B = [3,1,2]) == [0,1,3]\n\n\ncheck(Solution().findThePrefixCommonArray)"}
147
88
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array of string words and two integers left and right. A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'. Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].   Please complete the following python code precisely: ```python class Solution: def vowelStrings(self, words: List[str], left: int, right: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"are\",\"amy\",\"u\"], left = 0, right = 2) == 2\n assert candidate(words = [\"hey\",\"aeo\",\"mu\",\"ooo\",\"artro\"], left = 1, right = 4) == 3\n\n\ncheck(Solution().vowelStrings)"}
136
82