description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n, inf = len(arr), float("inf")
a, s = [[] for _ in range(n)], [(inf, -1)]
for i, x in enumerate(arr):
while s[-1][0] < x:
if s[-1][1] >= i - d:
a[i].append(s[-1][1])
s.pop()
s.append((x, i))
s = [(inf, -1)]
for i, x in reversed(list(enumerate(arr))):
while s[-1][0] < x:
if s[-1][1] <= i + d:
a[i].append(s[-1][1])
s.pop()
s.append((x, i))
dp, v = [1] * n, [0] * n
def dfs(i):
if v[i]:
return
v[i] = 1
for j in a[i]:
dfs(j)
dp[i] = max(dp[i], dp[j] + 1)
for i in range(n):
dfs(i)
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | def max_jump(a, d, memo, i):
if i in memo:
return memo[i]
max_ = 0
for j in range(1, d + 1):
if i + j >= len(a) or a[i + j] >= a[i]:
break
max_ = max(max_, max_jump(a, d, memo, i + j))
for j in range(1, d + 1):
if i - j < 0 or a[i - j] >= a[i]:
break
max_ = max(max_, max_jump(a, d, memo, i - j))
memo[i] = max_ + 1
return max_ + 1
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
memo = {}
for i in range(len(arr)):
max_jump(arr, d, memo, i)
return max(memo.values()) | FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
result = 1
N = len(arr)
cache = {}
def helper(i: int) -> int:
if i in cache:
return cache[i]
result = 1
if (i == 0 or arr[i] <= arr[i - 1]) and (
i == N - 1 or arr[i] <= arr[i + 1]
):
cache[i] = result
return cache[i]
j = i - 1
while j >= 0 and j >= i - d and arr[j] < arr[i]:
cur = helper(j)
result = max(result, cur + 1)
j = j - 1
j = i + 1
while j < N and j <= i + d and arr[j] < arr[i]:
cur = helper(j)
result = max(result, cur + 1)
j = j + 1
cache[i] = result
return cache[i]
for i in range(N):
cur = helper(i)
result = max(cur, result)
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n = len(arr)
jumps = [1] * n
def get_neighs(cur):
neighs = []
directions = [1, -1]
for dire in directions:
for i in range(cur + dire, cur + (d + 1) * dire, dire):
if i < 0 or i >= n:
break
if arr[i] >= arr[cur]:
break
neighs.append(i)
return neighs
@lru_cache(None)
def dp(cur):
for neigh in get_neighs(cur):
jumps[cur] = max(jumps[cur], dp(neigh) + 1)
return jumps[cur]
for i in range(len(arr)):
dp(i)
return max(jumps) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
hp = [(v, i) for i, v in enumerate(arr)]
heapq.heapify(hp)
dp = [0] * len(arr)
res = 0
while hp:
h, i = heapq.heappop(hp)
tmp = 1
for j in range(i - 1, i - 1 - d, -1):
if j < 0 or arr[j] >= arr[i]:
break
tmp = max(tmp, dp[j] + 1)
for j in range(i + 1, i + 1 + d):
if j >= len(arr) or arr[j] >= arr[i]:
break
tmp = max(tmp, dp[j] + 1)
dp[i] = tmp
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
l = []
n = len(arr)
for i in range(n):
l.append([arr[i], i])
l.sort()
l = [i[1] for i in l]
dp = [1] * n
for i in l:
for j in range(i + 1, min(n, i + d + 1)):
if arr[i] > arr[j]:
dp[i] = max(dp[i], dp[j] + 1)
else:
break
for j in range(i - 1, max(-1, i - d - 1), -1):
if arr[i] > arr[j]:
dp[i] = max(dp[i], dp[j] + 1)
else:
break
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
sorted_indexes = sorted(range(len(arr)), key=lambda i: arr[i])
n = len(arr)
dp = [1] * n
def get_neighs(cur):
neighs = []
directions = [1, -1]
for dire in directions:
for i in range(cur + dire, cur + (d + 1) * dire, dire):
if i < 0 or i >= n:
break
if arr[i] >= arr[cur]:
break
neighs.append(i)
return neighs
for cur in sorted_indexes:
for neigh in get_neighs(cur):
dp[cur] = max(dp[cur], dp[neigh] + 1)
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def helper(self, idx):
if self.dp[idx] != float("inf"):
return self.dp[idx]
max_step = 1
for c in range(idx - 1, max(0, idx - self.d) - 1, -1):
if self.arr[idx] <= self.arr[c]:
break
max_step = max(self.helper(c) + 1, max_step)
for c in range(idx + 1, min(self.n, idx + self.d + 1)):
if self.arr[idx] <= self.arr[c]:
break
max_step = max(self.helper(c) + 1, max_step)
self.dp[idx] = max_step
return max_step
def maxJumps(self, arr: List[int], d: int) -> int:
self.n = len(arr)
self.d = d
self.arr = arr
self.dp = [float("inf")] * self.n
self.trans = defaultdict(list)
max_ = 0
for i in range(self.n):
max_ = max(self.helper(i), max_)
return max_ | CLASS_DEF FUNC_DEF IF VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n = len(arr)
if n == 1 or d == 0:
return 1
e = [[] for i in range(n)]
for i in range(0, n):
for j in range(i - 1, max(0, i - d) - 1, -1):
if arr[j] < arr[i]:
e[i].append(j)
else:
break
for j in range(i + 1, min(n - 1, i + d) + 1):
if arr[j] < arr[i]:
e[i].append(j)
else:
break
ans = 1
queue = []
cnt = [(1) for i in range(n)]
for i in range(0, n):
queue.append([arr[i], i])
queue.sort()
for i in range(0, n):
u = queue[i][1]
for v in e[u]:
cnt[u] = max(cnt[u], cnt[v] + 1)
ans = max(ans, cnt[u])
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
def dp(arr, d, dic, i, n):
if dic[i] != None:
return dic[i]
maximum = 1
upreached = False
downreached = False
for p in range(1, d + 1):
if i + p < n and not upreached:
if arr[i + p] >= arr[i]:
upreached = True
else:
maximum = max(maximum, 1 + dp(arr, d, dic, i + p, n))
if i - p >= 0 and not downreached:
if arr[i - p] >= arr[i]:
downreached = True
else:
maximum = max(maximum, 1 + dp(arr, d, dic, i - p, n))
if upreached and downreached:
break
dic[i] = maximum
return maximum
n = len(arr)
dic = [None] * n
maximum = 0
for i in range(n):
maximum = max(maximum, dp(arr, d, dic, i, n))
return maximum | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
cache = {}
def f(i):
if i in cache:
return cache[i]
left = True
right = True
ret = 1
for step in range(1, d + 1):
m = i - step
if 0 <= m < len(arr) and arr[m] < arr[i] and left:
ret = max(ret, f(m) + 1)
else:
left = False
m = i + step
if 0 <= m < len(arr) and arr[m] < arr[i] and right:
ret = max(ret, f(m) + 1)
else:
right = False
if not left and not right:
break
cache[i] = ret
return ret
mm = 0
for i in range(len(arr)):
fi = f(i) if i not in cache else cache[i]
mm = max(f(i), mm)
return mm | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n = len(arr)
@lru_cache(None)
def dfs(i):
left, right = i, i
max_range = 1
while left - 1 >= 0 and arr[left - 1] < arr[i] and left - 1 >= i - d:
left -= 1
while right + 1 < n and arr[right + 1] < arr[i] and right + 1 <= i + d:
right += 1
for nxt in range(left, right + 1):
if nxt != i:
max_range = max(max_range, 1 + dfs(nxt))
return max_range
return max(dfs(i) for i in range(n)) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
dp = [1] * len(arr)
a = []
for idx, val in enumerate(arr):
a.append((val, idx))
a.sort()
print(a)
for _, idx in a:
for i in range(1, d + 1):
if idx + i < len(arr) and arr[idx + i] < arr[idx]:
dp[idx] = max(dp[idx], dp[idx + i] + 1)
else:
break
for i in range(1, d + 1):
if 0 <= idx - i and arr[idx - i] < arr[idx]:
dp[idx] = max(dp[idx], dp[idx - i] + 1)
else:
break
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
arr.append(10**6)
n = len(arr)
dp = [1] * n
stack = []
result = 0
for i, a in enumerate(arr):
while stack and arr[stack[-1]] < a:
for j in range(len(stack) - 2, -1, -1):
if arr[stack[j]] != arr[stack[j + 1]]:
break
else:
j = -1
for k in range(len(stack) - j - 1):
l = stack.pop()
if i - l <= d:
dp[i] = max(dp[i], 1 + dp[l])
if j >= 0 and l - stack[j] <= d:
dp[stack[j]] = max(dp[stack[j]], 1 + dp[l])
stack.append(i)
dp.pop()
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
if not arr:
return 0
result = [0] * len(arr)
def _helper(i: int) -> int:
if result[i]:
return result[i]
result[i] = 1
j = 1
while j <= d and i + j < len(arr) and arr[i + j] < arr[i]:
result[i] = max(result[i], _helper(i + j) + 1)
j += 1
j = 1
while j <= d and i - j >= 0 and arr[i - j] < arr[i]:
result[i] = max(result[i], _helper(i - j) + 1)
j += 1
return result[i]
for i in range(len(arr)):
_helper(i)
return max(result) | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
m = len(arr)
memo = [(-1) for _ in range(m)]
def dfs(i):
if memo[i] != -1:
return memo[i]
memo[i] = 1
left = i - 1
while left >= 0 and i - left <= d and arr[left] < arr[i]:
memo[i] = max(dfs(left) + 1, memo[i])
left -= 1
right = i + 1
while right < m and right - i <= d and arr[right] < arr[i]:
memo[i] = max(dfs(right) + 1, memo[i])
right += 1
return memo[i]
for i in range(m):
dfs(i)
print(memo)
return max(memo) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
sorted_indexes = sorted(range(len(arr)), key=lambda i: arr[i])
n = len(arr)
dp = [1] * n
def get_neighs(cur):
neighs = []
for i in range(cur + 1, min(cur + d + 1, n)):
if arr[i] < arr[cur]:
neighs.append(i)
else:
break
for i in range(cur - 1, max(cur - d - 1, -1), -1):
if arr[i] < arr[cur]:
neighs.append(i)
else:
break
return neighs
for cur in sorted_indexes:
neighs = get_neighs(cur)
for neigh in neighs:
dp[cur] = max(dp[cur], dp[neigh] + 1)
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n = len(arr)
res = [0] * n
def dp(i):
if res[i]:
return res[i]
res[i] = 1
for di in [-1, 1]:
for j in range(i + di, i + d * di + di, di):
if not (0 <= j < n and arr[j] < arr[i]):
break
res[i] = max(res[i], dp(j) + 1)
return res[i]
return max(map(dp, range(n))) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
res = 0
n = len(arr)
self.m = [0] * n
for i in range(n):
ans = self.dfs(arr, d, i)
res = max(ans, res)
return res
def dfs(self, arr, d, i):
res = 1
if self.m[i] != 0:
return self.m[i]
for k in range(1, d + 1):
if i + k >= len(arr):
break
if arr[i + k] >= arr[i]:
break
ans = self.dfs(arr, d, i + k) + 1
if ans > res:
res = ans
for k in range(1, d + 1):
if i - k < 0:
break
if arr[i - k] >= arr[i]:
break
ans = self.dfs(arr, d, i - k) + 1
if ans > res:
res = ans
self.m[i] = res
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n = len(arr)
memo = [0] * n
def dfs(start):
if start < 0 or start >= n:
return 0
if memo[start] > 0:
return memo[start]
visited[start] = 1
ret = 1
for i in range(start - 1, max(0, start - d) - 1, -1):
if arr[i] >= arr[start]:
break
if visited[i]:
continue
ret = max(ret, dfs(i) + 1)
for j in range(start + 1, min(start + d, n - 1) + 1):
if arr[j] >= arr[start]:
break
if visited[j]:
continue
ret = max(ret, dfs(j) + 1)
visited[start] = 0
memo[start] = ret
return ret
ret = 1
visited = [0] * n
for i in range(n):
ret = max(dfs(i), ret)
return ret | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, A: List[int], d: int) -> int:
n = len(A)
graph = {}
inorder = [0] * n
for i in range(n):
graph[i] = []
is_greater = False
cur_max = -1
for ind in range(i - 1, max(-1, i - d - 1), -1):
if A[ind] >= A[i]:
if A[ind] >= cur_max and A[ind] > A[i]:
cur_max = A[ind]
graph[ind].append(i)
inorder[i] += 1
is_greater = True
else:
if is_greater:
continue
graph[i].append(ind)
inorder[ind] += 1
stack = []
dp = [1] * n
for i in range(n):
if inorder[i] == 0:
stack.append(i)
while stack:
top = stack.pop()
for value in graph[top]:
inorder[value] -= 1
dp[value] = max(dp[value], dp[top] + 1)
if inorder[value] == 0:
stack.append(value)
return max(dp) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
v = []
n = len(arr)
for i in range(n):
v.append((arr[i], i))
v = sorted(v)
f = [-1] * n
ans = -1
for i in range(n):
idx = v[i][1]
f[idx] = 1
for j in range(1, d + 1):
if idx - j >= 0 and arr[idx - j] < arr[idx]:
f[idx] = max(f[idx], f[idx - j] + 1)
else:
break
for j in range(1, d + 1):
if idx + j < n and arr[idx + j] < arr[idx]:
f[idx] = max(f[idx], f[idx + j] + 1)
else:
break
ans = max(ans, f[idx])
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
dp = [0] * len(arr)
def get_dp(i):
if dp[i] == 0:
dp[i] = 1
j = i - 1
while j >= 0 and arr[i] > arr[j] and i - j <= d:
dp[i] = max(dp[i], 1 + get_dp(j))
j -= 1
j = i + 1
while j < len(arr) and arr[i] > arr[j] and j - i <= d:
dp[i] = max(dp[i], 1 + get_dp(j))
j += 1
return dp[i]
max_count = 0
for i in range(len(arr)):
max_count = max(max_count, get_dp(i))
return max_count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Example 4:
Input: arr = [7,1,7,1,7,1], d = 2
Output: 2
Example 5:
Input: arr = [66], d = 1
Output: 1
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length | class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
memo = [0] * len(arr)
def jump(i):
if memo[i]:
return memo[i]
ans = 1
for di in [-1, 1]:
for x in range(1, d + 1):
j = di * x + i
if 0 <= j < len(arr) and arr[j] < arr[i]:
ans = max(ans, jump(j) + 1)
else:
break
memo[i] = ans
return memo[i]
ans = 0
for i in range(len(arr)):
ans = max(ans, jump(i))
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d.
Johnny's truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are m gas stations located at various points along the way to the district center. The i-th station is located at the point xi on the number line and sells an unlimited amount of fuel at a price of pi dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery.
Input
The first line of input contains three space separated integers d, n, and m (1 ≤ n ≤ d ≤ 109, 1 ≤ m ≤ 200 000) — the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively.
Each of the next m lines contains two integers xi, pi (1 ≤ xi ≤ d - 1, 1 ≤ pi ≤ 106) — the position and cost of gas at the i-th gas station. It is guaranteed that the positions of the gas stations are distinct.
Output
Print a single integer — the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.
Examples
Input
10 4 4
3 5
5 8
6 3
8 4
Output
22
Input
16 5 2
8 2
5 1
Output
-1
Note
In the first sample, Johnny's truck holds 4 liters. He can drive 3 units to the first gas station, buy 2 liters of gas there (bringing the tank to 3 liters total), drive 3 more units to the third gas station, buy 4 liters there to fill up his tank, and then drive straight to the district center. His total cost is 2·5 + 4·3 = 22 dollars.
In the second sample, there is no way for Johnny to make it to the district center, as his tank cannot hold enough gas to take him from the latest gas station to the district center. | destination, max_gas_tank_volume, gas_prices_number = map(int, input().split())
start_point = 0
gas_prices = {start_point: 0}
for i in range(gas_prices_number):
coordinate, price = map(int, input().split())
gas_prices[coordinate] = price
points = sorted(gas_prices.keys(), reverse=True)
current_point = start_point
count = 0
gas_tank_volume = max_gas_tank_volume
reachable_points = []
while current_point != destination:
farthest_reachable_point = current_point + max_gas_tank_volume
while points and points[-1] <= farthest_reachable_point:
reachable_points.append(points.pop())
if reachable_points:
cheaper_reachable_points = sorted(
filter(
lambda point: gas_prices[point] < gas_prices[current_point],
reachable_points,
)
)
next_point = (
cheaper_reachable_points[0]
if cheaper_reachable_points
else min(reachable_points, key=lambda point: gas_prices[point])
)
if farthest_reachable_point >= destination and (
current_point == start_point
or gas_prices[next_point] >= gas_prices[current_point]
):
next_point = destination
else:
reachable_points = [
point for point in reachable_points if point > next_point
]
elif farthest_reachable_point >= destination:
next_point = destination
else:
count = -1
break
distantion = next_point - current_point
if (
next_point != destination
and gas_prices[current_point] <= gas_prices[next_point]
):
required_gas_volume = max_gas_tank_volume
else:
required_gas_volume = distantion
if required_gas_volume > gas_tank_volume:
count += (required_gas_volume - gas_tank_volume) * gas_prices[current_point]
gas_tank_volume = required_gas_volume
current_point = next_point
gas_tank_volume -= distantion
print(count) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for _ in range(t):
n = int(input())
s = list(map(int, input().split()))
p = {}
for i in s:
p[i] = p.get(i, 0) + 1
if n % 2 == 0:
if max(p.values()) <= n // 2:
print(0)
else:
print(2 * max(p.values()) - n)
elif max(p.values()) <= n // 2:
print(1)
else:
print(2 * max(p.values()) - n) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def frequency(l):
d = {}
for i in lis:
if i not in d:
d[i] = 1
else:
d[i] += 1
return d
for _ in range(int(input())):
n = int(input())
lis = list(map(int, input().split()))
d = {}
for i in lis:
if i not in d:
d[i] = 1
else:
d[i] += 1
fin = []
for i in d:
fin.append(d[i])
fin.sort()
l = len(fin)
if l == 1:
print(n)
else:
a = 2 * fin[-1] - sum(fin)
if a >= 0:
print(a)
else:
print(n % 2) | FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER 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 ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def Trans(array, n):
m = 0
answer = 0
how_much = {}
for i in array:
if i not in how_much:
how_much[i] = 0
how_much[i] += 1
for i in how_much:
if how_much[i] > m:
m = how_much[i]
answer = i
if n - 2 * (n - m) < 0:
if n % 2 == 1:
return 1
return 0
return n - 2 * (n - m)
t = int(input())
for i in range(t):
n = int(input())
lista = list(map(int, input().split()))
print(Trans(lista, n)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER RETURN BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
list1 = list(map(int, input().split()))
dict1 = dict()
for i in list1:
if i not in dict1.keys():
dict1[i] = 1
else:
dict1[i] += 1
list2 = list()
for i in dict1.keys():
list2.append(dict1[i])
list2.sort(reverse=True)
total = 0
if len(list2) == 1:
print(list2[0])
continue
for i in list2[1:]:
total += i
if total - list2[0] >= 0:
print((total - list2[0]) % 2)
else:
print(list2[0] - total) | 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 FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
while t > 0:
n = int(input())
a = [int(x) for x in input().split()]
d = {}
for i in a:
if d.get(i) != None:
d[i] += 1
else:
d[i] = 1
m = max(list(d.values()))
if m > (n + 1) // 2:
ans = m - (n - m)
else:
ans = n % 2
print(ans)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NONE VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input().strip())
for _ in range(t):
n = int(input().strip())
d = {}
for i in input().split():
d[i] = d.get(i, 0) + 1
print(max(2 * max(d.values()) - n, n % 2)) | 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 DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
l1 = []
for i in range(t):
n = int(input())
x = input()
l1.append(list(map(int, x.split())))
for i in l1:
dic = {}
for j in i:
if j not in dic:
dic[j] = 1
else:
dic[j] += 1
d = dic.values()
e = max(d)
if e <= len(i) / 2:
if len(i) % 2 == 0:
print(0)
else:
print(1)
elif e > len(i) / 2:
u = e - (len(i) - e)
print(u) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def solve(arr):
freqs = dict()
for num in arr:
if num not in freqs:
freqs[num] = 1
else:
freqs[num] += 1
max_freq = max(freqs.values())
return max(len(arr) % 2, max_freq - (len(arr) - max_freq))
def main():
t = int(input())
for i in range(t):
n = int(input())
arr = [int(s) for s in input().split()]
print(solve(arr))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | import sys
from _collections import deque
mod = 1000000007
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = dict()
for i in a:
d[i] = d.get(i, 0) + 1
t = 0
for i in d:
if d[i] > n // 2:
t = d[i]
break
if t == 0:
if n % 2:
print(1)
else:
print(0)
else:
print(2 * t - n) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | kk = int(input())
for i in range(kk):
l = int(input())
lst = list(map(int, input().split()))
lst.sort()
ans = len(lst)
if len(lst) % 2 == 0:
ans1 = lst[: len(lst) // 2]
ans2 = lst[len(lst) // 2 :]
for i in range(len(lst) // 2):
if ans1[i] != ans2[i]:
ans -= 2
else:
ans1 = lst[: len(lst) // 2]
ans2 = lst[len(lst) // 2 + 1 :]
for i in range(len(lst) // 2):
if ans1[i] != ans2[i]:
ans -= 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | try:
t = int(input())
for _ in range(t):
n = int(input())
l = list(input().split())
d = {}
for i in l:
if i in d:
d[i] += 1
else:
d[i] = 1
mx = 0
for i in d:
if d[i] > mx:
mx = d[i]
print(max(n % 2, 2 * mx - n))
except:
pass | 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 FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for i in [*open(0)][2::2]:
a = i.split()
a.sort()
tem = a[0]
ma = 0
b = 0
for j in a:
if j == tem:
b += 1
else:
if ma < b:
ma = b
b = 1
tem = j
if ma < b:
ma = b
if ma > len(a) / 2:
print(2 * ma - len(a))
elif len(a) % 2:
print(1)
else:
print(0) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | T = int(input())
for t in range(T):
n = int(input())
A = [int(i) for i in input().split()]
freq = dict()
for i in A:
if i not in freq:
freq[i] = 1
else:
freq[i] += 1
ans = 0
exists = 0
key = -1
for i in freq:
if freq[i] > n // 2:
exists = 1
key = i
others = 0
if not exists:
if n % 2 == 0:
print(0)
else:
print(1)
else:
for i in freq:
if i != key:
others += freq[i]
print(freq[key] - others) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n, d = int(input()), {}
for i in input().split():
d[i] = d[i] + 1 if i in d.keys() else 1
m = max(d.values())
print(2 * m - n if m > n // 2 else n & 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n, l = int(input()), list(map(int, input().split()))
m = {}
k = 0
for i in l:
if i in m:
m[i] += 1
else:
m[i] = 1
k = max(m[i], k)
print(max(2 * k - n, n % 2)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def solve():
n = int(input())
a = list(map(int, input().split()))
freq = {}
for x in a:
freq[x] = freq.get(x, 0) + 1
mx = max(freq.values())
if mx > n // 2:
print(mx - (n - mx))
elif n % 2 == 1:
print(1)
else:
print(0)
t = int(input())
for _ in range(t):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a.count(a[n // 2]) <= n // 2:
if n % 2 == 1:
print(1)
else:
print(0)
else:
print(2 * a.count(a[n // 2]) - n) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def helper(a, n):
d = {}
for i in range(n):
d[a[i]] = d.get(a[i], 0) + 1
m = max(d.values())
if m <= (n + 1) // 2:
return n % 2
else:
return m - (n - m)
t = int(input())
for i in range(t):
n = int(input())
a = list(input().split(" "))
print(helper(a, n)) | FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP 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 FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | k = int(input())
def chop(x):
if 2 * max(x) > sum(x):
return 2 * max(x) - sum(x)
else:
return sum(x) % 2
for i in range(k):
g = int(input())
ar = list(map(int, input().split()))
d = {}
for x in ar:
d[x] = d.get(x, 0) + 1
count_list = list(d.values())
print(chop(count_list)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR 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 ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | test_n = int(input())
class node:
def __init__(self, v):
self.v = v
self.amount = 0
for _ in range(test_n):
n = int(input())
a = map(int, input().split(" "))
m = {}
l = []
for i in a:
if m.get(i):
m[i] += 1
else:
m[i] = 1
max_val = max(m.values())
if max_val >= n - max_val:
print(2 * max_val - n)
continue
if n % 2 == 1:
print(1)
else:
print(0) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | import sys
inputlines = sys.stdin.readlines()
number_of_testcases = int(inputlines[0].strip())
def make_freq_dict(input_list):
freq_dict = {}
for i in input_list:
if i not in freq_dict:
freq_dict[i] = 0
freq_dict[i] += 1
return freq_dict
for i in range(number_of_testcases):
n = int(inputlines[2 * i + 1].strip())
integers = list(map(int, inputlines[2 * i + 2].strip().split(" ")))
freq_dict = make_freq_dict(integers)
values_list = list(freq_dict.values())
values_list.sort(reverse=True)
length_values_list = len(values_list)
if length_values_list == 1:
print(n)
continue
sum_other_than_maximum = 0
for j in range(1, length_values_list):
sum_other_than_maximum += values_list[j]
if values_list[0] <= sum_other_than_maximum:
if n % 2 == 0:
print(0)
else:
print(1)
else:
print(values_list[0] - sum_other_than_maximum) | IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
s = list(set(a))
if len(s) == 1:
print(n)
continue
elif len(s) == n:
print(n % 2)
continue
else:
d = {}
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
maxx = max(d.values())
if n < 2 * maxx:
print(2 * maxx - n)
elif n % 2 == 1:
print(1)
else:
print(0) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def transform(list_num):
mem = {}
for el in list_num:
if el in mem.keys():
mem[el] += 1
else:
mem[el] = 1
min_val = len(list_num) % 2
max_val = max(mem.values())
diff = max_val - (len(list_num) - max_val)
if diff > 0:
return diff
else:
return min_val
num = int(input())
for i in range(num):
s = input()
list_num = [int(el) for el in input().split()]
print(transform(list_num)) | FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for p in range(t):
n = int(input())
a = dict()
my_input = input().split()
for x in my_input:
a[x] = 0
for i in range(len(my_input)):
a[my_input[i]] += 1
b = set(my_input)
mx = max(a.values())
for x in a:
if a[x] == mx:
c = x
break
for x in b:
if x == c:
continue
mx -= a[x]
if mx < 0:
mx = 0
if n % 2 and mx == 0:
mx = 1
print(mx) | 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 ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for u in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
dic = {}
for num in a:
if num in dic:
dic[num] += 1
else:
dic[num] = 1
k = max(dic.values())
if k <= (n + 1) // 2:
print(1 if n & 1 else 0)
else:
print(2 * k - n) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
freq = {}
for i in a:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
if max(freq.values()) <= (n + 1) // 2:
if n % 2 == 0:
print(0)
else:
print(1)
else:
print(2 * max(freq.values()) - n) | IMPORT ASSIGN VAR 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 ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | Z = input
for _ in [0] * int(Z()):
n = int(Z())
c = {}
for i in Z().split():
c[i] = c.get(i, 0) + 1
m = max(c.values())
print(max(m + m - n, n % 2)) | ASSIGN VAR VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for t in range(int(input())):
n = int(input())
a = map(int, input().split())
c = {}
for x in a:
if x in c:
c[x] += 1
else:
c[x] = 1
m = max(c.values())
print(2 * m - n if m > n // 2 else n % 2) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
mp = {}
for x in a:
if x in mp:
mp[x] += 1
else:
mp[x] = 1
b = list(mp.values())
b.sort(reverse=True)
res = n % 2
if b[0] * 2 > n:
d = n - b[0]
res = n - 2 * d
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for case in range(t):
n = int(input())
d = dict()
max_count = 1
for i in map(int, input().split()):
if i in d:
d[i] += 1
if max_count < d[i]:
max_count = d[i]
else:
d[i] = 1
if n - max_count >= max_count:
print(n % 2)
else:
print(max_count - (n - max_count)) | 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input().strip())
for _ in range(t):
n = int(input().strip())
d = {}
for i in input().split():
d[i] = d.get(i, 0) + 1
M = max(d.values())
S = sum(d.values())
if 2 * M > S:
print(2 * M - S)
else:
print(n % 2) | 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 DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
h = n // 2 + n % 2
cnt = n
for i in range(n // 2):
if a[i] != a[i + h]:
cnt -= 2
print(cnt) | 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | T = int(input())
for _ in range(T):
n = int(input())
arr = list(map(int, input().split()))
d = {}
for i in arr:
d[i] = d.get(i, 0) + 1
d = sorted(d.items(), key=lambda x: x[1])
k = d[-1][1]
if n % 2 == 1:
ans = max(1, k - (n - k))
else:
ans = max(0, k - (n - k))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
l3 = sorted(list(map(int, input().split())))
a = 1
l = []
for i in range(n - 1):
if l3[i] == l3[i + 1]:
a += 1
else:
l.append(a)
a = 1
else:
l.append(a)
print(max(max(l) * 2 - n, n % 2)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
s = input().split()
d = dict()
for i in s:
d[i] = d.get(i, 0) + 1
mx = 0
for i in d:
if d[i] > mx:
mx = d[i]
if mx >= n - mx:
print(n - 2 * (n - mx))
elif n % 2 != 0:
print(1)
else:
print(0) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | n = int(input())
X = []
for i in range(n):
m = int(input())
X.append(list(map(int, input().split(" "))))
for x in X:
counter = {}
counter[0] = 0
has_max_count = 0
result = 0
for i, y in enumerate(x):
if y in counter:
counter[y] += 1
else:
counter[y] = 1
if counter[y] > counter[has_max_count]:
has_max_count = y
result = 0
if y == has_max_count and counter[y] > (i + 1) // 2:
result += 1
elif result > 0:
result -= 1
if result == 0 and i % 2 == 0:
result = 1
print(result) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
ans = []
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
mx = 0
l = 1
for i in range(n - 1):
if a[i] == a[i + 1]:
l += 1
else:
mx = max(l, mx)
l = 1
mx = max(l, mx)
if mx * 2 <= n:
ans.append(n % 2)
else:
ans.append(mx * 2 - n)
for i in ans:
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def main():
t = int(input())
for _ in range(t):
n = int(input())
a = sorted(list(map(int, input().split())))
if n % 2 == 0:
cnt = n
for i in range(n // 2):
if a[i] != a[i + n // 2]:
cnt -= 2
print(cnt)
else:
cnt = n
for i in range(n // 2):
if a[i] != a[i + n // 2 + 1]:
cnt -= 2
print(cnt)
main() | FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.append(-5)
maxi = 0
count = 1
for i in range(n):
if a[i] == a[i + 1]:
count += 1
else:
maxi = max(maxi, count)
count = 1
if n % 2 == 0:
print(max(0, maxi - (n - maxi)))
else:
print(max(1, maxi - (n - maxi))) | 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def main():
t = int(input())
for i in range(t):
n = int(input())
bloki = []
a = sorted(list(map(int, input().split())))
por = a[0]
lic = 0
for liczba in a:
if liczba == por:
lic += 1
else:
bloki.append(lic)
lic = 1
por = liczba
bloki.append(lic)
maks = max(bloki)
resz = n - maks
if n % 2 != 0:
if maks > 0:
print(1 + max(0, maks - 1 - resz))
else:
print(1 + maks(0, maks + 1 - resz))
else:
print(max(0, maks - resz))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for k in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
if i not in d:
d[i] = 1
else:
d[i] += 1
max_count = max(d.values())
if max_count > n // 2:
ans = max_count - (n - max_count)
print(ans)
elif n % 2 == 0:
print(0)
else:
print(1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
s = {}
for i in a:
if i not in s:
s[i] = 1
else:
s[i] += 1
m = 0
s2 = 0
for i in s.keys():
m = max(s[i], m)
s2 += s[i]
print(max(m - s2 + m, n % 2)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for _ in range(t):
n = int(input())
array = [int(i) for i in input().split()]
l = len(array)
if len(set(array)) < 2:
print(l)
else:
d = dict()
for a in array:
d.setdefault(a, 0)
d[a] += 1
m = max(d.values())
if m > l // 2:
print(2 * m - l)
else:
print(l % 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
d = {}
K = []
k = 0
for j in arr:
if j in d.keys():
d[j] += 1
else:
d.update({j: 1})
k += 1
for j in d:
K.append(d[j])
if k == 1:
print(K[0])
continue
if max(K) > sum(K) - max(K):
print(2 * max(K) - sum(K))
else:
print(sum(K) % 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
i = 0
if n % 2 == 0:
j = int(n / 2)
else:
j = int(n / 2) + 1
count = 0
while j < n:
if a[i] != a[j]:
count += 2
j += 1
i += 1
else:
j += 1
print(n - count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if n == 0 or n == 1:
print(n)
continue
d = dict()
for i in a:
d[i] = d.get(i, 0) + 1
m = max(d.values())
if m <= n // 2:
print(n % 2)
else:
print(2 * m - n) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for t in range(int(input())):
n = int(input())
a = sorted(list(map(int, input().split())))
def solve():
cnt = 0
i = 0
h = n // 2 + n % 2
while i < n // 2:
if a[i] != a[h + i]:
cnt += 2
i += 1
return n - cnt
print(solve()) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
my_key = {}
for c in a:
if c in my_key.keys():
my_key[c] += 1
else:
my_key[c] = 0
z = max(my_key.values()) + 1
if z > n - z:
print(2 * z - n)
else:
print(n % 2) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
temp = 1
if n == 1:
print(1)
continue
count = []
for i in range(1, n):
if a[i] == a[i - 1]:
temp += 1
if i == n - 1:
count.append(temp)
else:
count.append(temp)
temp = 1
if i == n - 1:
count.append(temp)
count.sort()
if len(count) == 1:
print(count[0])
continue
if count[-1] <= sum(count[: len(count) - 1]):
print(n % 2)
else:
print(count[-1] - sum(count[: len(count) - 1])) | 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
lis = list(map(int, input().split()))
dic = {}
for i in lis:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
m = max(list(dic.values()))
if n % 2 == 1 and m <= n // 2 + 1:
ans = 1
elif n % 2 == 0 and m <= n // 2:
ans = 0
else:
ans = m - (n - m)
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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
i, c = 0, 0
for j in range((n + 1) // 2, n):
if a[i] != a[j]:
c += 1
i += 1
print(n - c * 2) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def ret(a, n):
res = 0
prind = 0
val = a[0]
for i in range(1, n):
vali = a[i]
if vali != val:
res = max(res, i - prind)
prind = i
val = vali
res = max(res, n - prind)
return res
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
r = ret(a, n)
if r > n / 2:
print(2 * r - n)
elif n % 2 == 1:
print(1)
else:
print(0) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | import sys
input = sys.stdin.readline
class Problem:
def __init__(self):
pass
def solve(self):
ans = 0
n = int(input())
A = list(map(int, input().split()))
count = {}
maxcount = 0
for a in A:
if a not in count:
count[a] = 0
count[a] += 1
maxcount = max(maxcount, count[a])
ans = max(n % 2, 2 * maxcount - n)
print(ans)
def main():
p = Problem()
t = int(input())
while t:
p.solve()
t -= 1
main() | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l.sort()
i = n // 2 - 1
j = n - 1
ans = 0
while i >= 0 and j >= n // 2:
if l[i] != l[j]:
ans += 2
i -= 1
j -= 1
print(n - 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
d = dict()
for x in a:
if not x in d:
d[x] = 0
d[x] += 1
print(max(0, 2 * max(d.values()) - n, n % 2)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def solve():
n = int(input())
t = list(map(int, input().split()))
numbers = {}
for e in t:
if e in numbers:
numbers[e] += 1
else:
numbers[e] = 1
mx = max(numbers.values())
if mx >= (n + 1) // 2:
print(mx - (n - mx))
else:
print(n % 2)
tests = int(input())
for _ in range(tests):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l.sort()
i = 0
ans = 0
w = n // 2
while i < n // 2 and w < n:
if l[i] != l[w]:
i += 1
w += 1
ans += 1
elif l[i] == l[w]:
w += 1
print(n - 2 * 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | import sys
N = int(200000.0 + 5)
sys.setrecursionlimit(N)
def charming():
n = int(input())
a = list(map(int, input().split()))
d = dict()
mx = 0
for i in a:
if i not in d:
d[i] = 1
else:
d[i] += 1
mx = max(mx, d[i])
if mx <= n - mx:
print(n & 1)
return
else:
print(mx - n + mx)
for t in range(int(input())):
charming() | IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | from sys import stdin, stdout
Pi = lambda x: stdout.write(str(x) + "\n")
Ps = lambda x: stdout.write(str(x))
S = lambda x: (x + (x + 1) // 2) * (x // 2)
I = lambda x: 1 + 2 * x
R = lambda: stdin.readline()
Ri = lambda x: map(int, x.split())
Rs = lambda x: map(str, x.split())
Rf = lambda x: map(float, x.split())
char = lambda x, y: ord(x) - ord(y)
gcd = lambda a, b: a if b == 0 else gcd(b, a % b)
lcm = lambda a, b: a * b // gcd(a, b)
distance = lambda a, b, c, d: (a - c) ** 2 + (b - d) ** 2
Matriz = lambda n, m: [[(0) for i in range(m)] for i in range(n)]
MaxN = int(100000.0) + 10
mod = int(1000000000.0) + 7
fx, fy = [0, 0, -1, 1], [1, -1, 0, 0]
def find(u, p):
if u != p[u]:
p[u] = find(p[u], p)
return p[u]
def isPrime(n):
if n == 2:
return 1
if n < 2 or n % 2 == 0:
return 0
for i in range(3, int(n**0.5) + 1):
if n % i == 0:
return 0
return 1
def bs(A, l, r, x):
while r - l > 1:
m = (l + r) // 2
if A[m] >= x:
r = m
else:
l = m
return r
def f(a, b):
n, m = len(a), len(b)
ans = n + m
for i in range(1, n + 1):
for j in range(i, n + 1):
if a[j - i : j] in b:
ans = min(ans, n + m - i * 2)
return ans
def main():
t = int(R())
for _ in range(t):
n = int(R())
A = list(Ri(R()))
dic = {}
mx = 0
for x in A:
if x in dic:
dic[x] += 1
else:
dic[x] = 1
mx = max(mx, dic[x])
ans = 0
if mx > n // 2:
ans = n - (n - mx) * 2
else:
ans = n % 2
Pi(ans)
main() | ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF 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 FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
while t > 0:
t = t - 1
dict = {}
n = int(input())
L = input().split()
Max = 0
for x in L:
if x not in dict.keys():
dict[x] = 1
else:
dict[x] = dict[x] + 1
if dict[x] > Max:
Max = dict[x]
if 2 * Max > n:
print(2 * Max - n)
elif n % 2 == 0:
print(0)
else:
print(1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | t = int(input())
for _ in range(t):
n = int(input())
arr = input().split(" ")
arr = [int(x) for x in arr]
cnt = {}
for x in arr:
cnt[x] = 0
for x in arr:
cnt[x] += 1
mx = 0
for a in cnt.values():
if a > mx:
mx = a
if mx + mx >= n:
rem = n - mx
ans = n - (rem + rem)
else:
ans = n % 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None | def solve():
n = int(input())
a = sorted(list(map(int, input().split())))
c = 0
i = 0
h = n // 2 + n % 2
while i < n // 2:
if a[i] != a[h + i]:
c += 2
i += 1
return n - c
t = int(input())
while t > 0:
print(solve())
t -= 1 | FUNC_DEF 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
input = sys.stdin.readline
q, x = map(int, input().split())
mex = 0
a = [(0) for i in range(x)]
for i in range(q):
y = int(input())
a[y % x] += 1
while a[mex % x] > 0:
a[mex % x] -= 1
mex += 1
print(mex) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split())
a = [0] * x
mex = 0
out = []
for i in range(q):
a[int(input()) % x] += 1
while a[mex % x] > 0:
a[mex % x] -= 1
mex += 1
out.append(mex)
print(*out) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
n, x = map(int, sys.stdin.readline().split())
data = [0] * x
cur_res = 0
level = 0
for _ in range(n):
num = int(sys.stdin.readline()) % x
data[num] += 1
pos = cur_res % x
if num == pos:
done = False
for i in range(pos, x):
if data[i] > level:
cur_res += 1
else:
done = True
break
if not done:
level += 1
while not done:
for i in range(x):
if data[i] > level:
cur_res += 1
else:
done = True
break
if not done:
level += 1
print(cur_res) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
q, x = map(int, stdin.readline().split())
a = [0] * x
mex = 0
for i in range(q):
e = int(stdin.readline())
a[e % x] += 1
while a[mex % x] >= mex // x + 1:
mex += 1
print(mex) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
qx = list(map(int, stdin.readline().split()))
q = qx[0]
x = qx[1]
dic = {}
s = set()
mex = 0
for i in range(q):
y = int(stdin.readline())
m = y % x
if m not in dic:
dic[m] = m
s.add(m)
else:
dic[m] += x
s.add(dic[m])
while mex in s:
mex += 1
stdout.write(str(mex) + "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
q, x = map(int, stdin.readline().split())
d = {i: (0) for i in range(x)}
segs = {(0): 0}
queries = []
for _ in range(q):
i = int(stdin.readline().strip()) % x
queries.append(i)
d[i] += 1
m = min(d.values())
cur = next(filter(lambda x: x[1] == m, d.items()))[0] + m * x
lines = []
for i in reversed(queries):
lines.append("{}\n".format(cur))
d[i] -= 1
cur = min(d[i] * x + i, cur)
stdout.writelines(reversed(lines)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
def build(v, l, r):
if l == r:
do[v] = l
else:
m = (l + r) // 2
build(2 * v, l, m)
build(2 * v + 1, m + 1, r)
do[v] = min(do[2 * v + 1], do[2 * v])
def update(v, l, r, value):
if l == r:
do[v] = 1000000000.0
else:
m = (l + r) // 2
if value <= m:
update(2 * v, l, m, value)
else:
update(2 * v + 1, m + 1, r, value)
do[v] = min(do[2 * v + 1], do[2 * v])
q, x = map(int, input().split())
mods = [-1] * x
do = [0] * 4 * q
build(1, 0, q)
g = sys.stdin.readlines()
for i in range(q):
y = int(g[i].replace("\n", ""))
mods[y % x] += 1
update(1, 0, q, mods[y % x] * x + y % x)
print(do[1]) | IMPORT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
def main():
q, x = map(int, stdin.readline().split())
mex = 0
m = [0] * x
i = 0
for j in range(q):
el = int(stdin.readline())
m[el % x] += 1
while m[i % x] > 0:
m[i % x] -= 1
i += 1
stdout.write(str(i) + "\n")
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | q, x = map(int, input().split())
a, m, s = [0] * (x + 1), 0, ""
while q > 0:
q -= 1
a[int(input()) % x] += 1
while a[m % x] > 0:
a[m % x] -= 1
m += 1
s += str(m) + "\n"
print(s) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER STRING WHILE VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | from sys import stdin, stdout
t = 1
for i in range(t):
q, x = map(int, stdin.readline().split())
mul = [0] * x
ANS = 0
ans = [False] * 500000
for i in range(q):
y = int(stdin.readline())
m = y % x
if mul[m] * x + m < 500000:
ans[mul[m] * x + m] = True
mul[m] += 1
while ans[ANS]:
ANS += 1
stdout.write(str(ANS) + "\n") | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | n, x = map(int, input().split())
arr, result = [0] * n, []
mex = 0
for _ in range(n):
arr[int(input()) % x] += 1
while arr[mex % x]:
arr[mex % x] -= 1
mex += 1
result.append(mex)
print("\n".join(map(str, result))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
q, x = list(map(int, sys.stdin.readline().rstrip().split(" ")))
punt = 0
m = [0] * x
for i in range(q):
y = int(sys.stdin.readline().rstrip())
m[y % x] += 1
while m[punt % x] > 0:
m[punt % x] -= 1
punt += 1
print(punt) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array.
You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.
You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.
In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.
You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).
You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).
Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$.
-----Input-----
The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$.
The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array.
-----Output-----
Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.
-----Examples-----
Input
7 3
0
1
2
2
0
0
10
Output
1
2
3
3
4
4
7
Input
4 3
1
2
1
2
Output
0
0
0
0
-----Note-----
In the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$. | import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
n, m = mints()
rem = [0] * m
mex = 0
for i in range(n):
q = mint()
rem[q % m] += 1
while rem[mex % m] > 0:
rem[mex % m] -= 1
mex += 1
print(mex) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.