description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().strip().split())) a.sort() poss = True diff = [None] * 2 places = [[] for x in range(2)] for x in range(n - 1): curr = a[x + 1] - a[x] if diff[0] is None: diff[0] = curr places[0].append(x) elif curr != diff[0] and diff[1] is None: diff[1] = curr places[1].append(x) elif curr == diff[0]: places[0].append(x) elif curr == diff[1]: places[1].append(x) else: poss = False break if len(places[0]) > 1 and len(places[1]) > 1: poss = False break if poss == True: if diff[1] is None: if len(places[0]) == 0: print(-1) elif len(places[0]) == 1: if diff[0] % 2 == 0 and diff[0] != 0: print(3) print(a[0] - diff[0], a[0] + diff[0] // 2, a[1] + diff[0]) elif diff[0] == 0: print(1) print(a[0]) else: print(2) print(a[0] - diff[0], a[1] + diff[0]) elif diff[0] != 0: print(2) print(a[0] - diff[0], a[-1] + diff[0]) else: print(1) print(a[0]) elif len(places[0]) > 1: if diff[0] * 2 == diff[1]: print(1) print(a[places[1][0]] + diff[0]) else: print(0) elif len(places[1]) > 1: if diff[1] * 2 == diff[0]: print(1) print(a[places[0][0]] + diff[1]) else: print(0) elif diff[0] * 2 == diff[1]: print(1) print(a[places[1][0]] + diff[0]) elif diff[1] * 2 == diff[0]: print(1) print(a[places[0][0]] + diff[1]) else: print(0) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NONE ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NONE IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = sorted(map(int, input().split())) if n == 1: print(-1) else: if a[0] == a[-1]: p = [a[0]] else: p = [] d = list(set(a[i + 1] - a[i] for i in range(n - 1))) if len(d) == 1: p = [a[0] - d[0], a[-1] + d[0]] if n == 2 and (a[1] - a[0]) % 2 == 0: p += [(a[1] + a[0]) // 2] elif len(d) == 2: if max(d) == 2 * min(d): for i in range(n - 1): if a[i + 1] - a[i] == max(d): if len(p) == 0: p = [(a[i + 1] + a[i]) // 2] else: p = [] break print(len(p)) print(" ".join(map(str, sorted(p))))
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 VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def sequence_check(m, delta): count = 0 for i in range(1, len(m)): if m[i] - m[i - 1] != delta: if delta == 0: return -2 mistake = i count += 1 if count == 2 or m[i] == m[i - 1] and delta != 0: return -2 if delta == 0 and count == 0: return -3 elif delta == 0 and count > 0: return -2 elif count > 0: return mistake return -1 n = int(input()) m = list(map(int, input().split())) m.sort() if n == 1: print(-1) else: if len(m) >= 4: if m[1] - m[0] == m[2] - m[1]: delta = m[1] - m[0] elif m[1] - m[0] != m[2] - m[1] and m[1] - m[0] == m[3] - m[2]: delta = m[1] - m[0] else: delta = m[2] - m[1] else: delta = m[1] - m[0] for i in range(1, len(m)): if m[i] - m[i - 1] < delta: delta = m[i] - m[i - 1] result = sequence_check(m, delta) if result == -3: print(1) print(m[0]) elif result == -2: print(0) elif result == -1 and n == 2 and m[0] % 2 == m[1] % 2: print(3) print(m[0] - delta, (m[0] + m[1]) // 2, m[-1] + delta) elif result == -1: print(2) print(m[0] - delta, m[-1] + delta) elif (m[result - 1] + m[result]) % 2 == 0 and (m[result - 1] + m[result]) // 2 == m[ result ] - delta: print(1) print(m[result] - delta) else: print(0)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (n,) = I() l = sorted(I()) df = [0] * (n - 1) r = c = -1 x = 0 for i in range(1, n): df[i - 1] = l[i] - l[i - 1] s = set(df) if len(s) > 2: print(0) elif len(s) == 2: r, c = list(s) if df.count(r) == 1 and r == 2 * c: print(1) k = df.index(r) print(l[k] + c) elif df.count(c) == 1 and c == 2 * r: print(1) k = df.index(c) print(l[k] + r) else: print(0) else: r = list(s)[0] if len(s) == 1 else 0 if n == 1: print(-1) elif r == 0: print(1, "\n" + str(l[0])) elif n == 2 and r % 2 == 0: print(3) print(l[0] - r, l[0] + r // 2, l[n - 1] + r) else: print(2) print(l[0] - r, l[n - 1] + r)
IMPORT ASSIGN VAR 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 FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP STRING FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def main(): n = int(input()) if n == 1: print(-1) return elif n == 2: a, b = list(map(int, input().split())) res = {a * 2 - b, b * 2 - a} if not a + b & 1: res.add((a + b) // 2) else: l = sorted(map(int, input().split())) d, a = {}, -l[-1] for i, b in enumerate(l): x, a = b - a, b cnt, _ = d.get(x, (0, 0)) d[x] = cnt + 1, b t = sorted(d.items()) if len(t) == 2: y, (_, b) = t[0] res = {l[0] - y, b + y} elif len(t) == 3: (x, _), (y, (c2, b)), _ = t if not x or c2 > 1 or x * 2 != y: res = () else: res = (b - x,) else: res = () print(len(res)) if res: print(*sorted(res)) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) array = sorted(list(map(int, input().split()))) def all_same(items): return all(x == items[0] for x in items) if n == 1: print(-1) elif n == 2: t = max(array) - min(array) if t == 0: print(1) print(array[0]) elif t % 2 == 0: print(3) print(min(array) - t, int(min(array) + t / 2), max(array) + t) else: print(2) print(min(array) - t, max(array) + t) else: diff = [(array[i + 1] - array[i]) for i in range(n - 1)] if all_same(diff): t = array[1] - array[0] if t == 0: print(1) print(array[0]) else: print(2) print(min(array) - t, max(array) + t) elif max(diff) == 2 * min(diff) and diff.count(max(diff)) == 1: print(1) print(array[diff.index(max(diff))] + min(diff)) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = [int(i) for i in input().split()] a = sorted(a) if len(a) == 1: print(-1) elif len(a) == 2: d = a[1] - a[0] if d == 0: print(1) print(a[0]) return elif d % 2 == 0: print(3) print(a[0] - d, int(a[0] + d / 2), a[1] + d) return else: print(2) print(a[0] - d, a[1] + d) return else: d = a[1] - a[0] d2 = a[2] - a[1] if d != d2: if len(a) > 3: d = a[3] - a[2] else: d = min(d, d2) c = 0 e = 0 f = 0 for i in range(len(a) - 1): if a[i + 1] - a[i] != d: e = a[i + 1] - a[i] f = i c += 1 if c == 2: print(0) return if c == 0: if d == 0: print(1) print(a[0]) return else: print(2) print(a[0] - d, a[len(a) - 1] + d) return elif e == 2 * d: print(1) print(a[f] + d) return else: print(0) return
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER RETURN
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) if n == 1: print(-1) else: k = 0 res = [] a.sort() b1 = {i for i in a} if len(b1) == 1: print(1) print(a[0]) else: c = [] b = set() for i in range(n - 1): c.append(a[i + 1] - a[i]) b.add(a[i + 1] - a[i]) if len(b) != 1 and len(b) != 2 or 0 in b: print(0) elif len(b) == 1: k = 2 x = list(b)[0] res.append(a[0] - x) res.append(a[n - 1] + x) if n == 2: if x / 2 % 1 == 0: k += 1 res.append(int(a[0] + x / 2)) res.sort() print(k) print(" ".join(map(str, res))) elif len(b) == 2: d = [i for i in b] ma = max(d) mi = min(d) if ma / 2 == mi and c.count(ma) == 1: k = 1 res.append(a[c.index(ma)] + mi) print(k) print(" ".join(map(str, res)))
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 NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def isProgression(): nonlocal N, A, d, error ans = True for i in range(N - 1): if d != A[i + 1] - A[i]: ans = False error += 1 return ans N = int(input()) A = list(int(i) for i in input().split()) error = 0 A.sort() d = (A[N - 1] - A[0]) // N res = isProgression() if error == N - 1 and N != 1 and (A[N - 1] - A[0]) % (N - 1) == 0: d = (A[N - 1] - A[0]) // (N - 1) error = 0 res = isProgression() if N == 1: print(-1) elif N == 2 and (A[0] + A[1]) % 2 == 0 and A[0] != A[1]: print(3) d = A[1] - A[0] print(A[0] - d, (A[0] + A[1]) // 2, A[1] + d) elif res == True and d != 0: print(2) print(A[0] - d, A[N - 1] + d) elif res == True: print(1) print(A[0]) elif error > 1: print(0) else: for i in range(N - 1): if d != A[i + 1] - A[i]: if A[i + 1] - A[i] < d: print(0) elif A[i + 1] - A[i] == 2 * d: print(1) print((A[i] + A[i + 1]) // 2) else: print(0) break
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = sorted(list(map(int, input().split()))) how = sum([(1) for i in range(n) if a[0] == a[i]]) if n >= 2: d = min([(a[i] - a[i - 1]) for i in range(1, len(a))]) else: d = 0 is_ok = True for i in range(1, len(a)): if a[i] - a[i - 1] != d: is_ok = False if n == 1: print(-1) exit(0) elif how == n: print(1) print(a[0]) elif is_ok: L = [] if n == 2: if (a[0] + a[1]) % 2 == 0: L.append((a[0] + a[1]) // 2) L.append(min(a) - d) L.append(max(a) + d) L = sorted(L) print(len(L)) if len(L) > 0: for i in L: print(i, end=" ") print() else: L = [] if n == 2: if (a[0] + a[1]) % 2 == 0: L.append((a[0] + a[1]) // 2) for i in range(1, n): if 2 * d == a[i] - a[i - 1] and d != 0: L.append(a[i - 1] + d) if len(L) <= 1: valid = True for i in L: a.append(i) a = sorted(a) tmp = a[1] - a[0] for i in range(1, len(a)): if a[i] - a[i - 1] != tmp: valid = False break if valid == False: print(0) exit(0) print(len(L)) for i in L: print(i, end=" ") print() else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR LIST IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def main(): n = int(input()) l = sorted(map(int, input().split())) if n == 1: print(-1) return elif n == 2: a, b = l res = {a * 2 - b, b * 2 - a} if not a + b & 1: res.add((a + b) // 2) else: q = min(l[1] - l[0], l[-1] - l[-2]) a, res = l[0] - q, () for b in l: if q != b - a: if res or q * 2 != b - a: res = () break else: res = (b - q,) a = b else: if not res: res = {l[0] - q, b + q} print(len(res)) if res: print(*sorted(res)) main()
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 EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) a.sort() if n == 1: print(-1) elif n == 2: d = a[1] - a[0] if (a[0] + a[1]) % 2 == 0: if d == 0: print(1) print(a[0]) else: print(3) print(a[0] - d, (a[0] + a[1]) // 2, a[1] + d) else: print(2) print(a[0] - d, a[1] + d) else: diff = [(a[i + 1] - a[i]) for i in range(n - 1)] maxval = minval = a[1] - a[0] maxi = 0 for i in range(1, n - 1): val = a[i + 1] - a[i] if val > maxval: maxval = val maxi = i if val < minval: minval = val n1 = diff.count(minval) n2 = diff.count(maxval) if n1 == n - 1: if minval == 0: print(1) print(a[0]) else: print(2) print(a[0] - minval, a[-1] + minval) elif n2 == 1 and n1 == n - 2 and 2 * minval == maxval: print(1) print(a[maxi] + minval) else: print(0)
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) arr = [int(x) for x in input().split()] arr.sort() if n == 1: print(-1) else: ans = [] if n == 2: if (arr[1] - arr[0]) % 2 == 0: d = arr[1] - arr[0] if d != 0: ans.append(arr[0] - d) ans.append(arr[0] + d // 2) ans.append(arr[1] + d) else: ans.append(arr[0] + d // 2) else: d = arr[1] - arr[0] ans.append(arr[0] - d) ans.append(arr[1] + d) else: dic = {} for i in range(n - 1): d = arr[i + 1] - arr[i] if d in dic: dic[d] += 1 else: dic[d] = 1 if len(dic) == 1: for ele in dic: d = ele num = dic[ele] if d == 0: ans.append(arr[0] - d) else: ans.append(arr[0] - d) ans.append(arr[-1] + d) elif len(dic) == 2: m = float("inf") d = -1 m1, d2 = -float("inf"), -1 for ele in dic: if dic[ele] < m: m = dic[ele] d = ele elif dic[ele] == m: if ele > d: m = dic[ele] d = ele if dic[ele] > m1: m1 = dic[ele] d1 = ele elif dic[ele] == m1: if ele < d: m1 = dic[ele] d1 = ele if dic[d] == 1: if d % 2 == 0 and d // 2 == d1: for i in range(n - 1): if arr[i + 1] - arr[i] == d: ans.append(arr[i] + d // 2) break if len(ans) == 0: print(0) else: print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) def main(): lst = sorted(list(map(int, input().split(" ")))) dct = set() for i in range(len(lst) - 1): dct.add(lst[i + 1] - lst[i]) if len(dct) > 2: print(0) elif len(dct) == 2 and 0 in dct: print(0) elif len(dct) == 2: if 2 * min(dct) == max(dct): cnt = 0 for i in range(len(lst) - 1): if lst[i + 1] - lst[i] == max(dct): ans = (lst[i] + lst[i + 1]) // 2 cnt += 1 if cnt >= 2: print(0) return print(1) print(ans) else: print(0) elif len(lst) == 1: print(-1) elif len(dct) == 1 and len(lst) > 2: if list(dct)[0] == 0: print(1) print(lst[0]) else: print(2) print(lst[0] - lst[1] + lst[0], end=" ") print(lst[-1] + lst[1] - lst[0]) elif len(dct) == 1 and len(lst) == 2: if list(dct)[0] == 0: print(1) print(lst[0]) else: ans = [] ans.append(lst[0] - lst[1] + lst[0]) ans.append(lst[-1] + lst[1] - lst[0]) t = 1.0 * (lst[0] + lst[1]) / 2 if t % 1 == 0: print(3) ans.append(int(t)) else: print(2) print(" ".join(map(str, sorted(ans)))) main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
from sys import stdin def solve(tc): n = int(stdin.readline().strip()) seq = sorted(list(map(int, stdin.readline().split()))) ans = [] if n == 1: print(-1) return if n == 2: diff = seq[1] - seq[0] if diff == 0: print(1) print(seq[0]) return ans.append(seq[0] - diff) if diff % 2 == 0: ans.append(seq[0] + diff // 2) ans.append(seq[1] + diff) print(len(ans)) print(" ".join(map(lambda x: str(x), ans))) return mindiff = int(1e18) for i in range(1, n): mindiff = min(mindiff, seq[i] - seq[i - 1]) for i in range(1, n): diff = seq[i] - seq[i - 1] if diff > mindiff: if diff != mindiff * 2: print(0) return if len(ans) > 0: print(0) return ans.append(seq[i - 1] + mindiff) if len(ans) == 0: if mindiff == 0: print(1) print(seq[0]) return ans.append(seq[0] - mindiff) ans.append(seq[-1] + mindiff) print(len(ans)) print(" ".join(map(lambda x: str(x), ans))) tcs = 1 for tc in range(tcs): solve(tc)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) num = [int(k) for k in input().split()] num.sort() diff = [0] * (n - 1) for i in range(n - 1): diff[i] = num[i + 1] - num[i] if n == 1: print(-1) elif len(diff) > 1: same = True for i in range(len(diff) - 1): if diff[i] != diff[i + 1]: same = False if same == True: if diff[0] == 0: print(1) print(num[0]) else: print(2) print(num[0] - diff[0], num[n - 1] + diff[0]) elif diff.count(diff[diff.index(min(diff))]) != len(diff) - 1: print(0) elif 2 * diff[diff.index(min(diff))] != diff[diff.index(max(diff))]: print(0) else: print(1) print(num[diff.index(max(diff))] + diff[diff.index(min(diff))]) elif len(diff) == 1: if diff[0] == 0: print(1) print(num[0]) elif (num[0] + num[1]) / 2 % 1 == 0: print(3) print(num[0] - diff[0], int((num[0] + num[1]) / 2), num[1] + diff[0]) else: print(2) print(num[0] - diff[0], num[1] + diff[0])
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
for _ in range(1): n = int(input()) arr = [int(i) for i in input().split()] arr.sort() l = [] if n == 1: print(-1) elif n == 2: if arr[1] - arr[0] == 0: print(1) print(arr[0]) elif (arr[1] - arr[0]) % 2 == 0: print(3) print(2 * arr[0] - arr[1], (arr[0] + arr[1]) // 2, 2 * arr[1] - arr[0]) else: print(2) print(2 * arr[0] - arr[1], 2 * arr[1] - arr[0]) else: for i in range(n - 1): l.append(arr[i + 1] - arr[i]) if len(set(l)) == 1 and l[0] == 0: print(1) print(arr[0]) elif len(set(l)) == 1: print(2) print(2 * arr[0] - arr[1], 2 * arr[n - 1] - arr[n - 2]) elif len(set(l)) == 2: l = list(set(l)) l.sort() if l[0] * 2 == l[1]: count = 0 for i in range(n - 1): if arr[i + 1] - arr[i] == l[1]: count += 1 index = i if count == 1: print(1) print(arr[index] + l[0]) else: print(0) else: print(0) else: print(0)
FOR VAR FUNC_CALL VAR NUMBER 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 LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) a.sort() diff, s, i, val = set(), set(), 0, 0 while i < n - 1: diff.add(a[i + 1] - a[i]) i += 1 diff = list(diff) diff.sort() if n == 1: print(-1) elif n == 2: s.add(a[0] - diff[0]) s.add(a[1] + diff[0]) if diff[0] % 2 == 0: s.add(a[0] + diff[0] // 2) print(len(s)) s = list(s) s.sort() for ele in s: print(ele, end=" ") else: if len(diff) == 1: s.add(a[0] - diff[0]) s.add(a[n - 1] + diff[0]) elif len(diff) == 2: if diff[0] == diff[1] / 2: i = 0 while i < n - 1: if a[i + 1] - a[i] == diff[1]: s.add(a[i] + diff[0]) val += 1 i += 1 if val > 1: print(0) else: print(len(s)) s = list(s) s.sort() for ele in s: print(ele, end=" ")
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 VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) arr = [int(x) for x in input().split()] arr.sort() if n == 1: print("-1") elif n == 2: diff = arr[1] - arr[0] if diff % 2 == 0 and diff != 0: print("3") print(arr[0] - diff, (arr[0] + arr[1]) // 2, arr[1] + diff) elif diff == 0: print("1") print(arr[0]) else: print("2") print(arr[0] - diff, arr[1] + diff) elif n == 3: diff1 = arr[1] - arr[0] diff2 = arr[2] - arr[1] if diff1 == diff2 and diff1 != 0: print("2") print(arr[0] - diff1, arr[2] + diff1) elif diff1 == diff2 and diff1 == 0: print("1") print(arr[1]) elif diff1 == 2 * diff2: print("1") print(arr[0] + diff2) elif diff2 == 2 * diff1: print("1") print(arr[1] + diff1) else: print("0") else: diff = dict() keys = 0 for i in range(1, len(arr)): cdiff = arr[i] - arr[i - 1] if cdiff in diff: diff[cdiff] += 1 else: keys += 1 diff[cdiff] = 1 if keys > 2: break if keys > 2: print("0") elif keys == 1: cdiff = list(diff)[0] if cdiff == 0: print("1") print(arr[0]) else: print("2") print(arr[0] - cdiff, arr[n - 1] + cdiff) elif keys == 2: diff1 = list(diff)[0] diff2 = list(diff)[1] if diff[diff1] > diff[diff2]: maxd = diff1 mind = diff2 else: maxd = diff2 mind = diff1 if mind == 2 * maxd and diff[mind] == 1: print("1") pos = 0 for i in range(0, len(arr) - 1): d = arr[i + 1] - arr[i] if d == mind: pos = i break print(arr[pos] + maxd) else: print("0")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) if n == 1: print(-1) return a.sort() d = a[1] - a[0] if n == 2: if d == 0: print(1) print(a[0]) elif d % 2 == 1: print(2) print(a[0] - d, a[1] + d) else: print(3) print(a[0] - d, sum(a) // 2, a[1] + d) return d_list = [(a[i] - a[i - 1]) for i in range(1, n)] d2c = {} for d in d_list: if d not in d2c: d2c[d] = 1 else: d2c[d] += 1 if len(d2c) == 1: d1, c1 = d2c.popitem() if d1 == 0: print(1) print(a[0]) else: print(2) print(a[0] - d, a[-1] + d) return if len(d2c) == 2: d1, c1 = d2c.popitem() d2, c2 = d2c.popitem() if c1 == 1 and d1 == d2 * 2: print(1) print(a[d_list.index(d1)] + d2) elif c2 == 1 and d2 == d1 * 2: print(1) print(a[d_list.index(d2)] + d1) else: print(0) else: print(0)
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 RETURN EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
import sys t = 1 mod = 10**9 + 7 for __ in range(t): n = int(input()) l = list(map(int, input().split())) l.sort() diff = {} d = {} for i in range(n - 1): diff[l[i + 1] - l[i]] = diff.get(l[i + 1] - l[i], 0) + 1 if d.get(l[i + 1] - l[i], -1) == -1: d[l[i + 1] - l[i]] = [] d[l[i + 1] - l[i]].append(l[i + 1]) if len(diff) >= 3: print(0) elif len(diff) == 2: ch = 0 o, t = 0, 0 for i in diff: if ch == 0: o = i else: t = i ch += 1 ch = 0 f, s = 0, 0 cnt = 0 a1 = [] if diff[o] == 1 and o % 2 == 0 and o // 2 == t: f = i ch1 = 0 ind1 = 0 for i in range(n - 1): if l[i + 1] - l[i] == o: a1.append(l[i] + t) cnt += 1 if diff[t] == 1 and t % 2 == 0 and t // 2 == o: s = o ch1 = 0 ind1 = 0 for i in range(n - 1): if l[i + 1] - l[i] == t: a1.append(l[i] + o) cnt += 1 print(len(a1)) if len(a1) > 0: a1.sort() print(*a1) elif len(diff) == 1: a1 = [] f = 0 for i in diff: f = i if diff[f] == 1 and f % 2 == 0: a1.append(d[f][-1] - f // 2) a1.append(l[0] - f) a1.append(l[-1] + f) a1.sort() if f == 0: a1 = [] a1.append(f + l[0]) print(len(a1)) print(*a1) else: print(-1)
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) temparr = input() temparr = temparr.split() arr = [] for i in temparr: arr.append(int(i)) arr = sorted(arr) if n == 1: print(-1) elif n == 2: diff = arr[1] - arr[0] if diff & 1: print(2) ans = str(arr[0] - diff) + " " + str(arr[1] + diff) print(ans) elif diff == 0: print(1) print(arr[0]) else: print(3) half = diff // 2 midd = arr[0] + half ans = str(arr[0] - diff) + " " + str(midd) + " " + str(arr[1] + diff) print(ans) else: dicts = {} for i in range(1, n): diff = arr[i] - arr[i - 1] if diff not in dicts: dicts[diff] = 1 else: dicts[diff] += 1 if len(dicts) >= 3: print("0") elif len(dicts) >= 2 and 0 in dicts: print("0") elif len(dicts) == 1: if arr[1] - arr[0] == 0: print("1") print(arr[0]) else: print("2") first = arr[0] - diff last = arr[-1] + diff strsans = str(first) + " " + str(last) print(strsans) else: flag = 0 firstkey = 0 firstval = 0 seckey = 0 secval = 0 for key, value in dicts.items(): if flag == 0: firstval = value firstkey = key flag = 1 else: secval = value seckey = key lowerkey = min(firstkey, seckey) higherkey = max(firstkey, seckey) firstkey = lowerkey seckey = higherkey secval = dicts[higherkey] firstval = dicts[lowerkey] if firstval >= 1 and secval >= 2: print("0") else: minskey = min(firstkey, seckey) maxskey = max(firstkey, seckey) if minskey * 2 != maxskey: print(0) else: ans = 0 for i in range(1, n): if arr[i] - arr[i - 1] == maxskey: ans = arr[i - 1] + minskey break print(1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) if n == 1: print(-1) exit(0) b = [*map(int, input().split())] a = sorted(b) l = set() if n == 2 and a[1] - a[0] != 0 and (a[1] - a[0]) % 2 == 0: print(3) print(a[0] - (a[1] - a[0]), a[0] + (a[1] - a[0]) // 2, a[-1] + (a[1] - a[0])) exit(0) elif n == 2 and (a[1] - a[0]) % 2 != 0: print(2) print(a[0] - (a[1] - a[0]), a[-1] + (a[1] - a[0])) exit(0) d = min(a[i + 1] - a[i] for i in range(n - 1)) c = 0 s = set() for i in range(n - 1): if a[i + 1] - a[i] == d: continue elif a[i + 1] - a[i] == 2 * d: s.add(a[i] + d) else: print(0) exit(0) if s: if len(s) > 1: print(0) exit(0) print(len(s)) print(*s) else: l = {a[0] - d, a[-1] + d} print(len(l)) print(*sorted(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
import sys mod, MOD = 1000000007, 998244353 def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() def check_sequence(D): mydict = dict() for i in D: mydict[i] = mydict.get(i, 0) + 1 maxi = max(D) mini = min(D) if mydict[maxi] != 1 or maxi != 2 * mini: return True return False n = int(input()) Arr = get_array() Arr.sort() if n == 1: print(-1) exit() if n == 2: if Arr[1] - Arr[0] == 0: print(1) print(Arr[0]) elif Arr[1] - Arr[0] & 1: print(2) print(Arr[0] - (Arr[1] - Arr[0]), Arr[1] + (Arr[1] - Arr[0])) else: print(3) print( Arr[0] - (Arr[1] - Arr[0]), Arr[0] + (Arr[1] - Arr[0]) // 2, Arr[1] + (Arr[1] - Arr[0]), ) exit() D = [] for i in range(1, n): D.append(Arr[i] - Arr[i - 1]) if len(set(D)) == 1: if D[0] == 0: print(1) print(Arr[0]) else: print(2) print(Arr[0] - (Arr[1] - Arr[0]), Arr[-1] + (Arr[1] - Arr[0])) exit() if len(set(D)) >= 3 or check_sequence(D): print(0) exit() d = Arr[1] - Arr[0] ans = Arr[0] + (Arr[1] - Arr[0]) // 2 for i in range(1, n): if Arr[i] - Arr[i - 1] > d: ans = Arr[i - 1] + (Arr[i] - Arr[i - 1]) // 2 print(1) print(ans)
IMPORT ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
__author__ = "asmn" n = int(input()) a = sorted(map(int, input().split())) def checked(a, d): for i in range(1, len(a)): if a[i] - a[i - 1] != d: return False return True td = None def canfix(a): a = sorted(a[i] - a[i - 1] for i in range(1, len(a))) if a[-1] != a[0] * 2: return False elif sum(a) != a[0] * (len(a) + 1): return False else: global td td = a[0] return True def fix(a, d): for i in range(1, len(a)): if a[i] - a[i - 1] != d: return a[i] - d if n == 1: print(-1) elif n == 2: if sum(a) % 2 == 0: if a[0] == a[1]: print(1) print(a[0]) else: print(3) print(*(2 * a[0] - a[1], sum(a) // 2, 2 * a[1] - a[0])) else: print(2) print(*(2 * a[0] - a[1], 2 * a[1] - a[0])) else: d = a[1] - a[0] if checked(a, d): if d == 0: print(1) print(a[0]) else: print(2) print(a[0] - d, a[-1] + d) elif canfix(a): print(1) print(fix(a, td)) else: print(0)
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) t = list(map(int, input().split())) if n == 1: q = "-1" else: t.sort() d = min(t[1] - t[0], t[-1] - t[-2]) q = "2\n" + str(t[0] - d) + " " + str(t[-1] + d) if d else "1\n" + str(t[0]) if n == 2: if d and d % 2 == 0: q = "3\n" + str(t[0] - d) + " " + str(t[0] + d // 2) + " " + str(t[-1] + d) else: for i in range(1, n): if t[i] - t[i - 1] != d: if t[i] - t[i - 1] == 2 * d and q[0] == "2": q = "1\n" + str(t[i] - d) else: q = "0" break print(q)
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 ASSIGN VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER STRING ASSIGN VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) a.sort() if n == 1: print(-1) elif n == 2: if (a[1] - a[0]) % 2 == 1 or a[1] == a[0]: df = a[1] - a[0] if df == 0: print(1) print(a[0]) else: print(2) print(a[0] - df, a[1] + df) else: print(3) df = a[1] - a[0] print(a[0] - df, (a[0] + a[1]) // 2, a[1] + df) else: d = {} for i in range(1, n): try: k = d[a[i] - a[i - 1]] d[a[i] - a[i - 1]] += 1 except: d[a[i] - a[i - 1]] = 1 k = min(d.keys()) c = 0 b = -1 for i in d.keys(): if i != k: c += d[i] b = i if c > 1: print(0) elif c == 1 and k == 0: print(0) elif c == 0 and k == 0: print(1) print(a[0]) elif c == 0: print(2) k1 = min(a[0], a[len(a) - 1]) k2 = max(a[0], a[len(a) - 1]) print(k1 - k, k2 + k) elif b % 2 == 1 or b // 2 != k: print(0) else: u = -1 for i in range(1, n): if a[i] - a[i - 1] == b: u = a[i - 1] break print(1) print(u + k)
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def discos(): n = int(input()) space = list() inserts = list() pos = int() posn = int() count = int() jump = int() numbers = [int(x) for x in input().split()] numbers.sort() if n == 1: print(-1) return elif n == 2: if numbers[0] == numbers[1]: print(1) print(numbers[0]) return dif = numbers[1] - numbers[0] inserts.append(numbers[0] - dif) inserts.append(numbers[1] + dif) if dif % 2 == 0: inserts.insert(1, numbers[0] + dif // 2) print(3) print(" ".join(str(x) for x in inserts)) return print(2) print(" ".join(str(x) for x in inserts)) return else: for i in range(0, n - 1): space.append(numbers[i + 1] - numbers[i]) for i in range(0, len(space) - 1): if space[i] != space[i + 1] or (i + 1 == len(space) - 1) & ( i != 1 & (count > 0 & (space[i + 1] != space[i - 1])) ): count += 1 posn = i + 1 if count > 0: if (space[i - 1] == space[i + 1]) & (i != 0): count -= 1 posn = pos if len(space) == 2: if space[1] == space[0] * 2: print(1) print(numbers[i + 1] + space[0]) return elif space[0] == space[1] * 2: print(1) print(numbers[i] + space[1]) return elif i == 0: if space[i] != space[i + 2]: posn = i elif jump == 0: jump = space[i] pos = posn if count == 0: if jump == 0: print(1) print(numbers[0]) return inserts.append(numbers[0] - space[0]) inserts.append(numbers[n - 1] + space[0]) print(2) print(" ".join(str(x) for x in inserts)) return elif count == 1: if space[pos] == 0 or space[pos - 1] == 0: print(0) return if numbers[pos + 1] - numbers[pos] == jump * 2: print(1) print(numbers[pos] + jump) return print(0) return discos()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN IF VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = sorted(map(int, input().split())) if n < 2: print(-1) else: count = {} I = range(n - 1) for i in I: d = a[i + 1] - a[i] count[d] = count.get(d, 0) + 1 d = min(count) if count[d] < n - 2: print(0) else: b = set() if count[d] + 1 == n: b.add(int(a[0] * 2 - a[1])) b.add(int(a[-1] * 2 - a[-2])) for i in I: e = a[i + 1] - a[i] if not e % 2: f = e / 2 count[f] = count.get(f, 0) + 2 count[e] -= 1 if count[min(d, f)] == n: b.add(int(a[i] + f)) count[e] += 1 count[f] -= 2 print(len(b)) print(" ".join(map(str, sorted(b))))
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 FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
n = int(input()) a = list(map(int, input().split())) a.sort() r = 0 res = [] def isAri(a): diff = a[1] - a[0] for i in range(len(a) - 1): if not a[i + 1] - a[i] == diff: return False return True if n == 1: print(-1) elif isAri(a) and a[1] - a[0] == 0: r += 1 print(r) print(a[0]) elif isAri(a): r += 1 res.append(a[0] - (a[1] - a[0])) r += 1 res.append(a[len(a) - 1] + (a[1] - a[0])) if n == 2 and (a[0] + a[1]) % 2 == 0: r += 1 res.append(int((a[0] + a[1]) / 2)) res.sort() print(r) for i in range(0, len(res)): print(res[i], end=" ") else: diff = a[1] - a[0] if a[1] - a[0] == 2 * (a[2] - a[1]): diff = a[2] - a[1] errorIndex = -1 errors = 0 for i in range(0, len(a) - 1): curr = a[i + 1] - a[i] if curr != diff: errors += 1 if curr == 2 * diff: errorIndex = i if errors == 1 and errorIndex != -1: print(1) print(a[errorIndex] + diff) else: print(0)
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 NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5) β€” the number of cards. The next line contains the sequence of integers β€” the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8. -----Output----- If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples). -----Examples----- Input 3 4 1 7 Output 2 -2 10 Input 1 10 Output -1 Input 4 1 3 5 9 Output 1 7 Input 4 4 3 4 5 Output 0 Input 2 2 4 Output 3 0 3 6
def func(n, l1): if n == 1: print(-1) return 0 s = set(l1) if len(s) == 1: print(1) print(l1[0]) return 0 l1.sort() diff = min(l1[-1] - l1[-2], l1[1] - l1[0]) l = [] if n == 2: if diff == 0: print(1) print(l1[0]) return 0 l.append(l1[0] - diff) if diff & 1 == 0: l.append(l1[0] + diff // 2) l.append(l1[1] + diff) print(len(l)) print(*l) return 0 for i in range(1, n): d = l1[i] - l1[i - 1] if d < diff: print(0) return 0 elif d == diff: continue else: if d == 2 * diff: l.append(l1[i] - diff) else: print(0) return 0 if len(l) > 1: print(0) return 0 if len(l) == 1: print(1) print(l[0]) return 0 print(2) print(l1[0] - diff, l1[-1] + diff) n = int(input()) l1 = list(map(int, input().split())) func(n, l1)
FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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 VAR VAR
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
def ask(x): s = {} s[0] = 1 sum, cnt, res = 0, 0, 0 for i in range(n): if a[i] < x: sum -= 1 cnt -= s.get(sum, 0) else: cnt += s.get(sum, 0) sum += 1 s[sum] = s.get(sum, 0) + 1 res += cnt return res n, m = map(int, input().split()) a = list(map(int, input().split())) print(ask(m) - ask(m + 1))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR ASSIGN 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 EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
class BinaryIndexedTree: def __init__(self, n): self.bit = [0] * n def add(self, i, x): i += 1 while i <= len(self.bit): self.bit[i - 1] += x i += i & -i def sum_sub(self, i): a = 0 i += 1 while i: a += self.bit[i - 1] i -= i & -i return a def sum(self, i, j): a = 0 if j != 0: a += self.sum_sub(j - 1) if i != 0: a -= self.sum_sub(i - 1) return a def f(m): ans = 0 bit = BinaryIndexedTree(2 * n + 7) f = 0 for i in range(n): bit.add(n + f, 1) if a[i] > m: f -= 1 else: f += 1 ans += bit.sum_sub(n + f) return ans n, m = map(int, input().split()) a = list(map(int, input().split())) print(f(m) - f(m - 1))
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN 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 EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
def Yeee(x, v, n): re = 0 pre = 0 sum = 1 cnt = [0] * n + [1] + [0] * n for i in v: if i < x: pre += 1 sum += cnt[pre + n] else: sum -= cnt[pre + n] pre -= 1 cnt[pre + n] += 1 re += sum return re n, x = list(map(int, input().split())) v = [int(i) for i in input().split()] print(Yeee(x + 1, v, n) - Yeee(x, v, n))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
MAXN = 200001 def less_sum(s, m): n = len(s) a = 0 b = 0 res = 0 last = 0 count = [(0) for i in range(-MAXN, MAXN + 1)] count[0] = 1 x = 0 last = 1 for i in range(n): if s[i] > m: b += 1 else: a += 1 x = a - b if s[i] > m: last -= count[x + 1] else: last += count[x] res += last count[x] += 1 last += 1 return res n, m = map(int, input().split(" ")) s = list(map(int, input().split(" ")))[0:n] print(less_sum(s, m) - less_sum(s, m - 1))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
n, m = map(int, input().split()) a = list(map(int, input().split())) class BIT: def __init__(self, n): self.n = n self.data = [0] * (n + 1) def to_sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): while i <= self.n: self.data[i] += x i += i & -i def get(self, i, j): return self.to_sum(j) - self.to_sum(i - 1) def f(x, V): if x < V: return -1 return 1 def calc_median(M): b = [f(v, M) for v in a] res = 0 c = [0] for x in b: c.append(c[-1] + x) d = [(c[i], i) for i in range(n + 1)] bit = BIT(2 * n + 10) for value, index in d: if index == 0: bit.add(value + n + 1, 1) continue res += bit.get(1, value + n) bit.add(value + n + 1, 1) return res print(calc_median(m) - calc_median(m + 1))
ASSIGN 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 CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
L1 = list(map(int, input().split())) numList = list(map(int, input().split())) length = L1[0] m = L1[1] def greaterCount(numList, m): countDic = {(0): 1} sum = 0 total = 0 rem = 0 for number in numList: if number >= m: sum += 1 rem += countDic[sum - 1] total += rem else: sum -= 1 if sum in countDic: rem -= countDic[sum] total += rem if sum in countDic: countDic[sum] += 1 else: countDic[sum] = 1 return total print(greaterCount(numList, m) - greaterCount(numList, m + 1))
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 VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
n, m = map(int, input().split()) a = [int(x) for x in input().split()] def gC(m, a): s = [(0) for x in range(2 * n + 1)] sum = n res = 0 s[sum] = 1 ad = 0 for i in range(n): if a[i] < m: sum -= 1 ad -= s[sum] else: ad += s[sum] sum += 1 res += ad s[sum] += 1 return res print(gC(m, a) - gC(m + 1, a))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
def main(): n, m = map(int, input().split()) l = list(map(int, input().split())) res = [] for m in (m, m - 1): r = c = 0 cnt = [0] * 400002 cnt[0] = last = 1 for a in l: if a > m: c -= 1 last -= cnt[c + 1] else: c += 1 last += cnt[c] r += last cnt[c] += 1 last += 1 res.append(r) print(res[0] - res[1]) main()
FUNC_DEF ASSIGN 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 LIST FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
def grCount(m, n, a): s = [0] * (2 * n + 1) sx = n result = 0 s[sx] = 1 add = 0 for i in range(n): if a[i] < m: sx -= 1 add -= s[sx] else: add += s[sx] sx += 1 result += add s[sx] += 1 return result n, m = map(int, input().split()) a = list(map(int, input().split())) print(grCount(m, n, a) - grCount(m + 1, n, a))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN 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 EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$). -----Output----- Print the required number. -----Examples----- Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
class BIT: def __init__(self, n): self.BIT = [0] * (n + 1) self.num = n def query(self, idx): res_sum = 0 while idx > 0: res_sum += self.BIT[idx] idx -= idx & -idx return res_sum def update(self, idx, x): while idx <= self.num: self.BIT[idx] += x idx += idx & -idx return n, m = map(int, input().split()) a = list(map(int, input().split())) def solve(x): tmp = [(0) for i in range(n)] for i in range(n): if a[i] > x: tmp[i] = -1 else: tmp[i] = 1 tmp[i] += tmp[i - 1] tmp = [0] + tmp val = list(set([tmp[j] for j in range(n + 1)])) val.sort() comp = {i: (e + 1) for e, i in enumerate(val)} bit = BIT(n + 1) res = 0 for i in range(n + 1): res += bit.query(comp[tmp[i]]) bit.update(comp[tmp[i]], 1) return res print(solve(m) - solve(m - 1))
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN ASSIGN 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 FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() res = 0 low = 1 high = stalls[n - 1] - stalls[0] while low <= high: mid = low + high >> 1 if self.can_place_cow(stalls, n, k, mid): res = mid low = mid + 1 else: high = mid - 1 return res def can_place_cow(self, stalls, n, k, mid): first = stalls[0] count = 1 for i in range(1, n): if stalls[i] - first >= mid: count += 1 first = stalls[i] if count == k: return True return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def correct(self, n, k, stalls, dis): c = 1 cor = stalls[0] for i in range(1, n): if abs(stalls[i] - cor) >= dis: cor = stalls[i] c += 1 return c >= k def solve(self, n, k, stalls): stalls.sort() low = 1 high = stalls[n - 1] - stalls[0] res = low while low <= high: mid = low + high >> 1 if self.correct(n, k, stalls, mid): res = mid low = mid + 1 else: high = mid - 1 return high
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): def isPossible(n, k, stalls, maxi): cow, cur = 1, stalls[0] for i in range(n): if stalls[i] - cur >= maxi: cow += 1 if cow == k: return True cur = stalls[i] return False ans = -1 l, r = 0, max(stalls) stalls.sort() while l <= r: mid = l + (r - l) // 2 if isPossible(n, k, stalls, mid): ans = mid l = mid + 1 else: r = mid - 1 return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, arr): arr.sort() low = 0 high = arr[n - 1] - arr[0] res = [] while low <= high: mid = low + (high - low) // 2 if self.cow_Place(arr, n, k, mid): res.append(mid) low = mid + 1 else: high = mid - 1 return max(res) def cow_Place(self, arr, n, k, mid): i = 0 j = 1 count = 1 while j < n: if arr[j] - arr[i] >= mid: count += 1 i = j if count == k: return True j += 1 return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): def isPossible(stalls, n, cows, minDist): count = 1 lastPlacedCow = stalls[0] for i in range(1, n): if stalls[i] - lastPlacedCow >= minDist: count += 1 lastPlacedCow = stalls[i] if count >= cows: return 1 return 0 stalls.sort() low = 1 high = stalls[n - 1] - stalls[0] while low <= high: mid = (low + high) // 2 if isPossible(stalls, n, k, mid): low = mid + 1 else: high = mid - 1 return high
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def valid(self, n, k, stalls, mid): pos = stalls[0] cc = 1 for i in range(1, n): if stalls[i] - pos >= mid: cc += 1 pos = stalls[i] if cc == k: return True return False def solve(self, n, k, stalls): stalls = sorted(stalls) mindist = stalls[0] maxdist = stalls[n - 1] low = 0 high = maxdist - mindist ans = 1 while low <= high: mid = (low + high) // 2 if self.valid(n, k, stalls, mid): ans = mid low = mid + 1 else: high = mid - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() def helper(mid, k): last = stalls[0] k -= 1 for i in range(1, n): curr = stalls[i] if curr - last >= mid: last = curr k -= 1 if k == 0: return True return False l = 0 r = stalls[n - 1] - stalls[0] ans = 0 while l <= r: mid = l + (r - l) // 2 if helper(mid, k): ans = max(ans, mid) l = mid + 1 else: r = mid - 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def ispossible(self, stalls, mid, k): cow = 1 lastcow = stalls[0] for i in range(1, len(stalls)): if stalls[i] >= lastcow + mid: cow += 1 lastcow = stalls[i] if cow == k: return True return False def solve(self, n, k, stalls): stalls = sorted(stalls) left = 1 right = max(stalls) - min(stalls) while left < right: mid = (left + right) // 2 if self.ispossible(stalls, mid, k): left = mid + 1 else: right = mid return left - 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def AllocateCows(self, arr, count_cows, gap_limit): count_cows -= 1 position = arr[0] for i in range(1, len(arr)): if arr[i] - position >= gap_limit: count_cows -= 1 position = arr[i] if count_cows == 0: return True else: return False def solve(self, n, k, stalls): if k > len(stalls): return -1 feasible_ans = -1 stalls.sort() l = 1 h = stalls[-1] - stalls[0] while l <= h: mid = l + (h - l) // 2 if self.AllocateCows(stalls, k, mid): feasible_ans = mid l = mid + 1 else: h = mid - 1 return feasible_ans
CLASS_DEF FUNC_DEF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): low, high = 1, max(stalls) - min(stalls) res = 0 stalls.sort() while low <= high: mid = (low + high) // 2 if self.helper_func(mid, k, stalls): res = mid low = mid + 1 else: high = mid - 1 return res def helper_func(self, mid, k, stalls): curr_idx = 0 count = 1 for i in range(len(stalls)): if stalls[i] - stalls[curr_idx] >= mid: count += 1 curr_idx = i if count == k: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def check(self, mid, stalls, k): temp = stalls[0] count = 1 for i in stalls: if i - temp >= mid: temp = i count += 1 if count == k: return 1 return 0 def solve(self, n, k, stalls): stalls.sort() ans = 1 min_diff = 1 max_diff = stalls[-1] - stalls[0] while min_diff <= max_diff: mid = (min_diff + max_diff) // 2 if self.check(mid, stalls, k): min_diff = mid + 1 ans = max(ans, mid) else: max_diff = mid - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() if k > n: return -1 low, high = 0, stalls[-1] - stalls[0] res = 1 def canPlace(val): count = 1 cow = stalls[0] for i in range(1, n): if stalls[i] - cow >= val: count += 1 cow = stalls[i] if count >= k: return True return False while low <= high: mid = (low + high) // 2 if canPlace(mid): res = max(res, mid) low = mid + 1 else: high = mid - 1 return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): pass stalls.sort() def feasible(sep): prev = stalls[0] yes = 1 for i in range(1, len(stalls)): if stalls[i] - prev >= sep: yes += 1 prev = stalls[i] return yes >= k l = 1 h = stalls[n - 1] - stalls[0] while l <= h: mid = (l + h) // 2 if feasible(mid): l = mid + 1 else: h = mid - 1 return h
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def ispossible(self, arr, cows, gap): curr_stall = arr[0] cows -= 1 for num in arr[1:]: if cows == 0: break if num - curr_stall >= gap: cows -= 1 curr_stall = num return -1 if cows != 0 else gap def solve(self, n, k, stalls): stalls.sort() min_gap = 1 max_gap = max(stalls) while min_gap <= max_gap: curr_gap = (min_gap + max_gap) // 2 canbeplaced = self.ispossible(stalls, k, curr_gap) if canbeplaced != -1: candidate = curr_gap min_gap = curr_gap + 1 else: max_gap = curr_gap - 1 return candidate
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def allocation(self, stalls, mid, k): count = 1 start = 0 for i in range(len(stalls)): if stalls[i] - stalls[start] >= mid: count += 1 start = i if count < k: return False return True def solve(self, n, k, stalls): start = 1 end = max(stalls) - min(stalls) stalls.sort() res = -1 while start <= end: mid = start + (end - start) // 2 if self.allocation(stalls, mid, k): res = mid start = mid + 1 else: end = mid - 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
def can_place_cows(stalls, n, k, distance): pos = stalls[0] cnt = 1 for i in range(1, n): if stalls[i] - pos >= distance: pos = stalls[i] cnt += 1 if cnt == k: return True return False class Solution: def solve(self, n, k, stalls): stalls.sort() high = stalls[n - 1] - stalls[0] low = 1 res = 0 while low <= high: mid = (low + high) // 2 if can_place_cows(stalls, n, k, mid): res = mid low = mid + 1 else: high = mid - 1 return res
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def bn(self, k, mid, arr): pos = arr[0] cow = 1 for i in arr: if i - pos >= mid: cow += 1 if cow == k: return True pos = i return False def solve(self, n, k, stalls): stalls.sort() s = 0 e = max(stalls) mid = (s + e) // 2 ans = -1 while s <= e: if self.bn(k, mid, stalls): ans = mid s = mid + 1 else: e = mid - 1 mid = (s + e) // 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): pass stalls.sort() left, right, ans = 0, stalls[-1] - stalls[0], -1 while left <= right: mid = left + (right - left) // 2 cows, last_pos = 1, stalls[0] for i in range(1, n): if stalls[i] - last_pos >= mid: cows += 1 last_pos = stalls[i] if cows >= k: ans = mid left = mid + 1 else: right = mid - 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
import sys class Solution: def solve(self, n, k, stalls): l = 1 stalls.sort() r = stalls[-1] - stalls[0] mindiff = -sys.maxsize while l <= r: mid = (l + r) // 2 last = -sys.maxsize diff = sys.maxsize c = 0 for i in range(n): if stalls[i] - last >= mid: diff = min(diff, stalls[i] - last) last = stalls[i] c += 1 if c == k: break if c == k: mindiff = max(mindiff, diff) l = mid + 1 else: r = mid - 1 return mindiff
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() i, j = 1, stalls[-1] - stalls[0] if k == 2: return j while i <= j: m = i + j >> 1 c = 1 curr = 0 for x in range(1, n): dst = stalls[x] - stalls[curr] if dst >= m: c += 1 curr = x if c >= k: i = m + 1 else: j = m - 1 return i - 1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def isPossible(self, n, k, stalls, mid): cowcount = 1 position = stalls[0] for i in range(1, len(stalls)): if stalls[i] - position >= mid: cowcount += 1 if cowcount == k: return True position = stalls[i] return False def solve(self, n, k, stalls): stalls.sort() start = 0 end = stalls[n - 1] mid = start + (end - start) // 2 ans = -1 while start <= end: if self.isPossible(n, k, stalls, mid): ans = mid start = mid + 1 else: end = mid - 1 mid = start + (end - start) // 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): if k > n: return -1 stalls.sort() st_min = 0 st_max = -1 for el in stalls: if st_max < el: st_max = el def possible(mid, stalls): cnt = 1 prev = stalls[0] for i in range(1, len(stalls)): if stalls[i] - prev >= mid: cnt += 1 prev = stalls[i] if cnt >= k: return True return False ans = -1 while st_min <= st_max: mid = (st_min + st_max) // 2 if possible(mid, stalls): ans = mid st_min = mid + 1 else: st_max = mid - 1 return ans
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution(object): def solve(self, n, cows, stalls): def canPlaceAllCows(stalls, minDistance): lastCowIndex, cowsPlaced = 0, 1 for index in range(1, n): if stalls[index] - stalls[lastCowIndex] >= minDistance: lastCowIndex = index cowsPlaced += 1 if cowsPlaced == cows: break return cowsPlaced == cows stalls.sort() left, right = 1, stalls[-1] - stalls[0] result = -1 while left <= right: minDistance = (left + right) // 2 if canPlaceAllCows(stalls, minDistance): result = minDistance left = minDistance + 1 else: right = minDistance - 1 return result
CLASS_DEF VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() def fun(stalls, d, n, k): ans = 1 prev = stalls[0] for i in range(1, n): if stalls[i] - prev >= d: ans += 1 prev = stalls[i] return ans >= k l = 1 h = max(stalls) + 1 curr_ans = -1 while l <= h: mid = (l + h) // 2 temp = fun(stalls, mid, n, k) if temp: curr_ans = mid l = mid + 1 else: h = mid - 1 return curr_ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): min_dis = float("-inf") stalls.sort() low = 1 high = stalls[-1] - stalls[0] while low <= high: mid = low + (high - low) // 2 if possible(stalls, k, mid): min_dis = max(mid, min_dis) low = mid + 1 else: high = mid - 1 return min_dis def possible(stalls, k: int, d: int) -> bool: a = stalls[0] k -= 1 if k == 0: return True for i in range(1, len(stalls)): if stalls[i] - a >= d: a = stalls[i] k -= 1 if k == 0: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() def check(mid): no = 1 initial = stalls[0] for i in stalls: if no >= k: return True elif i - initial >= mid: no += 1 initial = i else: continue else: if no >= k: return True return False left = 0 right = stalls[-1] - stalls[0] while left <= right: mid = (left + right) // 2 if check(mid): left = mid + 1 else: right = mid - 1 return right
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, nums): pass nums.sort() low = 1 high = nums[-1] - nums[0] def canPlace(ch): cnt = 1 pos = nums[0] for i in nums: if i - pos >= ch: pos = i cnt += 1 if cnt == k: return True return False while low <= high: mid = low + (high - low) // 2 if canPlace(mid) == True: low = mid + 1 else: high = mid - 1 return high
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): l = 0 r = max(stalls) - min(stalls) stalls.sort() prev = -99999999999 while l <= r: count = 0 mid = (l + r) // 2 for i in range(n): if abs(stalls[i] - prev) >= mid: count += 1 prev = stalls[i] if count < k: r = mid - 1 else: l = mid + 1 return r
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def isPossible(self, stalls, n, k, mid): cows = 1 last = stalls[0] for i in range(n): if stalls[i] - last >= mid: cows += 1 last = stalls[i] if cows == k: return True return False def solve(self, n, k, stalls): stalls.sort() s = 0 ans = -1 maxi = -1 for i in range(n): maxi = max(maxi, stalls[i]) e = maxi mid = s + (e - s) // 2 while s <= e: if self.isPossible(stalls, n, k, mid): ans = mid s = mid + 1 else: e = mid - 1 mid = s + (e - s) // 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, N, k, stalls): stalls.sort() low = 0 high = max(stalls) - min(stalls) res = -1 while low <= high: mid = (low + high) // 2 if self.allocation_ispossible(mid, stalls, k, N): res = mid low = mid + 1 else: high = mid - 1 return res def allocation_ispossible(self, mid, stalls, k, N): cow_count = 1 last_pos = stalls[0] for i in range(N): if stalls[i] - last_pos >= mid: cow_count += 1 if cow_count == k: return True last_pos = stalls[i] return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def ispossible(self, stalls, mid, k): count = 1 last = stalls[0] for ele in stalls[1:]: if ele - last >= mid: count += 1 if count == k: return True last = ele return False def solve(self, n, k, stalls): stalls.sort() low = 1 high = stalls[-1] - stalls[0] maxdist = 1 while low <= high: mid = (low + high) // 2 if self.ispossible(stalls, mid, k): maxdist = mid low = mid + 1 else: high = mid - 1 return maxdist
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def canAssignCows(self, cows, stalls, mid): j = 1 lastStall = 0 for i in range(1, cows): while j < len(stalls): distanceCovered = stalls[j] - stalls[lastStall] if distanceCovered >= mid: lastStall = j break j += 1 if i >= cows: return True elif j >= len(stalls) and i < cows: return False return True def aggressiveCows(self, cows, stalls): start = 1 stalls.sort() end = stalls[-1] - stalls[0] ans = end while start <= end: mid = start + (end - start) // 2 if self.canAssignCows(cows, stalls, mid): ans = mid start = mid + 1 else: end = mid - 1 return ans def solve(self, n, k, stalls): return self.aggressiveCows(k, stalls)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() def can(dist_bw_cows): bef = 0 cnt = 1 for i in range(1, n): if stalls[i] - stalls[bef] >= dist_bw_cows: bef = i cnt += 1 if cnt >= k: return True return False L, R = 1, 10**9 ans = None while L <= R: M = L + (R - L) // 2 if can(M): ans = M L = M + 1 else: R = M - 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): res = -1 stalls.sort() s = 1 e = stalls[n - 1] - stalls[0] def isValid(arr, n, k, mid): c = 1 p = 0 cors = arr[0] for i in range(1, n): p = arr[i] - cors if p >= mid: c += 1 cors = arr[i] if c == k: return True return False while s <= e: mid = s + (e - s) // 2 if isValid(stalls, n, k, mid): res = mid s = mid + 1 else: e = mid - 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def can_assign_cows(self, stalls, k, dist): prev_cow_pos = stalls[0] n = 1 for i in range(1, len(stalls)): if stalls[i] - prev_cow_pos >= dist: prev_cow_pos = stalls[i] n += 1 if n == k: return True return False def solve(self, n, k, stalls): l = 0 r = 10**9 max_dist = -float("inf") stalls.sort() while l <= r: dist = l + (r - l) // 2 if self.can_assign_cows(stalls, k, dist): max_dist = max(max_dist, dist) l = dist + 1 else: r = dist - 1 return max_dist
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() start = 0 end = 10**9 ans = -1 while start <= end: middle = start + (end - start) // 2 prev = stalls[0] c = 1 for i in stalls: if i - prev >= middle: prev = i c += 1 if c >= k: ans = middle start = middle + 1 else: end = middle - 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() dist = stalls[-1] - stalls[0] lb = 1 ub = dist def isCompatible(dist): lastcow = stalls[0] cows_placed = 1 for i in range(1, n): if cows_placed == k: break if stalls[i] - lastcow >= dist: cows_placed += 1 lastcow = stalls[i] if cows_placed == k: return True return False while lb <= ub: mid = lb + ub >> 1 if isCompatible(mid): lb = mid + 1 else: ub = mid - 1 return ub
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): s = 0 e = max(stalls) ans = 0 mid = s + (e - s) // 2 stalls.sort() def isPossible(stalls, k, mid): cowCount = 1 lastPos = stalls[0] for i in range(len(stalls)): if stalls[i] - lastPos >= mid: cowCount += 1 if cowCount == k: return True lastPos = stalls[i] return False while s <= e: if isPossible(stalls, k, mid): ans = mid s = mid + 1 else: e = mid - 1 mid = s + (e - s) // 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() ans = 0 l = 1 r = max(stalls) - min(stalls) while l <= r: mid = (l + r) // 2 count = 1 last = 0 for i in range(1, len(stalls)): if abs(stalls[i] - stalls[last]) >= mid: count += 1 last = i if count < k: r = mid - 1 elif count >= k: ans = mid l = mid + 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() l = [] def find(arr, left, right, cows): if left <= right: mid = (left + right) // 2 a = arr[0] k = cows - 1 for i in range(1, n): b = arr[i] if b - a >= mid: k = k - 1 a = b if k <= 0: l.append(mid) if left == right: return max(l) return find(arr, mid + 1, right, cows) else: return find(arr, left, mid - 1, cows) else: return max(l) return find(stalls, 0, max(stalls), k)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() low = 0 high = 10**9 mid = (low + high) // 2 ans = -1 while low <= high: if self.ispossible(stalls, n, k, mid): ans = mid low = mid + 1 else: high = mid - 1 mid = (low + high) // 2 return ans def ispossible(self, stalls, n, k, mid): cc = 1 lp = stalls[0] for i in range(len(stalls)): if stalls[i] - lp >= mid: cc += 1 if cc == k: return True lp = stalls[i] return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() low = 1 high = stalls[n - 1] - stalls[0] + 1 def isTrue(mid): left = 0 total = 1 for i in range(1, n): if stalls[i] - stalls[left] >= mid: left = i total += 1 if total == k: return True return False res = 1 while low <= high: mid = (low + high) // 2 if isTrue(mid): res = mid low = mid + 1 else: high = mid - 1 return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): if len(stalls) < k: return -1 def cows(mid): count = 1 latest_put_cow = 0 for i in range(1, len(stalls)): if stalls[i] - stalls[latest_put_cow] >= mid: count += 1 latest_put_cow = i return count stalls.sort() left, right = 1, stalls[-1] - stalls[0] ans = left while left <= right: mid = left + (right - left) // 2 if cows(mid) >= k: ans = mid left = mid + 1 else: right = mid - 1 return ans
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def num_cows(self, arr, min_dist): cows = 1 curr = arr[0] for i in range(1, len(arr)): if arr[i] - curr >= min_dist: cows += 1 curr = arr[i] return cows def solve(self, n, k, stalls): start = 1 end = max(stalls) - min(stalls) ans = 1 stalls.sort() while start <= end: mid = (start + end) // 2 curr_cows = self.num_cows(stalls, mid) if curr_cows < k: end = mid - 1 else: ans = max(ans, mid) start = mid + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def solve(self, n, k, stalls): stalls.sort() left = 1 right = stalls[n - 1] - stalls[0] ans = -1 while left <= right: mid = (left + right) // 2 cows = k - 1 i, j = 0, 1 while j < n: if abs(stalls[j] - stalls[i]) < mid: j += 1 else: cows -= 1 i = j j += 1 if cows > 0: right = mid - 1 else: ans = max(mid, ans) left = mid + 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def count(self, stalls, mid, n): prev = stalls[0] temp = 1 for i in range(n): if stalls[i] - prev >= mid: temp += 1 prev = stalls[i] return temp def solve(self, n, k, stalls): stalls.sort() low = 1 high = stalls[n - 1] - stalls[0] ans = 0 while low <= high: mid = (low + high) // 2 temp = self.count(stalls, mid, n) if temp < k: high = mid - 1 else: low = mid + 1 ans = mid return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible. The first line of input contains two space-separated integers n and k. The second line contains n space-separated integers denoting the position of the stalls. Example 1: Input: n=5 k=3 stalls = [1 2 4 8 9] Output: 3 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[2] and the third cow can be placed at stalls[3]. The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways. Example 2: Input: n=5 k=3 stalls = [10 1 2 7 5] Output: 4 Explanation: The first cow can be placed at stalls[0], the second cow can be placed at stalls[1] and the third cow can be placed at stalls[4]. The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways. Your Task: Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows. Expected Time Complexity: O(n*log(10^9)). Expected Auxiliary Space: O(1). Constraints: 2 <= n <= 10^5 2 <= k <= n 0 <= stalls[i] <= 10^9
class Solution: def isPlace(stalls, cows, dist): coord = stalls[0] cnt = 1 for i in range(1, len(stalls)): if stalls[i] - coord >= dist: cnt += 1 coord = stalls[i] if cnt >= cows: return True else: return False def solve(self, n, k, stalls): stalls.sort() low = 1 high = stalls[-1] - stalls[0] res = [] while low <= high: mid = (low + high) // 2 if Solution.isPlace(stalls, k, mid) == 1: low = mid + 1 res.append(mid) else: high = mid - 1 if res: return max(res) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
def sort(a): res = [] n = len(a) for i in range(n // 2): res.append([min(a[i], a[-i - 1]), max(a[i], a[-i - 1])]) res.sort() return res for tc in range(int(input())): n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) r1 = sort(a) r2 = sort(b) if n % 2: if a[n // 2] != b[n // 2]: print("No") continue if r1 == r2: print("yes") continue print("No")
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR 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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n, a, b = ( int(input()), list(map(int, input().split())), list(map(int, input().split())), ) a1, b1 = [], [] for i in range((n + 1) // 2): a1.append([min(a[i], a[n - i - 1]), max(a[i], a[n - i - 1])]) b1.append([min(b[i], b[n - i - 1]), max(b[i], b[n - i - 1])]) a.sort() b.sort() if n % 2 and a[n // 2] != b[n // 2]: print("No") continue a1.sort() b1.sort() print("Yes" if a1 == b1 else "No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from sys import stdin, stdout R = lambda: stdin.readline().strip() RL = lambda: list(map(int, R().split(" "))) output = lambda x: stdout.write(str(x) + "\n") def sort(a): res = [] for i in range(len(a) // 2): res.append([min(a[i], a[-i - 1]), max(a[i], a[-i - 1])]) res.sort() return res for tc in range(int(R())): n = int(R()) a = RL() b = RL() if n % 2 and a[n // 2] != b[n // 2]: output("No") continue if sort(a) == sort(b): output("yes") continue output("No")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
import sys input = sys.stdin.readline t = int(input()) for i in range(t): l = int(input()) n = list(map(int, input().split())) m = list(map(int, input().split())) if l % 2 == 1 and m[l // 2] != n[l // 2]: print("no") else: n_pair = [] m_pair = [] for j in range(l // 2): n_pair.append([n[j], n[l - 1 - j]]) m_pair.append([m[j], m[l - 1 - j]]) for j in range(len(n_pair)): n_pair[j] = [min(n_pair[j]), max(n_pair[j])] m_pair[j] = [min(m_pair[j]), max(m_pair[j])] n_pair.sort() m_pair.sort() if n_pair == m_pair: print("yes") else: print("no")
IMPORT ASSIGN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) arr1 = list(map(int, input().split(" "))) arr2 = list(map(int, input().split(" "))) res = "yes" if sorted(arr1) != sorted(arr2): print("no") continue if n % 2 == 1 and arr1[n // 2] != arr2[n // 2]: print("no") continue pos_map = {} set1 = [] set2 = [] for idx, i in enumerate(arr1[: n // 2]): set1.append( [max(arr1[idx], arr1[n - 1 - idx]), min(arr1[idx], arr1[n - 1 - idx])] ) set2.append( [max(arr2[idx], arr2[n - 1 - idx]), min(arr2[idx], arr2[n - 1 - idx])] ) if sorted(set1) == sorted(set2): print("yes") else: print("no")
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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) a = tuple(map(int, input().split())) b = tuple(map(int, input().split())) if sorted(zip(a, reversed(a))) == sorted(zip(b, reversed(b))): print("Yes") else: print("No")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = "YES" if sorted(b) != sorted(a): ans = "NO" if n % 2 == 1: if a[n // 2] != b[n // 2]: ans = "NO" x, y = [], [] for i in range(n // 2): j = n - i - 1 x.append(sorted([a[i], a[j]])) y.append(sorted([b[i], b[j]])) if sorted(x) != sorted(y): ans = "NO" print(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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
q = int(input()) for _ in range(q): n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) x1 = 0 x2 = 0 if n % 2 == 1: x1 = l1.pop(n // 2) x2 = l2.pop(n // 2) if x1 != x2: print("No") else: if n % 2 == 1: n -= 1 d1 = [] d2 = [] for i in range(n // 2): d1.append([min(l1[i], l1[n - i - 1]), max(l1[i], l1[n - i - 1])]) d2.append([min(l2[i], l2[n - i - 1]), max(l2[i], l2[n - i - 1])]) d1.sort() d2.sort() if d1 == d2: print("Yes") else: print("No")
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) s1 = [] s2 = [] if n % 2 == 1 and a[n // 2] != b[n // 2]: print("No") continue for i in range(n // 2): s1.append((min(a[i], a[n - i - 1]), max(a[i], a[n - i - 1]))) s2.append((min(b[i], b[n - i - 1]), max(b[i], b[n - i - 1]))) if sorted(s1) == sorted(s2): print("Yes") else: print("No")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
import sys from sys import stdin, stdout t = int(stdin.readline().strip()) for _ in range(t): n = int(stdin.readline().strip()) a = list(map(int, stdin.readline().strip().split(" "))) b = list(map(int, stdin.readline().strip().split(" "))) d = {} for i in range(n // 2): t1 = b[i] t2 = b[n - 1 - i] if t1 > t2: t1, t2 = t2, t1 if t1 in d: d[t1].append(t2) else: d[t1] = [t2] ans = True if n % 2 == 1: if a[n // 2] != b[n // 2]: ans = False for i in range(n // 2): t1 = a[i] t2 = a[n - 1 - i] if t1 > t2: t1, t2 = t2, t1 if t1 in d: if t2 in d[t1]: d[t1].remove(t2) else: ans = False break else: ans = False break if ans: stdout.write("Yes\n") else: stdout.write("No\n")
IMPORT 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 FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) a = [int(X) for X in input().split()] b = [int(x) for x in input().split()] an = 1 if n % 2: if a[n // 2] != b[n // 2]: an = 0 x = [] y = [] for i in range(n // 2): x.append(a[i] + a[n - 1 - i]) y.append(b[i] + b[n - 1 - i]) x.sort() y.sort() for i in range(len(x)): if x[i] != y[i]: an = 0 break print("Yes" if an else "No")
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$Β β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$Β β€” the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ β€” elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ β€” elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for case in range(t): n = int(input()) a = list(map(int, input().split())) aCompressed = [] b = list(map(int, input().split())) bCompressed = [] validity = True if n % 2 != 0: if a[int(n / 2)] != b[int(n / 2)]: validity = False for i in range(int(n / 2)): aCompressed.append(min(a[i], a[-i - 1]) * 1000000001 + max(a[i], a[-i - 1])) bCompressed.append(min(b[i], b[-i - 1]) * 1000000001 + max(b[i], b[-i - 1])) aCompressed.sort() bCompressed.sort() for i in range(int(n / 2)): if aCompressed[i] != bCompressed[i]: validity = False break if validity: print("Yes") else: print("No")
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 LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING