description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
To stay woke and attentive during classes, Karen needs some coffee! [Image] Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between l_{i} and r_{i} degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? -----Input----- The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between l_{i} and r_{i} degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. -----Output----- For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. -----Examples----- Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 -----Note----- In the first test case, Karen knows 3 recipes. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.
MAX_BOUNDRY = 200002 interval = [0] * MAX_BOUNDRY optimal = [0] * MAX_BOUNDRY num_of_recipes, admision, num_of_question = map(int, input().split()) recipes = [] questions = [] for i in range(num_of_recipes): recipes.append(list(map(int, input().split()))) for i in range(num_of_question): questions.append(list(map(int, input().split()))) for i in range(num_of_recipes): interval[recipes[i][0]] += 1 interval[recipes[i][1] + 1] -= 1 optimal[0] = interval[0] for i in range(1, MAX_BOUNDRY): optimal[i] = interval[i] + optimal[i - 1] for i in range(MAX_BOUNDRY): if optimal[i] >= admision: optimal[i] = 1 else: optimal[i] = 0 for i in range(1, MAX_BOUNDRY): optimal[i] += optimal[i - 1] for question in questions: res = optimal[question[1]] - optimal[question[0] - 1] print(res)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
To stay woke and attentive during classes, Karen needs some coffee! [Image] Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between l_{i} and r_{i} degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? -----Input----- The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between l_{i} and r_{i} degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. -----Output----- For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. -----Examples----- Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 -----Note----- In the first test case, Karen knows 3 recipes. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.
n, k, q = map(int, input().split()) c = [0] * 200003 for i in range(n): a, b = map(int, input().split()) c[a] += 1 c[b + 1] -= 1 for i in range(1, len(c)): c[i] += c[i - 1] c[0] = 1 if c[0] >= k else 0 for i in range(1, len(c)): c[i] = 1 if c[i] >= k else 0 c[i] += c[i - 1] ans = "" for i in range(q): a, b = map(int, input().split()) ans += str(c[b] - c[a - 1]) + "\n" print(ans, end="")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR STRING
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
for tcase in range(int(input())): n, m, k = map(int, input().split()) c = list(map(int, input().split())) t = list(map(int, input().split())) s1 = [[0] for i in range(m)] for i in range(n): s1[c[i] - 1].append(t[i]) s2 = [] for i in range(m): s1[i].sort() for j in range(len(s1[i]) // 2): s2.append(s1[i][2 * j] + s1[i][2 * j + 1]) s2.sort() i, time = 0, 0 while i < len(s2) and time + s2[i] <= k: time += s2[i] i += 1 print(i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n, m, k = ma() c = lis() tt = lis() ind = {} for i in range(n): try: ind[c[i]].append(tt[i]) except: ind[c[i]] = [tt[i]] for i in ind.keys(): ind[i].sort(reverse=True) tot = [] res = 0 for i in ind.keys(): x = ind[i].pop() tot.append([1, x]) while len(ind[i]) > 1: x = ind[i].pop() y = ind[i].pop() tot.append([1, x + y]) tot.sort() for i in tot: k -= i[1] if k < 0: break res += 1 print(res)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER VAR WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
for _ in range(int(input())): n, m, k = map(int, input().split()) c = [int(i) for i in input().split()] t = [int(i) for i in input().split()] dict1 = dict() for i in c: dict1[i] = [] for i in range(n): dict1[c[i]].append(t[i]) for i in dict1: dict1[i].sort() list1 = [] for i in dict1: list1.append(dict1[i][0]) for j in range(1, len(dict1[i]) - 1, 2): list1.append(dict1[i][j] + dict1[i][j + 1]) sum = 0 list1.sort() for i in range(len(list1)): if list1[i] <= k: sum += 1 k -= list1[i] print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
for _ in range(int(input())): n, m, k = map(int, input().split()) c = list(map(int, input().split())) t = list(map(int, input().split())) d = dict() l = dict() for i in c: d[i] = [] l[i] = 0 for i in range(n): d[c[i]].append(t[i]) l[c[i]] += 1 lis = [] le = [] for i in d: d[i].sort() lis.append(d[i]) le.append(l[i]) s = len(le) final = [] for i in range(s): final.append(lis[i][0]) for j in range(1, le[i] - 1, 2): final.append(lis[i][j] + lis[i][j + 1]) final.sort() ans = 0 for i in final: if i <= k: ans += 1 k -= i print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
def main(): n, m, k = map(int, input().split()) subjects = list(map(int, input().split())) times = list(map(int, input().split())) dic = {} for i in range(n): s, t = subjects[i], times[i] if s in dic: dic[s].append(t) else: dic[s] = [t] for s in dic: dic[s].sort() anotherDic = {} for s in dic: anotherDic[s] = [dic[s][0]] arr = dic[s] for i in range(2, len(arr), 2): anotherDic[s].append(arr[i] + arr[i - 1]) arr = [] for s in anotherDic: arr.extend(anotherDic[s]) arr.sort() ans = 0 count = 0 for num in arr: if ans + num <= k: count += 1 ans += num else: break print(count) t = int(input()) for _ in range(t): main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
for t in range(int(input())): N, M, K = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) arr = [[a[i], b[i]] for i in range(N)] arr.sort() vis = [0] * (N + 1) narr = [] i = 0 while i < N: if vis[arr[i][0]]: if i < N - 1 and arr[i + 1][0] == arr[i][0]: narr.append([arr[i][1] + arr[i + 1][1], arr[i][0]]) i += 2 else: i += 1 else: narr.append([arr[i][1], arr[i][0]]) vis[arr[i][0]] = 1 i += 1 narr.sort() count = [0] * (N + 1) for i, j in narr: if i <= K: K -= i if count[j]: count[j] += 2 else: count[j] += 1 else: break ans = 0 for i in count: ans += (i + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
for _ in range(int(input())): n, m, k = map(int, input().split()) c = list(map(int, input().split())) q = list(map(int, input().split())) arr = [[] for i in range(m + 1)] for i in range(n): arr[c[i]].append(q[i]) v = [] for i in range(1, m + 1): arr[i] = sorted(arr[i]) if len(arr[i]) > 0: v.append(arr[i][0]) for j in range(1, len(arr[i]), 2): if j + 1 != len(arr[i]): v.append(arr[i][j] + arr[i][j + 1]) ans = 0 v = sorted(v) for i in range(len(v)): if k >= v[i]: k -= v[i] ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef's exam is near. There is a total of M subjects in his syllabus. Each subject consists of several topics. However, the questions will be set only from N topics. These topics are numbered 1 through N. The i^{th} topic belongs to C_{i}^{th} subject and takes T_{i} hours to cover. Chef has only K hours left before the exam and wants to score the maximum marks. If Chef covers x_{1} number of topics of the 1^{st} subject, x_{2} number of topics of the 2^{nd} subject, and so on upto x_{M} number of topics of the M^{th} subject in the remaining K hours, he will get a total of \lceil \frac{x_{1}}{2} \ \rceil + \lceil \frac{x_{2}}{2} \ \rceil + \cdots + \lceil \frac{x_{M}}{2} \ \rceil marks in the exam. So Chef chooses the topics optimally. Determine the maximum possible marks Chef can score in the exam. Note: \lceil x \rceil : Returns the smallest integer that is greater than or equal to x (i.e rounds up to the nearest integer). For example, \lceil 1.5 \rceil = 2, \lceil 5 \rceil = 5. ------ Input Format ------ - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line of each test case contains three space-separated integers N, M, K. - The second line contains N space-separated integers C_{1}, C_{2}, \dots, C_{N}. - The third line contains N space-separated integers T_{1}, T_{2}, \dots, T_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the maximum marks Chef can score. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ K ≤ 10^{9}$ $1 ≤ C_{i} ≤ M$ $1 ≤ T_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5\cdot10^{5}$. ----- Sample Input 1 ------ 3 3 2 2 1 1 2 1 1 2 4 3 2 1 1 2 2 1 1 2 1 5 3 10 1 1 1 2 3 1 5 2 5 1 ----- Sample Output 1 ------ 1 2 3 ----- explanation 1 ------ Test case $1$: Chef covers the $1^{st}$ and $2^{nd}$ topic in $1 + 1 = 2$ hours. Both topics belongs to $1^{st}$ subject. He does not cover any topic of the second subject. By doing so, Chef gets $\lceil \frac{2}{2} \ \rceil + \lceil \frac{0}{2} \ \rceil$ = $\lceil \ 1 \ \rceil + \lceil \ 0 \ \rceil = 1$ marks. Test case $2$: Chef covers the $1^{st}$ topic which belongs to the first subject and $4^{th}$ topic which belongs to the second subject in $1 + 1 = 2$ hours. There is no topic from the third subject. So Chef gets $\lceil \frac{1}{2} \ \rceil + \lceil \frac{1}{2} \ \rceil$ = $\lceil \ 0.5 \ \rceil + \lceil \ 0.5 \ \rceil = 1 + 1 = 2$ marks.
for _ in range(int(input())): N, M, K = map(int, input().split()) C = list(map(int, input().split())) T = [(int(j), i) for i, j in enumerate(input().split())] T.sort() F = [[] for i in range(M)] Q = [] for t, i in T: s = C[i] - 1 if len(F[s]) & 1 == 0: c = t + (F[s][-1] if len(F[s]) != 0 else 0) Q.append(c) F[s].append(t) Q.sort() ans = 0 i = 0 while i < len(Q) and Q[i] <= K: K -= Q[i] ans += 1 i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces. Output On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1. Examples Input 3 4 7 4 7 7 4 Output 0 Input 5 4 7 7 4 2 11 9 7 1 1 Output 2 Note In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
def solve(): n = int(input()) cards = [] cnt = {} for i in range(n): card = tuple(map(int, input().split(" "))) cards.append(card) cnt[card[0]] = [0, 0] cnt[card[1]] = [0, 0] for card in cards: if card[0] != card[1]: cnt[card[0]][0] += 1 cnt[card[1]][1] += 1 else: cnt[card[0]][0] += 1 border = (len(cards) + 1) // 2 best = float("inf") for num in cnt.keys(): count = cnt[num] if sum(count) >= border: needTop = max(0, border - count[0]) best = min(best, needTop) if best == float("inf"): print(-1) else: print(best) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The Little Elephant loves to play with color cards. He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered). Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces. Output On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1. Examples Input 3 4 7 4 7 7 4 Output 0 Input 5 4 7 7 4 2 11 9 7 1 1 Output 2 Note In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
def main(): n = int(input()) d = {} for i in range(n): a, b = [int(i) for i in input().split()] if a == b: if a not in d: d[a] = [0, 0] d[a][0] += 1 else: if a not in d: d[a] = [0, 0] d[a][0] += 1 if b not in d: d[b] = [0, 0] d[b][1] += 1 result = float("inf") half = (n + 1) // 2 for a, b in d.values(): if a + b >= half: result = min(max(0, half - a), result) if result == float("inf"): print(-1) else: print(result) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has g_{i} Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving. Formally, an evolution plan is a permutation f of {1, 2, ..., m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y. The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol. Two evolution plans f_1 and f_2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f_1(i) ≠ f_2(i). Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains two integers n and m (1 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^6) — the number of gyms and the number of Pokemon types. The next n lines contain the description of Pokemons in the gyms. The i-th of these lines begins with the integer g_{i} (1 ≤ g_{i} ≤ 10^5) — the number of Pokemon in the i-th gym. After that g_{i} integers follow, denoting types of the Pokemons in the i-th gym. Each of these integers is between 1 and m. The total number of Pokemons (the sum of all g_{i}) does not exceed 5·10^5. -----Output----- Output the number of valid evolution plans modulo 10^9 + 7. -----Examples----- Input 2 3 2 1 2 2 2 3 Output 1 Input 1 3 3 1 2 3 Output 6 Input 2 4 2 1 2 3 2 3 4 Output 2 Input 2 2 3 2 2 1 2 1 2 Output 1 Input 3 7 2 1 2 2 3 4 3 5 6 7 Output 24 -----Note----- In the first case, the only possible evolution plan is: $1 \rightarrow 1,2 \rightarrow 2,3 \rightarrow 3$ In the second case, any permutation of (1, 2, 3) is valid. In the third case, there are two possible plans: $1 \rightarrow 1,2 \rightarrow 2,3 \rightarrow 4,4 \rightarrow 3$ $1 \rightarrow 1,2 \rightarrow 2,3 \rightarrow 3,4 \rightarrow 4$ In the fourth case, the only possible evolution plan is: $1 \rightarrow 1,2 \rightarrow 2$
import sys n, m = sys.stdin.readline().split(" ") n = int(n) m = int(m) a1097 = 1000000007 def myread(s, i): j = i lens = len(s) while j < lens and s[j] != " ": j += 1 return int(s[i:j]), j + 1 table = {} total = 1 for i in range(n): inp = sys.stdin.readline() si = 0 input_len, si = myread(inp, si) while input_len > 0: ij, si = myread(inp, si) ij -= 1 input_len -= 1 if ij not in table: table[ij] = {} if i in table[ij]: table[ij][i] += 1 else: table[ij][i] = 1 unused = m - len(table.keys()) current_index = list(table.keys()) list_len = len(current_index) cache = {} for val in table.values(): key = tuple(sorted(val.items())) if key in cache: count = (cache[key] + 1) % a1097 cache[key] = count total *= count total = total % a1097 else: cache[key] = 1 for i in range(2, unused + 1): total = total * (i % a1097) % a1097 print(total)
IMPORT ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, string value, int timestamp) Stores the key and value, along with the given timestamp. 2. get(string key, int timestamp) Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the one with the largest timestamp_prev. If there are no values, it returns the empty string ("").   Example 1: Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]] Output: [null,null,"bar","bar",null,"bar2","bar2"] Explanation:   TimeMap kv;   kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   kv.get("foo", 1); // output "bar"   kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"   kv.set("foo", "bar2", 4);   kv.get("foo", 4); // output "bar2"   kv.get("foo", 5); //output "bar2"   Example 2: Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]] Output: [null,null,null,"","high","high","low","low"]   Note: All key/value strings are lowercase. All key/value strings have length in the range [1, 100] The timestamps for all TimeMap.set operations are strictly increasing. 1 <= timestamp <= 10^7 TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
class TimeMap: def __init__(self): self._data = collections.defaultdict(list) def set(self, key, value, timestamp): self._data[key].append((value, timestamp)) def get(self, key, timestamp): if key not in self._data: return "" arr = self._data[key] start, end = 0, len(arr) - 1 while start + 1 < end: mid = start + (end - start) // 2 if arr[mid][1] <= timestamp: start = mid else: end = mid - 1 if arr[end][1] <= timestamp: return arr[end][0] if arr[start][1] <= timestamp: return arr[start][0] return ""
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR RETURN VAR VAR NUMBER IF VAR VAR NUMBER VAR RETURN VAR VAR NUMBER RETURN STRING
Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, string value, int timestamp) Stores the key and value, along with the given timestamp. 2. get(string key, int timestamp) Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the one with the largest timestamp_prev. If there are no values, it returns the empty string ("").   Example 1: Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]] Output: [null,null,"bar","bar",null,"bar2","bar2"] Explanation:   TimeMap kv;   kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   kv.get("foo", 1); // output "bar"   kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"   kv.set("foo", "bar2", 4);   kv.get("foo", 4); // output "bar2"   kv.get("foo", 5); //output "bar2"   Example 2: Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]] Output: [null,null,null,"","high","high","low","low"]   Note: All key/value strings are lowercase. All key/value strings have length in the range [1, 100] The timestamps for all TimeMap.set operations are strictly increasing. 1 <= timestamp <= 10^7 TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
class TimeMap(object): def __init__(self): self.map = collections.defaultdict(list) def set(self, key, value, timestamp): self.map[key].append((timestamp, value)) def get(self, key, timestamp): values = self.map[key] if not values: return "" left, right = 0, len(values) - 1 while left < right: mid = (left + right + 1) // 2 pre_time, value = values[mid] if pre_time > timestamp: right = mid - 1 else: left = mid return values[left][1] if values[left][0] <= timestamp else ""
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR RETURN STRING ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR NUMBER VAR VAR VAR NUMBER STRING
Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, string value, int timestamp) Stores the key and value, along with the given timestamp. 2. get(string key, int timestamp) Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the one with the largest timestamp_prev. If there are no values, it returns the empty string ("").   Example 1: Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]] Output: [null,null,"bar","bar",null,"bar2","bar2"] Explanation:   TimeMap kv;   kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   kv.get("foo", 1); // output "bar"   kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"   kv.set("foo", "bar2", 4);   kv.get("foo", 4); // output "bar2"   kv.get("foo", 5); //output "bar2"   Example 2: Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]] Output: [null,null,null,"","high","high","low","low"]   Note: All key/value strings are lowercase. All key/value strings have length in the range [1, 100] The timestamps for all TimeMap.set operations are strictly increasing. 1 <= timestamp <= 10^7 TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
class TimeMap: def __init__(self): self.d = {} def set(self, key, value, timestamp): self.d.setdefault(key, []).append([timestamp, value]) def get(self, key, timestamp): v = self.d.get(key, []) i = bisect.bisect_right(v, [timestamp, chr(ord("z") + 1)]) return v[i - 1][1] if i else ""
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF EXPR FUNC_CALL FUNC_CALL VAR VAR LIST LIST VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER RETURN VAR VAR BIN_OP VAR NUMBER NUMBER STRING
Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, string value, int timestamp) Stores the key and value, along with the given timestamp. 2. get(string key, int timestamp) Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the one with the largest timestamp_prev. If there are no values, it returns the empty string ("").   Example 1: Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]] Output: [null,null,"bar","bar",null,"bar2","bar2"] Explanation:   TimeMap kv;   kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   kv.get("foo", 1); // output "bar"   kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"   kv.set("foo", "bar2", 4);   kv.get("foo", 4); // output "bar2"   kv.get("foo", 5); //output "bar2"   Example 2: Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]] Output: [null,null,null,"","high","high","low","low"]   Note: All key/value strings are lowercase. All key/value strings have length in the range [1, 100] The timestamps for all TimeMap.set operations are strictly increasing. 1 <= timestamp <= 10^7 TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
class TimeMap: def __init__(self): self.M = collections.defaultdict(list) def set(self, key: str, value: str, timestamp: int) -> None: self.M[key].append((timestamp, value)) def get(self, key: str, timestamp: int) -> str: A = self.M[key] if not A: return "" left = 0 right = len(A) - 1 while left <= right: mid = left + (right - left) // 2 if timestamp == A[mid][0]: return A[mid][1] if timestamp < A[mid][0]: right = mid - 1 else: left = mid + 1 if right == -1: return "" return A[right][1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NONE FUNC_DEF VAR VAR ASSIGN VAR VAR VAR IF VAR RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING RETURN VAR VAR NUMBER VAR
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): begin, end = 0, 2 if len(nums) == 1: return 0 while end < len(nums): mid = (begin + end) // 2 if nums[mid] > nums[begin] and nums[mid] > nums[end]: return mid begin += 1 end += 1 if nums[0] > nums[1]: return 0 if nums[-1] > nums[-2]: return len(nums) - 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): if len(nums) == 0: return None if len(nums) == 1: return 0 if len(nums) == 2: return 0 if nums[0] > nums[1] else 1 res = list() if nums[0] > nums[1]: return 0 if nums[-1] > nums[-2]: return len(nums) - 1 for i in range(1, len(nums) - 1): pre = nums[i - 1] cur = nums[i] nex = nums[i + 1] if cur > pre and cur > nex: return i
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): if len(nums) == 1: return 0 left, right = 0, len(nums) - 1 length = len(nums) while left + 1 < right: mid = (left + right) // 2 if mid == 0: if nums[mid] > nums[mid + 1]: return 0 else: left = mid + 1 elif mid == length - 1: if nums[-1] > nums[-2]: return mid else: right = mid - 1 elif nums[mid] > nums[mid - 1] and nums[mid] > nums[mid + 1]: return mid elif nums[mid] < nums[mid - 1] and nums[mid] < nums[mid + 1]: right = mid - 1 elif nums[mid] > nums[mid + 1]: right = mid - 1 else: left = mid + 1 for idx in range(left, right + 1): if idx == 0 and nums[idx] > nums[idx + 1]: return 0 if idx == length - 1 and nums[-1] > nums[-2]: return idx if nums[idx] > nums[idx - 1] and nums[idx] > nums[idx + 1]: return idx
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): n = len(nums) if n == 0: return null if n == 1: return 0 if nums[0] > nums[1]: return 0 for ind in range(1, len(nums) - 1): print(ind) if nums[ind] > nums[ind - 1] and nums[ind] > nums[ind + 1]: return ind if nums[n - 1] > nums[n - 2]: return n - 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): length = len(nums) if length == 0: return None if length == 1: return 0 if length == 2: return 0 if nums[0] > nums[1] else 1 for idx in range(0, length): value = nums[idx] if ( idx > 0 and idx < length - 1 and nums[idx + 1] < value and nums[idx - 1] < value ): pick = idx elif idx == 0 and nums[idx + 1] < value: pick = idx elif idx == length - 1 and nums[idx - 1] < value: pick = idx return pick
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): def bs(nums, i, j): if i >= j: return j k = (i + j) // 2 if nums[k] < nums[k + 1]: return bs(nums, k + 1, j) else: return bs(nums, i, k) if not nums: return None return bs(nums, 0, len(nums) - 1)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NONE RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): return self.helper(nums, 0, len(nums) - 1) def helper(self, nums, start, end): if start == end: return start mid = (end + start) // 2 if nums[mid] > nums[mid + 1]: return self.helper(nums, start, mid) else: return self.helper(nums, mid + 1, end)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): if len(nums) == 0 or len(nums) == 1: return 0 l, r = 0, len(nums) while l < r: if l == r: break m = (l + r) // 2 if m < len(nums) - 1 and nums[m] > nums[m + 1]: if nums[m] >= nums[m - 1]: return m if m < len(nums) - 1 and nums[m] < nums[m + 1] and nums[m - 1] < nums[m]: l = m + 1 else: r = m if l > 0: if nums[l] > nums[l - 1]: return l else: return l - 1 elif nums[l] > nums[l + 1]: return l else: return l + 1
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): if not nums: return -1 start = 0 end = len(nums) - 1 while start + 1 < end: mid = (start + end) // 2 if nums[mid] > nums[mid - 1]: if nums[mid] > nums[mid + 1]: return mid else: start = mid else: end = mid if nums[start] > nums[end]: return start else: return end
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN VAR RETURN VAR
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that nums[-1] = nums[n] = -∞. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,   or index number 5 where the peak element is 6. Note: Your solution should be in logarithmic complexity.
class Solution: def findPeakElement(self, nums): for i in range(1, len(nums)): if nums[i] < nums[i - 1]: return i - 1 return len(nums) - 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): level = 0 index = 0 ans = list() while index < n: nodesCurrLevel = pow(2, level) - 1 lastindex = min(index + nodesCurrLevel, n - 1) arr = ( arr[:index] + sorted(arr[index : lastindex + 1]) + arr[lastindex + 1 :] ) lst = list() while index <= lastindex: lst.append(arr[index]) index += 1 ans.append(lst) level += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] i = 0 c = 0 j = 2**c while i < n and j < n: t = arr[i:j] ans.append(sorted(t)) i = j c += 1 j = min(n, i + 2**c) ans.append(sorted(arr[i:j])) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): a = [] i = p = 0 while i < len(arr): a.append(sorted(arr[i : i + 2**p])) i += 2**p p += 1 return a t = int(input()) for tc in range(t): n = int(input()) arr = list(map(int, input().split())) ob = Solution() res = ob.binTreeSortedLevels(arr, n) for i in range(len(res)): for j in range(len(res[i])): print(res[i][j], end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): l = 0 el = 1 r = l + el ans = [] while r <= len(arr): brr = arr[l:r] brr.sort() ans.append(brr) el *= 2 if r < len(arr): l = r if l + el <= len(arr): r = l + el else: r = len(arr) elif r == len(arr): break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] i = 0 while arr: temp = [] c = 0 while c < 2**i and arr: temp.append(arr.pop(0)) c += 1 ans.append(sorted(list(temp))) i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): re = [] level = 0 i = 0 while i < n: ans = [] if level == 0: re.append([arr[i]]) i += 1 level += 1 else: size = 2**level if i + size < n: ans.extend(arr[i : i + size]) ans.sort() re.append(ans) i += size level += 1 else: ans.extend(arr[i:]) ans.sort() re.append(ans) break return re
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ind = 0 ans = [] q = [arr[ind]] while q: b = sorted(q) ans.append(b) nn = len(q) for i in range(nn): p = q.pop(0) if ind + 1 < n: q.append(arr[ind + 1]) if ind + 2 < n: q.append(arr[ind + 2]) ind += 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): if n == 0: return [] ans = [[arr[0]]] l = 0 r = 1 while r < n: l = min(l * 2 + 1, n - 1) r = min(r * 2 + 1, n) ans.append(sorted(arr[l:r])) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] i = 1 while i <= n: temp = [] for j in range(i): if not arr: break temp.append(arr.pop(0)) temp.sort() ans.append(temp) i *= 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): if not arr: return [] res = [] q = [arr[0]] pointer = 1 while q: tempQ = [] data = [] while q: data.append(q.pop(0)) if pointer < len(arr): tempQ.append(arr[pointer]) pointer += 1 if pointer < len(arr): tempQ.append(arr[pointer]) pointer += 1 res.append(sorted(data)) q = tempQ return res
CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, l, n): start = 0 end = 0 count = 0 result_list = [] while True: end = 2**count if end > len(l): break result_list.append(sorted(l[start : end + start])) count += 1 start += end return result_list
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): i = 0 a = [] while 2**i <= n: a.append(sorted(arr[: 2**i])) arr[:] = arr[2**i :] i = i + 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] if n == 0: return ans size = 1 start = 0 while True: if start + size > n: level = arr[start:n] if level: ans.append(sorted(level)) break else: level = arr[start : start + size] ans.append(sorted(level)) start += size size *= 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] m = 1 level = [] j = 0 for i in range(n): if j < m: level.append(arr[i]) j += 1 else: level.sort() ans.append(level.copy()) level.clear() m += m j = 1 level.append(arr[i]) level.sort() ans.append(level) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): res, start, end, len1 = [], 0, 1, 1 while start < n: res.append(sorted(arr[start:end])) len1 *= 2 start = end end = start + len1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): a1 = {} queue = [0] queue1 = [0] while len(queue) > 0: x = queue.pop(0) y = queue1.pop(0) if y not in a1: a1[y] = [] a1[y].append(arr[x]) if 2 * x + 1 < len(arr): queue.append(2 * x + 1) queue1.append(y + 1) if 2 * x + 2 < len(arr): queue.append(2 * x + 2) queue1.append(y + 1) e = [] for i in range(max(a1) + 1): e.append(sorted(a1[i])) return e
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): final = [] l = [1] final.append([arr[0]]) i = 0 while True: li = len(l) l = [] for j in range(li): if 2 * i + 1 < n: l.append(arr[2 * i + 1]) if 2 * i + 2 < n: l.append(arr[2 * i + 2]) i += 1 if len(l): final.append(sorted(l)) else: break return final
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): i = 0 c = 0 b = [] while i < n: e = 2**c k = [] for j in range(e): if i < n: k.append(arr[i]) i += 1 k.sort() c += 1 b.append(k) return b t = int(input()) for tc in range(t): n = int(input()) arr = list(map(int, input().split())) ob = Solution() res = ob.binTreeSortedLevels(arr, n) for i in range(len(res)): for j in range(len(res[i])): print(res[i][j], end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): n = len(arr) list2 = [[arr[0]]] c = 0 j = 1 list3 = [] for x in range(1, n): if c == 2**j - 1: list3.append(arr[x]) list3.sort() list2.append(list3) list3 = [] j += 1 c = 0 else: list3.append(arr[x]) c += 1 if len(list3) != 0: list3.sort() list2.append(list3) return list2
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): j = 0 l = [] while j < n: i = 2 * j + 1 l1 = arr[j:i] l1.sort() l.append(l1) j = i return l
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): a = [[arr[0]]] i = 1 while i < n: b = arr[i : 2 * i + 1] b.sort() a.append(b) i = 2 * i + 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): res = [] i = 0 ls = 1 while i < n: t = (1 << ls) - 1 t = min(t, n) temp = sorted(arr[i:t]) i = t ls += 1 res.append(temp) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): j = 0 level = [] result = [] for i in range(n): if len(level) < 2**j: level.append(arr[i]) if len(level) == 2**j or i == n - 1: result.append(sorted(level.copy())) level.clear() j += 1 i += 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): mm = 0 x = 0 b = [] if n == 1: return [arr] exit() while x < n: e = 2**mm t = [] for k in range(e): if x < n: t.append(arr[x]) x = x + 1 t.sort() mm = mm + 1 b.append(t) return b
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER RETURN LIST VAR EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, x, n): res = [] i = 0 j = 1 while i < n: res.append(sorted(x[i : i + j])) i = i + j j = j * 2 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): output = [] i = 0 while 2**i <= n: j = 2**i k = 2 ** (i + 1) i += 1 output.append(sorted(arr[j - 1 : k - 1])) return output
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): res = [] l = 0 i = 0 while True: count = int(2**l) tmp = [] while count != 0 and i < n: tmp.append(arr[i]) i += 1 count -= 1 res.append(sorted(tmp)) if i >= n: break l += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): c = 0 i = 0 ans = [] while c < n: l = [] x = pow(2, i) while x > 0 and c < n: l.append(arr[c]) c += 1 x -= 1 i += 1 l.sort() ans.append(l) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): lst = [] level = 0 i = 0 while i < n: l = [] if level == 0: l.append(arr[i]) lst.append(l) i = i + 1 level = level + 1 else: size = 2**level if i + size < n: l.extend(arr[i : i + size]) l.sort() lst.append(l) i = i + size level = level + 1 else: l.extend(arr[i:]) l.sort() lst.append(l) break return lst
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): lvl = -1 ans = [] while len(arr) > 0: lvl += 1 s = pow(2, lvl) if s > len(arr): s = len(arr) arr[0:s].sort() ans.append([]) while s > 0: ans[lvl].append(arr.pop(0)) s -= 1 for i in range(lvl + 1): ans[i].sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): k, i = 0, 1 ans = [] z = [] tot = 0 for x in arr: if k < i: z.append(x) k += 1 else: ans.append(sorted(z)) tot += k k = 0 i *= 2 z = [] z.append(x) k += 1 if tot != n: ans.append(sorted(z)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): curr = 1 i = 0 j = 0 final_ans = [] ans = [] while i < n: ans.append(arr[i]) i += 1 j += 1 if j == curr: j = 0 curr *= 2 ans.sort() final_ans.append(ans) ans = [] if len(ans): ans.sort() final_ans.append(ans) return final_ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): c = 0 p = 0 l = [] while p < n: s = 2**c k = arr[p : p + s] c = c + 1 p = p + s k.sort() l.append(k) return l
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): i = 1 ans = [] while len(arr): ans.append(sorted(arr[:i])) arr = arr[i:] i <<= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] c = 0 i = 1 while True: v = [] j = 0 while j < i and c < n: v.append(arr[c]) c += 1 j += 1 ans.append(sorted(v)) i = 2 * i if c == n: break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): if not arr: return res = [[arr[0]]] level = 1 i = 1 l = len(arr) while i < l: tmp = [] j = 0 while i < l and j < 2**level: tmp.append(arr[i]) i += 1 j += 1 level += 1 tmp.sort() res.append(tmp) return res
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): l = 0 i = 0 s = [] while i < n: cln = 2**l j = min(i + cln - 1, n - 1) s.append(sorted(arr[i : j + 1])) i = j + 1 l += 1 return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): res = [] level = 0 i = 0 while i < len(arr): res.append([]) j = min(len(arr), i + pow(2, level)) - 1 for k in range(j, i - 1, -1): res[-1].append(arr[k]) i = j + 1 level += 1 res[-1].sort() return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): i = 0 k = 1 res = [] while i < n: temp = arr[i : i + k] temp.sort() res.append(temp) i += k k *= 2 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, a, n): l = [] q = [0] while q: t = a[q[0] : q[-1] + 1] t.sort() l.append(t) t = q.copy() q.clear() for e in t: r1 = 2 * e + 1 r2 = 2 * e + 2 if r1 < n: q.append(r1) if r2 < n: q.append(r2) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): start = 0 i = 0 increment = 2**i list1 = [] while start < n: list1.append(sorted(arr[start : start + increment])) start += increment i += 1 increment = 2**i return list1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): if n == 1: return [[arr[0]]] else: l = [[arr[0]]] i = 1 c = 1 while i < n: size = 2**c if i + size < n: a = arr[i : i + size] a.sort() l.append(a) i += size c += 1 else: a = arr[i:] a.sort() l.append(a) break return l
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST LIST VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order. Example 1: Input: N = 7 arr[] = {7 6 5 4 3 2 1} Output: 7 5 6 1 2 3 4 Explanation: The formed Binary Tree is: 7 / \ 6 5 / \ / \ 4 3 2 1 Example 2: Input: N = 6 arr[] = {5 6 4 9 2 1} Output: 5 4 6 1 2 9 Explanation: The formed Binary Tree is: 5 / \ 6 4 / \ / 9 2 1 Your Task: You don't need to read input or print anything. Your task is to complete the function binTreeSortedLevels() which takes the array arr[] and its size N as inputs and returns a 2D array where the i-th array denotes the nodes of the i-th level in sorted order. Expected Time Complexity: O(NlogN). Expected Auxiliary Space: O(N). Constraints: 1 <= N <= 10^{4}
class Solution: def binTreeSortedLevels(self, arr, n): ans = [] si = 0 k = 0 while si < n: size = 2**k k += 1 if si + size >= n: tans = arr[si:] else: tans = arr[si : si + size] tans.sort() ans.append(tans) si += size return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input().strip()) for i in range(t): n = int(input().strip()) nums = list(map(int, input().split())) mp = {} mx = 0 for j in range(n): x = nums[j] if x not in mp: mp[x] = 1 else: mp[x] += 1 mx = max(mx, mp[x]) if n <= 2: print(0) else: print(min(n - 2, n - mx))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) if n <= 2: print(0) continue d = {} for i in l: if i not in d: d[i] = 1 else: d[i] = d[i] + 1 z = max(d.values()) if z == 1: print(n - 2) else: print(n - z)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
test_cases = int(input()) for i in range(test_cases): num_integers = int(input()) arr = [int(x) for x in input().split()] frequency = {} for i in range(num_integers): if arr[i] not in frequency: frequency[arr[i]] = 1 else: frequency[arr[i]] += 1 if num_integers == 1 or num_integers == 2: print("0") else: max_frequency = 0 for i in frequency.values(): max_frequency = max(max_frequency, i) if max_frequency == 1: max_frequency += 1 print(num_integers - max_frequency)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
T: int = int(input()) for x in range(T): N: int = int(input()) inp: list = list(map(int, input().split(" "))) count: dict = {} if len(inp) <= 2: print(0) else: for i in range(0, N): n = inp[i] if n in count: k = count[n] + 1 count.update({n: k}) else: count[n] = 1 maxcount: int = max(count.values()) if maxcount == 1: print(N - 2) else: print(N - maxcount)
VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR DICT IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
test = int(input()) for z in range(test): n = int(input()) A = list(map(int, input().split())) if n <= 2: print(0) continue d = {} Max = 1 for i in A: if i in d: d[i] += 1 if Max < d[i]: Max = d[i] else: d[i] = 1 if Max == 1: print(n - 2) else: print(n - Max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
test_cases = int(input()) list_final = [] for t in range(test_cases): n = int(input()) data = input() if n > 2: list_data = [] a = 0 for j in range(len(data)): if data[j] == " ": list_data.append(data[a:j]) a = j + 1 list_data.append(data[a:]) list_data.sort() list_count = [] count = 1 for k in range(1, n): if list_data[k] == list_data[k - 1]: count = count + 1 else: list_count.append(count) count = 1 list_count.append(count) max_count = int(list_count[0]) for i in range(len(list_count)): if int(list_count[i]) > max_count: max_count = int(list_count[i]) if len(list_count) != n: req_ans = n - max_count list_final.append(req_ans) else: list_final.append(n - 2) else: list_final.append(0) for k in range(len(list_final)): print(list_final[k])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) while t: n = int(input()) a = list(map(int, input().split())) if n <= 2: print(0) else: d = {} x = 0 for i in range(n): d[a[i]] = d.get(a[i], 0) + 1 for i in d.values(): if i > x: x = i if x == 1: print(n - 2) else: print(n - x) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) for _ in range(t): n = int(input()) val = input().split() dict = {} greatest = 0 for i in val: if i in dict: dict[i] = dict[i] + 1 else: dict[i] = 1 greatest = max(dict[i], greatest) if greatest > 2: print(n - greatest) else: print(max(0, n - 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) if n <= 2: print(0) elif len(set(arr)) == len(arr): print(n - 2) else: d = {} for i in arr: if i in d: d[i] += 1 else: d[i] = 1 m = 0 for i in d: m = max(m, d[i]) print(n - m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for i in range(int(input())): a = int(input()) l = list(map(int, input().split())) d = {} if a > 2: for i in range(a): if l[i] not in d: d[l[i]] = 0 d[l[i]] += 1 fre = list(d.values()) m = max(fre) if m == 1: print(a - 2) else: print(a - m) else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
T = int(input()) while T > 0: n = int(input()) a = list(map(int, input().split()[:n])) dict = {} for i in range(n): if a[i] in dict: dict[a[i]] += 1 else: dict[a[i]] = 1 l = [] for i in dict: l.append(dict[i]) l.sort() if len(l) == n: if len(l) > 1: print(n - 2) else: print(0) else: print(n - l[-1]) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
def solve(array): if len(array) <= 2: return 0 max_count = 0 hashing = {} for x in array: try: hashing[x] += 1 except: hashing[x] = 1 if hashing[x] > max_count: max_count = hashing[x] if max_count == 1: return len(array) - 2 return len(array) - max_count test = int(input()) for _ in range(test): x = int(input()) array = [int(x) for x in input().split()] print(solve(array))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) for _ in range(t): n = int(input()) numbers = list(map(int, input().split())) numbers.sort() max_occuring_number = None current_num_occurence = 0 max_occurence = None last_number = None for index, number in enumerate(numbers): if index == 0: last_number = number num_count = 1 max_occurence = 1 continue if number == last_number: num_count += 1 else: if num_count > max_occurence: max_occurence = num_count num_count = 1 last_number = number if num_count > max_occurence: max_occurence = num_count if n == 2 and max_occurence == 1: print(0) elif max_occurence == 1 and n > 1: print(n - max_occurence - 1) else: print(n - max_occurence)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n <= 2: print(0) else: d = {} for i in range(n): d[a[i]] = d.get(a[i], 0) + 1 v = list(d.values()) m = 0 for i in v: if i > m: m = i if m == 1: print(n - 2) else: print(n - m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
repeats = int(input()) for _ in range(0, repeats): input() nums = list(map(int, input().split())) if len(nums) <= 2: print(0) continue nc = {} count_max = 0 for n in nums: c = nc.get(n, 0) + 1 count_max = max(c, count_max) nc[n] = nc.get(n, 0) + 1 if count_max == 1: count_max += 1 print(len(nums) - count_max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): n = int(input()) lst = [int(x) for x in input().split()] if len(lst) > 2: counts = {} for item in lst: if item in counts: counts[item] += 1 else: counts[item] = 1 max_count = 0 max_item = None for item, count in counts.items(): if count > max_count: max_count = count max_item = item if max_count == 1: print(len(lst) - 2) else: print(len(lst) - max_count) else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) for _ in range(t): N = int(input()) A = input().split() unique = {} if N <= 2: print(0) continue maxi_nb = 0 for i in A: if i in unique: unique[i] = unique[i] + 1 else: unique[i] = 1 if unique[i] > maxi_nb: maxi_nb = unique[i] if maxi_nb >= 2: print(N - maxi_nb) else: print(N - 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for i in range(int(input())): n = int(input()) lis = list(map(int, input().strip().split(" "))) lis.sort() a = [] if n == 2 or n == 1: print(0) else: while lis: x = 0 s = lis[0] while lis and s == lis[0]: x += 1 lis.pop(0) a.append(x) a.sort() if a[-1] != 1: print(n - a[-1]) else: print(n - 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) for i in range(t): n = int(input()) a = [int(k) for k in input().split()] a.sort() m = 2 c = 0 ox = 0 for x in a: if x == ox: c += 1 if c > m: m = c else: c = 1 ox = x if n > 1: print(n - m) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) d = {} for i in arr: d[i] = d.get(i, 0) + 1 odd_count = 0 even_count = 0 m = max(d.values()) if n <= 2: print("0") elif m == 1: print(n - 2) elif m > 1: print(n - m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) freq = {} for i in arr: if i in freq: freq[i] += 1 else: freq[i] = 1 maxi = 0 for i in freq: maxi = max(maxi, freq[i]) if n == 1 or n == 2: print(0) elif maxi == 1: print(n - 2) else: print(n - maxi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) d = {} for i in l: d[i] = d.get(i, 0) + 1 ans = d[max(d, key=lambda x: d[x])] print(n + int(n == 1) - int(ans == 1) - ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
def min_deletions(arr): freq = {} for num in arr: freq[num] = freq.get(num, 0) + 1 max_freq = max(freq.values()) if max_freq == len(arr): return 0 if max_freq == 1: return len(arr) - 2 return len(arr) - max_freq t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(min_deletions(arr))
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
def frequency_check(dictionary): for item in range(len(dictionary.keys()) - 1): if dictionary.keys()[item] != dictionary.keys()[item + 1]: return False return True def max_key(dictionary): Max = 0 for item in dictionary: if dictionary[item] > Max: Max = dictionary[item] return Max def func(): n = int(input()) arr = list(map(int, input().split())) if n <= 2: return 0 empt = {} for i in range(n): if arr[i] not in empt: empt[arr[i]] = 1 else: empt[arr[i]] += 1 if max_key(empt) == 1: return n - 2 return n - max_key(empt) t = int(input()) while t != 0: t = t - 1 print(func())
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
def sol(): a = int(input()) b = list(map(int, input().split())) if a < 3: return 0 else: z = {} for i in b: if i in z: z[i] += 1 else: z[i] = 1 y = max(z.values()) if y == 1: return a - 2 else: return a - max(z.values()) n = int(input()) for _ in range(n): print(sol())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
get_int = lambda: int(input()) get_mint = lambda: list(map(int, input().split())) for _ in range(get_int()): N = get_int() A = get_mint() if N < 3: print(0) continue if len(set(A)) == N: print(N - 2) continue d = dict() for Ai in A: if Ai not in d: d[Ai] = 0 d[Ai] += 1 print(N - max([v for v in d.values()]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): N = int(input()) A = [int(x) for x in input().split()] if N <= 2: print(0) else: dc = {} max = 1 for i in range(N): dc[A[i]] = 0 for i in range(N): dc[A[i]] += 1 for i in dc: if dc[i] > max: max = dc[i] if max > 2: print(N - max) else: print(N - 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for temp in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] freq = {} for i in arr: try: freq[i] += 1 except: freq[i] = 1 m1 = 0 l = len(freq) if n <= 2: print(0) elif n == 3: if l == 1: print(0) else: print(1) else: for j in freq.values(): if j > m1: m1 = j if m1 == 1: print(n - 2) else: print(n - m1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): n = int(input()) lst = list(map(int, input().split())) nc = {} count = 0 if n <= 2: print(0) continue for i in lst: c = nc.get(i, 0) + 1 count = max(c, count) nc[i] = nc.get(i, 0) + 1 if count == 1: count += 1 print(n - count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array of N integers. Find the *minimum* number of integers you need to delete from the array such that the absolute difference between each pair of integers in the remaining array will become equal. ------ Input Format ------ - The first line of 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 an integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of integers to be deleted to satisfy the given condition. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$. ----- Sample Input 1 ------ 3 2 1 2 5 2 5 1 2 2 4 1 2 1 2 ----- Sample Output 1 ------ 0 2 2 ----- explanation 1 ------ Test case $1$: There is only one pair of integers and the absolute difference between them is $|A_{1} - A_{2}| = |1 - 2| = 1$. So there is no need to delete any integer from the given array. Test case $2$: If the integers $1$ and $5$ are deleted, the array A becomes $[2, 2, 2]$ and the absolute difference between each pair of integers is $0$. There is no possible way to delete less than two integers to satisfy the given condition.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} m = [0, 0] for i in a: d.setdefault(i, 0) d[i] += 1 if d[i] > m[0]: m[0] = d[i] m[1] = i if n < 3: print(0) elif m[0] == 1: print(n - 2) else: print(n - m[0])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER