description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, hand: List[int], W: int) -> bool: if len(hand) % W: return False cnt = Counter(hand) cards = sorted(cnt) while True: first = cards.pop() if not cnt[first]: continue elif cnt[first] > 1: cnt[first] -= 1 cards.append(first) else: del cnt[first] for i in range(first - 1, first - W, -1): if not cnt[i]: return False elif cnt[i] > 1: cnt[i] -= 1 else: del cnt[i] if not cnt: return True return False
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR RETURN NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: h = Counter(nums) unique = sorted(h.keys()) for i in unique: while h[i] > 0: h[i] -= 1 for j in range(i + 1, i + k): if h[j] <= 0: return False h[j] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: if len(nums) % k != 0: return False d = collections.Counter(nums) s = list(d) s.sort() for n in s: if n in d: c = collections.Counter({m: d[n] for m in range(n, n + k)}) if all([(key in d and d[key] >= c[key]) for key in c]): d -= c else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: l = len(nums) if l % k != 0: return False g = int(l // k) num_dict = dict() for num in nums: num_dict[num] = num_dict.get(num, 0) + 1 for i in range(g): val = min(num_dict.keys()) if num_dict[val] == 1: del num_dict[val] else: num_dict[val] -= 1 for j in range(1, k): val += 1 if val not in num_dict: return False elif num_dict[val] == 1: del num_dict[val] else: num_dict[val] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: n = len(nums) if n % k != 0: return False count = Counter(nums) nums = sorted(count.keys()) while nums: for num in range(nums[0], nums[0] + k): if num not in count or count[num] == 0: return False count[num] -= 1 if count[num] == 0: nums.remove(num) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: count = collections.Counter(nums) print(count) nums.sort() for x in nums: if count[x] == 0: continue else: for i in range(1, k): print(x, x + i) if count[x + i] > 0: count[x + i] -= 1 elif count[x - i] > 0: count[x - i] -= 1 else: return False count[x] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: W = k hand = nums hand.sort() mp = defaultdict(list) for elem in hand: prev_len = -1 if len(mp[elem - 1]) == 0 else heapq.heappop(mp[elem - 1]) if prev_len == -1 or prev_len == W: heapq.heappush(mp[elem], 1) else: heapq.heappush(mp[elem], prev_len + 1) for key in mp.keys(): for length in mp[key]: if length != W: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: count = collections.Counter(nums) heap = [] for num, cnt in count.items(): heapq.heappush(heap, (num, cnt)) while heap: nums, cnts = [], [] for _ in range(k): if not heap: return False else: num, cnt = heapq.heappop(heap) nums.append(num) cnts.append(cnt) if not all(b == a + 1 for a, b in zip(nums, nums[1:])): return False for num, cnt in zip(nums, cnts): if cnt > 1: heapq.heappush(heap, (num, cnt - 1)) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: count = collections.Counter(nums) que = collections.deque() last_required = 0 last_num = 0 for num, num_count in sorted(count.items()): if last_required > 0 and last_num + 1 != num or num_count < last_required: return False que.append(num_count - last_required) last_required = num_count if len(que) == k: last_required -= que.popleft() last_num = num return last_required == 0 def isPossibleDivide_II(self, nums: List[int], k: int) -> bool: count = collections.Counter(nums) for next_num in sorted(count): next_dup = count[next_num] for j in range(next_num, next_num + k): if count[j] < next_dup: return False count[j] -= next_dup return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR RETURN NUMBER VAR VAR VAR RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: counter = Counter(nums) heap_nums = [] groups = [] for key, val in counter.items(): heapq.heappush(heap_nums, (key, val)) while heap_nums: count = 0 sub_group = [] remaining_elements = [] while count < k: if not heap_nums: return False popped_elem = heapq.heappop(heap_nums) if popped_elem[1] - 1 > 0: remaining_elements.append(popped_elem) if not sub_group: sub_group.append(popped_elem[0]) elif popped_elem[0] - 1 == sub_group[-1]: sub_group.append(popped_elem[0]) else: return False count += 1 groups += sub_group for elem in remaining_elements: heapq.heappush(heap_nums, (elem[0], elem[1] - 1)) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: n = len(nums) if n % k != 0: return False counter = collections.Counter(nums) keys = sorted(counter) print(keys) for i in keys: while i in counter: cur = i counter[cur] -= 1 if not counter[cur]: del counter[cur] for _ in range(k - 1): if cur + 1 in counter: cur += 1 counter[cur] -= 1 if not counter[cur]: del counter[cur] else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: nums.sort() D = deque() if len(nums) % k != 0: return False target_set_count = len(nums) // k for num in nums: if len(D): prev_val, count = D.pop() if num == prev_val + 1: prev_val += 1 count += 1 else: D.appendleft((prev_val, count)) count = 1 else: prev_val, count = num, 1 if count < k: D.appendleft((prev_val, count)) if len(D) > target_set_count: break return len(D) == 0
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: if len(nums) % k: return False if k == 1: return True n = len(nums) cnt = collections.Counter(nums) heapify(nums) for i in range(n // k): start = heappop(nums) while cnt[start] == 0: start = heappop(nums) for i in range(k): if cnt[start + i] == 0: return False cnt[start + i] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False.   Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false Explanation: Each array should be divided in subarrays of size 3.   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 1 <= k <= nums.length
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: numDict = {} for num in nums: if str(num) in numDict: numDict[str(num)] += 1 else: numDict[str(num)] = 1 for key in sorted(list(numDict.keys()), key=lambda x: int(x)): val = numDict[key] if val == 0: continue for i in range(1, k): if str(int(key) + i) not in numDict: return False temp = numDict[str(int(key) + i)] - val if temp < 0: return False numDict[str(int(key) + i)] = temp return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR
Monocarp plays a computer game (yet again!). This game has a unique trading mechanics. To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price $x$, then he can trade it for any item (exactly one item) with price not greater than $x+k$. Monocarp initially has $n$ items, the price of the $i$-th item he has is $a_i$. The character Monocarp is trading with has $m$ items, the price of the $i$-th item they have is $b_i$. Monocarp can trade with this character as many times as he wants (possibly even zero times), each time exchanging one of his items with one of the other character's items according to the aforementioned constraints. Note that if Monocarp gets some item during an exchange, he can trade it for another item (since now the item belongs to him), and vice versa: if Monocarp trades one of his items for another item, he can get his item back by trading something for it. You have to answer $q$ queries. Each query consists of one integer, which is the value of $k$, and asks you to calculate the maximum possible total cost of items Monocarp can have after some sequence of trades, assuming that he can trade an item of cost $x$ for an item of cost not greater than $x+k$ during each trade. Note that the queries are independent: the trades do not actually occur, Monocarp only wants to calculate the maximum total cost he can get. -----Input----- The first line contains three integers $n$, $m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the prices of the items Monocarp has. The third line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le 10^9$) — the prices of the items the other character has. The fourth line contains $q$ integers, where the $i$-th integer is the value of $k$ for the $i$-th query ($0 \le k \le 10^9$). -----Output----- For each query, print one integer — the maximum possible total cost of items Monocarp can have after some sequence of trades, given the value of $k$ from the query. -----Examples----- Input 3 4 5 10 30 15 12 31 14 18 0 1 2 3 4 Output 55 56 60 64 64 -----Note----- None
import sys int1 = lambda x: int(x) - 1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() inf = 4294967295 md = 998244353 class UnionFind: def __init__(self, n, cnt_a, val): self.cnt_a = cnt_a self.val = val self.state = [-1] * n self.size_table = [1] * n self.rr = list(range(1, n + 1)) self.s = sum(val) self.cnt = n def root(self, u): stack = [] while self.state[u] >= 0: stack.append(u) u = self.state[u] for v in stack: self.state[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u = self.root(u) v = self.root(v) if u == v: return False du = -self.state[u] dv = -self.state[v] if du < dv: u, v = v, u if du == dv: self.state[u] -= 1 self.state[v] = u self.cnt -= 1 self.size_table[u] += self.size_table[v] self.rr[u] = max(self.rr[u], self.rr[v]) self.cnt_a[u] += self.cnt_a[v] self.s -= val[u] + val[v] r = self.rr[u] val[u] = cs[r] - cs[r - self.cnt_a[u]] self.s += val[u] return True def size(self, u): return self.size_table[self.root(u)] n, m, q = LI() aa = LI() bb = LI() kk = LI() ab = [] for a in aa: ab.append((a, 1)) for a in bb: ab.append((a, 0)) ab.sort() cs = [0] cnt_a = [] val = [] for a, c in ab: cs.append(cs[-1] + a) cnt_a.append(c) if c: val.append(a) else: val.append(0) duv = [] for i in range(len(ab) - 1): d = ab[i + 1][0] - ab[i][0] duv.append((d, i, i + 1)) duv.sort(key=lambda x: -x[0]) uf = UnionFind(n + m, cnt_a, val) ik = [] for i, k in enumerate(kk): ik.append((i, k)) ik.sort(key=lambda x: x[1]) ans = [0] * q for i, k in ik: while duv and duv[-1][0] <= k: d, u, v = duv.pop() uf.merge(u, v) ans[i] = uf.s print(*ans, sep="\n")
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR STRING STRING VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Monocarp plays a computer game (yet again!). This game has a unique trading mechanics. To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price $x$, then he can trade it for any item (exactly one item) with price not greater than $x+k$. Monocarp initially has $n$ items, the price of the $i$-th item he has is $a_i$. The character Monocarp is trading with has $m$ items, the price of the $i$-th item they have is $b_i$. Monocarp can trade with this character as many times as he wants (possibly even zero times), each time exchanging one of his items with one of the other character's items according to the aforementioned constraints. Note that if Monocarp gets some item during an exchange, he can trade it for another item (since now the item belongs to him), and vice versa: if Monocarp trades one of his items for another item, he can get his item back by trading something for it. You have to answer $q$ queries. Each query consists of one integer, which is the value of $k$, and asks you to calculate the maximum possible total cost of items Monocarp can have after some sequence of trades, assuming that he can trade an item of cost $x$ for an item of cost not greater than $x+k$ during each trade. Note that the queries are independent: the trades do not actually occur, Monocarp only wants to calculate the maximum total cost he can get. -----Input----- The first line contains three integers $n$, $m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the prices of the items Monocarp has. The third line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le 10^9$) — the prices of the items the other character has. The fourth line contains $q$ integers, where the $i$-th integer is the value of $k$ for the $i$-th query ($0 \le k \le 10^9$). -----Output----- For each query, print one integer — the maximum possible total cost of items Monocarp can have after some sequence of trades, given the value of $k$ from the query. -----Examples----- Input 3 4 5 10 30 15 12 31 14 18 0 1 2 3 4 Output 55 56 60 64 64 -----Note----- None
import sys int1 = lambda x: int(x) - 1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() inf = 4294967295 md = 998244353 n, m, q = LI() aa = LI() bb = LI() kk = LI() s = sum(aa) ab = [] for a in aa: ab.append((a, 1)) for a in bb: ab.append((a, 0)) ab.sort() cs = [0] cnt_a = [] val = [] for a, c in ab: cs.append(cs[-1] + a) cnt_a.append(c) if c: val.append(a) else: val.append(0) duv = [] for i in range(len(ab) - 1): d = ab[i + 1][0] - ab[i][0] duv.append((d, i, i + 1)) duv.sort(key=lambda x: -x[0]) ik = [] for i, k in enumerate(kk): ik.append((i, k)) ik.sort(key=lambda x: x[1]) ans = [0] * q ll = list(range(n + m)) rr = list(range(1, n + m + 1)) for i, k in ik: while duv and duv[-1][0] <= k: d, u, v = duv.pop() l = ll[u] mid = ll[v] r = rr[v] s -= val[l] + val[mid] cnt_a[l] += cnt_a[mid] val[l] = cs[r] - cs[r - cnt_a[l]] s += val[l] ll[r - 1] = l rr[l] = r ans[i] = s print(*ans, sep="\n")
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR STRING STRING VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Monocarp plays a computer game (yet again!). This game has a unique trading mechanics. To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price $x$, then he can trade it for any item (exactly one item) with price not greater than $x+k$. Monocarp initially has $n$ items, the price of the $i$-th item he has is $a_i$. The character Monocarp is trading with has $m$ items, the price of the $i$-th item they have is $b_i$. Monocarp can trade with this character as many times as he wants (possibly even zero times), each time exchanging one of his items with one of the other character's items according to the aforementioned constraints. Note that if Monocarp gets some item during an exchange, he can trade it for another item (since now the item belongs to him), and vice versa: if Monocarp trades one of his items for another item, he can get his item back by trading something for it. You have to answer $q$ queries. Each query consists of one integer, which is the value of $k$, and asks you to calculate the maximum possible total cost of items Monocarp can have after some sequence of trades, assuming that he can trade an item of cost $x$ for an item of cost not greater than $x+k$ during each trade. Note that the queries are independent: the trades do not actually occur, Monocarp only wants to calculate the maximum total cost he can get. -----Input----- The first line contains three integers $n$, $m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the prices of the items Monocarp has. The third line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le 10^9$) — the prices of the items the other character has. The fourth line contains $q$ integers, where the $i$-th integer is the value of $k$ for the $i$-th query ($0 \le k \le 10^9$). -----Output----- For each query, print one integer — the maximum possible total cost of items Monocarp can have after some sequence of trades, given the value of $k$ from the query. -----Examples----- Input 3 4 5 10 30 15 12 31 14 18 0 1 2 3 4 Output 55 56 60 64 64 -----Note----- None
from itertools import accumulate class Dsu: def __init__(self, n): self.f = list(range(n)) def find(self, x): if self.f[x] != x: self.f[x] = self.find(self.f[x]) return self.f[x] def union(self, i, j, cnt, psum, maxv): fi, fj = self.find(i), self.find(j) if fi != fj: if fi < fj: fi, fj = fj, fi self.f[fj] = fi l1, l2 = cnt[fi], cnt[fj] l3 = l1 + l2 cnt[fi] = l3 maxv += ( psum[fi + 1 - l1] - psum[fi + 1 - l3] - (psum[fj + 1] - psum[fj + 1 - l2]) ) return maxv def main(): n, m, q = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) qs = list(map(int, input().split())) qss = list(zip(qs, list(range(q)))) qss.sort() nums = a + b nums.sort() tot = len(nums) cnt = [0] * tot i = j = 0 a.sort() while i < n: while a[i] != nums[j]: j += 1 cnt[j] = 1 i += 1 j += 1 edges = [] for i in range(tot): if i + 1 < tot: edges.append([nums[i + 1] - nums[i], i, i + 1]) edges.sort(reverse=True) dsu = Dsu(tot) psum = [0] + list(accumulate(nums)) maxv = sum(a) ans = [0] * q for k, idx in qss: while edges and edges[-1][0] <= k: _, i, j = edges.pop() maxv = dsu.union(i, j, cnt, psum, maxv) ans[idx] = maxv print(*ans, sep=" ") return main()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
import sys n = int(input()) degs = [None] * n xors = [None] * n degsdict = {} for i, l in enumerate(sys.stdin.read().splitlines()[:n]): d, xors[i] = map(int, l.split()) degs[i] = d if not d in degsdict: degsdict[d] = set() degsdict[d].add(i) edges = [] if not 1 in degsdict: print(0) sys.exit() while degsdict[1]: v = degsdict[1].pop() n = xors[v] edges.append([v, n]) ndeg = degs[n] degsdict[ndeg].remove(n) ndeg -= 1 if not ndeg in degsdict: degsdict[ndeg] = set() degsdict[ndeg].add(n) degs[n] -= 1 xors[n] = xors[n] ^ v print(len(edges)) print("\n".join([" ".join(map(str, i)) for i in edges]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
__author__ = "artyom" read = lambda: map(int, input().split()) n = int(input()) graph = [set() for _ in range(n)] degrees = [] adj = [] queue = [] for i in range(n): degree, s = read() degrees.append(degree) adj.append(s) if degree == 1: queue.append(i) counter = 0 ans = "" while queue: v = queue.pop() if degrees[v] == 0: continue s = adj[v] for u in graph[v]: s ^= u graph[s].add(v) graph[v].add(s) counter += 1 ans += "\n" + str(s) + " " + str(v) degrees[s] -= 1 if degrees[s] == 1: queue.append(s) print(str(counter) + ans)
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
def main(): n = int(input()) ones, l = set(), [] for i in range(n): degree, s = map(int, input().split()) if degree == 1: ones.add(i) l.append((degree, s)) res = [] while ones: i = ones.pop() degree_i, j = l[i] degree_j, s = l[j] res.append(" ".join((str(i), str(j)))) if degree_j == 1: ones.remove(j) else: degree_j -= 1 if degree_j == 1: ones.add(j) l[j] = degree_j, s ^ i print(len(res)) print("\n".join(res)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) f = [list(map(int, input().split())) for i in range(n)] deg1 = [] edges = [] for i in range(n): if f[i][0] == 1: deg1.append(i) while len(deg1): i = deg1.pop() if f[i][0] == 0: continue edges.append([i, f[i][1]]) f[i][0] -= 1 f[f[i][1]][0] -= 1 f[f[i][1]][1] ^= i if f[f[i][1]][0] == 1: deg1.append(f[i][1]) print(len(edges)) for e in edges: print(e[0], e[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) v = [] deg1 = [] degsum = 0 has_v = [True] * n for i in range(n): d, s = map(int, input().split()) degsum += d if d == 1: deg1.append(i) if d == 0: has_v[i] = False v.append([d, s]) print(degsum // 2) edge = [] while deg1 != []: f = deg1.pop() if has_v[f] is False: continue v[f][0] -= 1 has_v[f] = False t = v[f][1] print(f, t) v[t][0] -= 1 v[t][1] ^= f if v[t][0] == 1: deg1.append(t) elif v[t][0] == 0: has_v[t] = False
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) verts = [] e = [] for i in range(n): inp = [*map(int, input().split(" "))] verts.append([i] + inp) leavesQ = list(filter(lambda v: v[1] == 1, verts)) while len(leavesQ) > 0: l = leavesQ.pop() if l[1] != 1: continue l2 = verts[l[2]] e.append([l[0], l2[0]]) l[1] -= 1 l2[1] -= 1 l2[2] ^= l[0] if l2[1] == 1: leavesQ.append(l2) print(len(e)) for [x, y] in e: print(x, y)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
import sys input = sys.stdin.readline n = int(input()) sum_deg, adj, stk = 0, [], [] for i in range(n): d, s = map(int, input().split()) sum_deg += d adj.append([d, s]) if d == 1: stk.append(i) print(sum_deg // 2) while stk: u = stk.pop() if adj[u][0] != 1: continue v = adj[u][1] adj[u][0] -= 1 adj[u][1] ^= v adj[v][0] -= 1 adj[v][1] ^= u print(u, v) if adj[v][0] == 1: stk.append(v)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) stack_leaves = [] deg = [0] * n s = [0] * n summ = 0 for i in range(n): deg[i], s[i] = map(int, input().split()) summ += deg[i] if deg[i] == 1: stack_leaves.append(i) print(summ // 2) while stack_leaves: x = stack_leaves.pop() if deg[x] != 0: print(x, s[x]) deg[s[x]] -= 1 s[s[x]] ^= x if deg[s[x]] == 1: stack_leaves.append(s[x])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
def main(): n, l, ones, j = int(input()), [], [], 0 for i in range(n): degree, s = map(int, input().split()) l.append((degree, s)) j += degree if degree == 1: ones.append(i) print(j // 2) while ones: i = ones.pop() degree, j = l[i] if degree == 1: print(i, j) degree, s = l[j] s ^= i degree -= 1 l[j] = degree, s if degree == 1: ones.append(j) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
def main(): (n,) = read() degsum = [] stack = [] edges = 0 for i in range(n): degsum.append(tuple(read())) edges += degsum[-1][0] if degsum[-1][0] == 1: stack.append(i) print(edges // 2) while stack: v = stack.pop() if degsum[v][0] != 1: continue u = degsum[v][1] print(v, u) degsum[v] = 0, 0 degsum[u] = degsum[u][0] - 1, degsum[u][1] ^ v if degsum[u][0] == 1: stack.append(u) def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF VAR NONE ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
R = lambda: map(int, input().split()) n = int(input()) degs, xors = [0] * (2**16 + 1), [0] * (2**16 + 1) edges = [] for curr in range(n): degs[curr], xors[curr] = R() q = [] for curr in range(n): if degs[curr] == 1: q.append(curr) while q: curr = q.pop() if degs[curr] != 1: continue neighbor = xors[curr] edges.append((min(curr, neighbor), max(curr, neighbor))) degs[neighbor] -= 1 xors[neighbor] ^= curr if degs[neighbor] == 1: q.append(neighbor) filter(lambda p: p[0] < p[1], edges) print(len(edges)) for u, v in edges: if u < v: print(u, v)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) l = [] stack = [] for i in range(n): d, s = map(int, input().split()) if d == 1: stack.append(i) l.append([d, s]) c = 0 edges = "" while stack: i = stack.pop(0) d, s = l[i] if d == 0: continue dd, ss = l[s] if dd == 2: stack.append(s) l[s] = [dd - 1, ss ^ i] edges += str(i) + " " + str(s) + "\n" c += 1 print(c) print(edges)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) deg2vs = {} ss = [0] * n degs = [0] * n for i in range(n): degs[i], ss[i] = list(map(int, input().split())) if degs[i] > 0: if degs[i] not in deg2vs: deg2vs[degs[i]] = set() deg2vs[degs[i]].add(i) edges = [] while len(deg2vs) != 0: leaves = deg2vs.pop(1) for leaf in leaves: if degs[leaf] == 0: continue v = ss[leaf] edges.append((leaf, v)) v_deg = degs[v] if v_deg > 1: deg2vs[v_deg].remove(v) if len(deg2vs[v_deg]) == 0: deg2vs.pop(v_deg) ss[v] = ss[v] ^ leaf degs[v] -= 1 if degs[v] > 0: if degs[v] not in deg2vs: deg2vs[degs[v]] = set() deg2vs[degs[v]].add(v) print(len(edges)) for edge in edges: print(edge[0], edge[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n, st, xors, sumst, buf = int(input()), [], [], 0, [] for i in range(n): s = input().split() st.append(int(s[0])) xors.append(int(s[1])) sumst += st[i] if st[i] == 1: buf.append(i) if sumst % 2 != 0: print("0") exit(0) print(sumst // 2) while buf: v = buf.pop() if st[v] == 1: print(str(v) + " " + str(xors[v])) xors[xors[v]] ^= v st[xors[v]] -= 1 if st[xors[v]] == 1: buf.append(xors[v])
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST NUMBER LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
import sys def main(): import sys tokens = [int(i) for i in sys.stdin.read().split()] tokens.reverse() n = tokens.pop() vertices = [[tokens.pop(), tokens.pop()] for i in range(n)] l = [] for i in range(n): if vertices[i][0] == 1: l.append(i) result = [] while l: v = l.pop() if vertices[v][0] == 0: continue u = vertices[v][1] result.append((v, u)) vertices[u][0] -= 1 vertices[u][1] ^= v if vertices[u][0] == 1: l.append(u) print(len(result)) print("\n".join(" ".join(str(j) for j in i) for i in result)) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
def main(): n, l, ones = int(input()), [], [] for i in range(n): degree, s = map(int, input().split()) l.append((degree, s)) if degree == 1: ones.append(i) res = [] while ones: i = ones.pop() degree, j = l[i] if degree == 1: res.append(" ".join((str(i), str(j)))) degree, s = l[j] s ^= i degree -= 1 l[j] = degree, s if degree == 1: ones.append(j) print(len(res)) print("\n".join(res)) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. Input The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space. Output In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. Examples Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 Note The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".
n = int(input()) d = [] v = [] for i in range(n): a, b = map(int, input().split()) v.append([a, b, i]) g = dict() for i in range(n + 20): g[i] = set() for i in v: if i[0] in g: g[i[0]].add(i[2]) else: g[i[0]] = set() g[i[0]].add(i[2]) ans = [] while len(g[1]) > 0: i = 0 for x in g[1]: i = x break a = v[i][2] b = v[i][1] g[v[a][0]].discard(v[a][2]) g[v[a][0] - 1].add(v[a][2]) v[a][0] -= 1 g[v[b][0]].discard(v[b][2]) g[v[b][0] - 1].add(v[b][2]) v[b][0] -= 1 v[b][1] ^= a ans.append((a, b)) print(len(ans)) for i in ans: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
for _ in range(int(input())): n = int(input()) x = 2**n a1 = [] a2 = [] l = {} y = list(map(int, input().split())) y.sort() for i in range(1, x): if y[i] in l and l[y[i]] != 0: l[y[i]] -= 1 else: t = [] for j in a1: t.append(j + y[i]) for j in a2: t.append(j + y[i]) for j in t: if j not in l: l[j] = 0 l[j] += 1 a1.append(j) a2.append(y[i]) if len(a2) == n: break print(*a2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
for _ in range(int(input())): n = int(input()) arr = [int(c) for c in input().split()] subset = [] arr.sort() p = [] ans = [] x = 1 while len(ans) != n: while len(p) > 0 and p[0] < arr[x]: p.pop(0) if p and p[0] == arr[x]: p.pop(0) else: ans.append(arr[x]) subset.append(arr[x]) for i in range(len(subset) - 1): subset.append(subset[i] + arr[x]) p.append(subset[i] + arr[x]) p.sort() x += 1 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
def ans(arr, size): if size == 1: return [arr[1]] m = arr[1] new_arr = dict() now = list() for item in arr: if item - m in new_arr and new_arr[item - m] > 0: new_arr[item - m] -= 1 else: if item in new_arr: new_arr[item] += 1 else: new_arr[item] = 1 now.append(item) prev = ans(now, size - 1) prev.append(m) return prev cases = int(input()) for _ in range(cases): n = int(input()) arr = list(map(int, input().split())) r = ans(sorted(arr), n) r.sort() print(" ".join(list(map(str, r))))
FUNC_DEF IF VAR NUMBER RETURN LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
t = int(input()) for _ in range(t): n = int(input()) num = list(map(int, input().split())) num.sort() sum_arr1 = [] sum_arr2 = [] num_arr = [num[1]] point = 2 while len(num_arr) < n: if len(num_arr) == 1: num_arr.append(num[point]) sum_arr1.append(num_arr[0] + num[point]) sum_arr2.append(num_arr[0] + num[point]) elif num[point] in sum_arr2: sum_arr2.remove(num[point]) else: num_arr.append(num[point]) aa = len(sum_arr1) for e in range(len(num_arr) - 1): sum_arr1.append(num[point] + num_arr[e]) sum_arr2.append(num[point] + num_arr[e]) for f in range(aa): sum_arr1.append(sum_arr1[f] + num[point]) sum_arr2.append(sum_arr1[f] + num[point]) point += 1 print(*num_arr)
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 LIST ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def get_int(): return int(sys.stdin.readline().strip()) def get_list_strings(): return list(map(str, sys.stdin.readline().strip().split())) def solve(N, arr): store = {} for ele in arr: if ele not in store: store[ele] = 1 else: store[ele] += 1 del store[0] final = [] for ele in store: final.append([ele, store[ele]]) final.sort() elements = {} tracker = {} l = 0 for array in final: ele = array[0] count = array[1] current = 0 if l == N: break while current < count: if ele not in tracker: elements[ele] = 1 l += 1 elif tracker[ele] < count: if ele not in elements: elements[ele] = 1 else: elements[ele] += 1 l += 1 else: break if l == N: break temp = {ele: 1} for key in tracker: count1 = tracker[key] val = ele + key if val in temp: temp[val] += count1 else: temp[val] = count1 for key in temp: if key not in tracker: tracker[key] = temp[key] else: tracker[key] += temp[key] current = tracker[ele] ans = [] for key in elements: count = elements[key] for i in range(count): ans.append(key) ans.sort() sys.stdout.write(" ".join(map(str, ans)) + "\n") return T = get_int() while T: N = get_int() arr = get_list() solve(N, arr) T -= 1
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR DICT VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
def anumla(sumset, n): valueset = [] sum_arr = [] rem = {} for i in range(2**n - 1): if sumset[i] in rem and rem[sumset[i]] != 0: rem[sumset[i]] -= 1 else: sumsubset = [] for el in valueset: sumsubset.append(el + sumset[i]) for el in sum_arr: sumsubset.append(el + sumset[i]) for el in sumsubset: if el not in rem: rem[el] = 0 rem[el] += 1 sum_arr.append(el) valueset.append(sumset[i]) if len(valueset) == n: break return valueset t = int(input()) for _ in range(t): n = int(input()) sumset = list(map(int, input().split())) sumset.sort() print(*anumla(sumset[1:], n))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in Mandarin Chinese and Russian as well. Mahesh got a beautiful array named A as a birthday gift from his beautiful girlfriend Namratha. There are N positive integers in that array. Mahesh loved the array so much that he started to spend a lot of time on it everyday. One day, he wrote down all possible subsets of the array. Then for each subset, he calculated the sum of elements in that subset and wrote it down on a paper. Unfortunately, Mahesh lost the beautiful array :(. He still has the paper on which he wrote all subset sums. Your task is to rebuild beautiful array A and help the couple stay happy :) ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. First line of each test case contains one integer N, the number of elements in A. Second line of each test case contains 2^{N} integers, the values written on paper ------ Output ------ For each test case, output one line with N space separated integers in non-decreasing order. ------ Constraints ------ $1 ≤ T ≤ 50 $$1 ≤ N ≤ 15 $$0 ≤ Values on paper ≤ 10^{9} $$All input values are valid. A solution always exists ------ Example ------ ------ Input Format ------ 2 1 0 10 2 0 1 1 2 ------ Output Format ------ 10 1 1 ------ Explanation ------ Test case #2 For the array [1,1], possible subsets are {}, {1}, {1}, {1,1}, respective sums are 0, 1, 1, 2.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) ans = [] hp = {} dups = {} a.sort() for i in range(1, len(a)): if a[i] in hp and hp[a[i]] > 0: hp[a[i]] -= 1 else: new_subsets = [] for el in dups: new_subsets.append((el + a[i], dups[el])) for el in ans: new_subsets.append((el + a[i], 1)) for el in new_subsets: dups[el[0]] = dups.get(el[0], 0) + el[1] hp[el[0]] = hp.get(el[0], 0) + el[1] ans.append(a[i]) if len(ans) == n: break 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 LIST ASSIGN VAR DICT ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arrival, departure): arrival.sort() departure.sort() platforms = 1 max_platforms = 1 i = 1 j = 0 while i < len(arrival) and j < len(departure): if arrival[i] <= departure[j]: platforms += 1 i += 1 if platforms > max_platforms: max_platforms = platforms else: platforms -= 1 j += 1 return max_platforms
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): c = 1 arr.sort() dep.sort() platforms_needed_at_a_time = 0 result = 0 i = 0 j = 0 while i < n and j < n: if arr[i] <= dep[j]: platforms_needed_at_a_time += 1 i += 1 elif dep[j] < arr[i]: platforms_needed_at_a_time -= 1 j += 1 result = max(platforms_needed_at_a_time, result) return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): train = [] for i in range(n): train.append([arr[i], "a"]) train.append([dep[i], "d"]) train = sorted(train) n = len(train) count = 0 a = 0 d = 0 for i in train: if i[1] == "a": a += 1 else: d += 1 count = max(count, abs(a - d)) return count
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr = sorted(arr) dep = sorted(dep) times = [] for i in arr: times.append([i, "a"]) for i in dep: times.append([i, "d"]) times = sorted(times, key=lambda x: x[0]) platforms = 0 result = 0 for i in times: if i[1] == "a": platforms += 1 result = max(result, platforms) else: platforms -= 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() i = 0 ans = 1 for j in range(1, n): if arr[j] <= dep[i]: ans += 1 else: i += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() platforms = 1 max_platforms = 1 i, j = 1, 0 while i < n and j < n: if arr[i] <= dep[j]: platforms += 1 i += 1 elif arr[i] > dep[j]: platforms -= 1 j += 1 if platforms > max_platforms: max_platforms = platforms return max_platforms
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() i = j = count = max1 = 0 while i < n: if arr[i] <= dep[j]: i += 1 count += 1 max1 = max(max1, count) else: j += 1 count -= 1 return max1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr = sorted(arr) dep = sorted(dep) nplatform = 0 while len(arr) > 0: nplatform = nplatform + 1 ia = 0 id = ia * 1 chain = [0] dep_val = dep[0] del arr[0] del dep[0] nremaining = len(arr) for j in range(nremaining): if dep_val < arr[ia]: dep_val = dep[ia] del arr[ia] del dep[ia] id = ia * 1 else: ia = ia + 1 pass return nplatform
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): i = 0 j = 0 arr.sort() dep.sort() p = 0 m = p while i < len(arr) and j < len(dep): if arr[i] <= dep[j]: p += 1 i += 1 else: p -= 1 j += 1 m = max(m, p) return m
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): plat_needed = 1 final_count = 1 arr.sort() dep.sort() i, j = 1, 0 while i < n and j < n: if arr[i] <= dep[j]: plat_needed += 1 i += 1 else: plat_needed -= 1 j += 1 if plat_needed > final_count: final_count = plat_needed return final_count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() n = len(arr) platforms = 1 max_platforms = 1 arr_idx, dep_idx = 1, 0 while arr_idx < n and dep_idx < n: if arr[arr_idx] <= dep[dep_idx]: platforms += 1 arr_idx += 1 else: platforms -= 1 dep_idx += 1 max_platforms = max(max_platforms, platforms) return max_platforms
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): order = [] for i in range(n): order.append([arr[i], "a"]) order.append([dep[i], "d"]) order.sort(key=lambda x: x[1]) order.sort() result = 1 plat_needed = 0 for i in order: if i[1] == "a": plat_needed += 1 else: plat_needed -= 1 result = max(result, plat_needed) return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): cnt = [0] * 2401 for i in range(0, n): cnt[arr[i]] += 1 cnt[dep[i] + 1] -= 1 for i in range(1, 2400): cnt[i] += cnt[i - 1] return max(cnt)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() m, p, j = 0, 1, 0 for i in range(1, n): while j < i and dep[j] < arr[i]: j += 1 p = i - j + 1 m = max(m, p) return m
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): freq = [(0) for _ in range(2500)] for i in range(n): l, r = arr[i], dep[i] freq[l] += 1 freq[r + 1] -= 1 ans = 0 cur = 0 for num in freq: cur += num ans = max(ans, cur) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): cnt = 1 m = 0 arr.sort() dep.sort() i, j = 1, 0 while i < n: if arr[i] <= dep[j]: cnt = cnt + 1 i = i + 1 m = max(cnt, m) else: j = j + 1 cnt = cnt - 1 return m
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): d = {} for i in range(n): if arr[i] in d: if type(d[arr[i]]) == str: d[arr[i]] = ["a", "a"] else: d[arr[i]].append("a") else: d[arr[i]] = "a" for i in range(n): if dep[i] in d: if type(d[dep[i]]) == str: d[dep[i]] = [d[dep[i]], "d"] else: d[dep[i]].append("d") else: d[dep[i]] = "d" keys_sort = sorted(d) c = 0 max_count = 0 for key in keys_sort: if type(d[key]) == str: if d[key] == "a": c += 1 if max_count < c: max_count = c else: c -= 1 else: for i in d[key]: if i == "a": c += 1 if max_count < c: max_count = c else: c -= 1 return max_count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() a = [dep[0]] p = 1 for i in range(1, n): k = 0 for j in range(len(a)): if a[j] < arr[i]: a.pop(j) a.append(dep[i]) k = 1 break if k == 0: p += 1 a.append(dep[i]) return p
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr = sorted(arr) dep = sorted(dep) i, j, platform, result = 1, 0, 1, 1 while i < n and j < n: if arr[i] <= dep[j]: i += 1 platform += 1 else: platform -= 1 j += 1 if platform > result: result = platform return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): count = 0 plat = [] max_count = 0 for i in arr: plat.append((i, "a")) for i in dep: plat.append((i, "d")) plat_1 = sorted(plat, key=lambda tup: tup[0]) for i in plat_1: if i[1] == "a": count += 1 else: count -= 1 max_count = max(count, max_count) return max_count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): l = [0] * 2400 for i in arr: l[i] += 1 for i in dep: l[i + 1] -= 1 p = 0 for i in range(1, 2400): l[i] += l[i - 1] p = max(p, l[i]) return p
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): platform = [(0) for i in range(2361)] for i in range(n): platform[arr[i]] += 1 platform[dep[i] + 1] -= 1 minplatform = 1 for i in range(1, 2361): platform[i] = platform[i] + platform[i - 1] minplatform = max(minplatform, platform[i]) return minplatform
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def TimeInMinutes(self, time): if time >= 0 and time <= 2359: return time // 100 * 60 + time % 100 else: print("Invalid time input:", time) return 0 def minimumPlatform(self, n, arr, dep): MaxPlatform = 0 platReq = {} for t in range(n): m_arr = self.TimeInMinutes(arr[t]) m_dep = self.TimeInMinutes(dep[t]) sValue = platReq.get(m_arr, 0) eValue = platReq.get(m_dep, 0) if sValue == 0: platReq[m_arr] = "S" else: platReq[m_arr] = sValue + "S" if eValue == 0: platReq[m_dep] = "E" else: platReq[m_dep] = eValue + "E" newPlat = sorted(platReq.items()) MaxV = 0 count = 0 for i in range(len(newPlat)): v = newPlat[i][1] count += v.count("S") MaxV = max(count, MaxV) count -= v.count("E") return MaxV
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): temp = [] for el in dep: temp.append([el, "d"]) for el in arr: temp.append([el, "a"]) temp.sort() m = 0 cnt = 0 for el in temp: if el[1] == "a": cnt = cnt + 1 m = max(m, cnt) else: cnt = cnt - 1 return m
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() dp = [] count = 0 for i in range(0, n): if i == 0: count += 1 dp.append(dep[0]) else: j = min(dp) if j < arr[i]: dp[dp.index(j)] = dep[i] j = min(dp) else: dp.append(dep[i]) count += 1 return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): schedule = [(arrival, departure) for arrival, departure in zip(arr, dep)] schedule.sort() prev_departures = [float("-inf")] for arrival, departure in schedule: for i, prev_departure in enumerate(prev_departures): if prev_departure < arrival: prev_departures[i] = departure break else: prev_departures.append(departure) return len(prev_departures)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): dic = {} for i in range(n): if arr[i] in dic: dic[arr[i]] += 1 else: dic[arr[i]] = 1 if dep[i] + 1 in dic: dic[dep[i] + 1] -= 1 else: dic[dep[i] + 1] = -1 ans = 0 cnt = 0 for el in sorted(dic): cnt += dic[el] ans = max(ans, cnt) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): def findPlatform(arr, dep, n): times = [] for i in range(n): times.append([dep[i], "d"]) times.append([arr[i], "a"]) times = sorted(times, key=lambda x: x[1]) times = sorted(times, key=lambda x: x[0]) result, plat_needed = 0, 0 for i in range(2 * n): if times[i][1] == "a": plat_needed += 1 result = max(plat_needed, result) else: plat_needed -= 1 return result return findPlatform(arr, dep, n) arr = [900, 940, 950, 1100, 1500, 1800] dep = [910, 1200, 1120, 1130, 1900, 2000] n = len(arr)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): max = 1 cur = 1 ai = 1 di = 0 arr.sort() dep.sort() while ai < n: if arr[ai] <= dep[di]: ai += 1 cur += 1 if cur > max: max = cur else: cur -= 1 di += 1 return max
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() platform = 1 i = 1 j = 0 temp = 1 while i < n and j < n: if int(dep[j]) >= int(arr[i]): i = i + 1 temp = temp + 1 platform = max(platform, temp) else: j = j + 1 temp = temp - 1 return platform
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): arr.sort() dep.sort() i, j, ans = 0, 0, 0 while i < n: if arr[i] <= dep[j]: ans += 1 i += 1 else: i += 1 j += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): maxi = 0 c = 0 arr.sort() dep.sort() i, j = 0, 0 while i < n: if arr[i] <= dep[j]: c += 1 maxi = max(maxi, c) i += 1 else: c -= 1 j += 1 return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def minimumPlatform(self, n, arr, dep): max_platforms = 0 arr.sort() dep.sort() a = 0 d = 0 i_platforms = 0 while a < n and d < n: if arr[a] <= dep[d]: i_platforms += 1 a += 1 max_platforms = max(max_platforms, i_platforms) else: i_platforms -= 1 d += 1 return max_platforms
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms. Example 1: Input: n = 6 arr[] = {0900, 0940, 0950, 1100, 1500, 1800} dep[] = {0910, 1200, 1120, 1130, 1900, 2000} Output: 3 Explanation: Minimum 3 platforms are required to safely arrive and depart all trains. Example 2: Input: n = 3 arr[] = {0900, 1100, 1235} dep[] = {1000, 1200, 1240} Output: 1 Explanation: Only 1 platform is required to safely manage the arrival and departure of all trains. Your Task: You don't need to read input or print anything. Your task is to complete the function findPlatform() which takes the array arr[] (denoting the arrival times), array dep[] (denoting the departure times) and the size of the array as inputs and returns the minimum number of platforms required at the railway station such that no train waits. Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this may be > 59). Expected Time Complexity: O(nLogn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 50000 0000 ≤ A[i] ≤ D[i] ≤ 2359
class Solution: def func(self, a, b): if a[1] < b[1] or a[2] < b[2]: return a return b def minimumPlatform(self, n, arr, dep): train = [] for i in range(n): train.append([arr[i], "a"]) train.append([dep[i], "d"]) train = sorted(train, key=lambda x: x[1]) train = sorted(train, key=lambda x: x[0]) n = len(train) count = 0 a = 0 d = 0 for i in train: if i[1] == "a": a += 1 else: d += 1 count = max(count, abs(a - d)) return count
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() maxx = 0 k = 1 while 2 * k - 2 < n: i = 2 * k - 2 j = k while j >= 1: if a[i] > j: break i -= 1 j -= 1 if j == 0: maxx = k else: break k += 1 print(maxx)
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 WHILE BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
n = int(input()) for i in range(n): m = int(input()) hs = [int(x) for x in input().split()] t = hs.count(1) x = 0 y = 0 if t <= 1: print(str(t)) else: ii = 2 while ii <= t: tt = hs.count(ii) if tt == 0 and y == 0: t -= 1 elif tt > 1: y += tt - 1 elif tt == 0 and y > 0: y -= 1 ii += 1 print(str(t + x))
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) m = (n + 1) // 2 A = [int(i) for i in input().split()] A.sort() b = [] c = A.count(1) if c == 0: print(0) continue for i in range(c, n): if A[i] > i // 2 + 1: print(min(m, (i + 1) // 2)) break else: m = min(m, i + 2 - A[i]) else: print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() a.append(n + 1) c = set(a) c = list(c) c.sort() b = a.count(1) if b == 0: print(0) continue elif b == 1: print(1) continue else: for k in range(b, -1, -1): ans = 0 room = 1 for i in range(len(c)): if c[i] > k: c[i] = k break for j in range(i - 1, 0, -1): room += c[j + 1] - c[j] p = a.count(c[j]) if p <= room: ans += p room -= p else: ans += room room = 0 ans += b if ans >= 2 * k - 1: break print(k)
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 EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for x in range(int(input())): n = int(input()) a = sorted([int(y) for y in input().split()]) for k in range(n, -1, -1): if all(k - 1 + i < n and a[k - 1 + i] <= i + 1 for i in range(k)): print(k) break
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 VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) a = [int(i) for i in input().split()] a.sort() k = 0 for i in range((n + 1) // 2): true = True for j in range(i + 1): if a[i + j] > j + 1: true = False if true: k += 1 else: break print(k)
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 FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) array = [int(x) for x in input().split()] k_max = array.count(1) if k_max == 0: print(0) else: kk = 0 array = sorted(array) for k in range(1, min(k_max + 1, int((n + 3) / 2))): array_0 = array[k - 1 : 2 * k] if all([(array_0[i] <= i + 1) for i in range(k)]): kk = k print(kk)
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 NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
def cal(n, arr): if arr[0] != 1: return 0 if n == 1: return 1 t = arr.count(1) for i in range(t, -1, -1): if i + i - 1 > n: continue tag = 0 for j in range(i - 1, 2 * i - 1): if arr[j] > j - i + 2: tag = 1 break if tag == 0: return i for jj in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] arr.sort() print(cal(n, arr))
FUNC_DEF IF VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) game = list(map(int, input().split())) game.sort() k = 0 bl = True while bl: k += 1 a = [] m = 1 for i in range(n): if game[i] <= k: a.append(game[i]) if len(a) == 0: bl = False else: for j in range(1, len(a) + 1): if a[-j] <= k - m + 1: m += 1 if m == k + 1 and len(a) - j < k - 1: bl = False if m <= k: bl = False print(k - 1)
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 NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n == 1: print(1) else: c = a.count(1) k = 2 xx = 0 j = 0 cc = c nx = 0 while c > 0: if c == 1: xx += 1 c -= 1 else: x = a.count(k) nx += x if x == 0: if nx + cc % 2 < k - 1: c -= 2 xx += 1 else: xx += 1 c -= 1 if nx + cc % 2 >= k - 1 and nx < k - 1: c -= 1 elif x < c: c -= x xx += x else: xx += c c -= c k += 1 print(xx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
test = int(input()) for i in range(test): n = int(input()) array = sorted([int(x) for x in input().split()]) x1 = array.count(1) if x1 == 0: print(0) else: k = x1 for j in range(x1): for s in range(k, 1, -1): zs = len([x for x in array if x <= s]) - k if zs < s - 1: k -= 1 break else: ans = k print(k) break
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) s = [int(x) for x in input().split()] s.sort() q = 0 p = [] m = 0 o = s.count(1) pa = 1 ma = 0 for i in range(o): m += s.count(i + 1) p.append(m) if o != 0: for x in range(1, o + 1): for g in range(2, x + 1): if p[g - 2] < x + g - 2: pa = 0 break elif p[g - 1] < x + g - 1: pa = 0 break if pa == 1: ma = x print(ma) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
def evaluate(lis, k): for i in range(k): if lis[k - 1 + i] > i + 1: return False return True t = int(input()) for _ in range(t): n = int(input()) array = sorted(list(map(int, input().split()))) k_approx = (n + 1) // 2 + 1 alice_win = False while alice_win is False: k_approx -= 1 if k_approx == 0: alice_win = True print(0) break else: alice_win = evaluate(array, k_approx) if alice_win: print(k_approx) break
FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) alist = [int(x) for x in input().split()] alist.sort() d = 0 if alist == [1]: print(1) elif alist.count(1) == len(alist): print((n + 1) // 2) else: k = min(alist.count(1), n // 2 + 1) if k < 2: print(k) else: while d == 0: i = 0 new = alist while i <= k: i += 1 new = sorted([int(x) for x in new if x <= k - i + 1]) if len(new) > 2: new = new[1:] new.pop() elif alist.count(1) == 0: k -= 1 i = 2 * k elif k - i != 0: k -= 1 i = 2 * k else: print(k) d -= 1 i = 2 * k
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 LIST NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) axis = [0] * len(a) for i in a: axis[i - 1] += 1 k_max = 0 for k in range(axis[0], 0, -1): avail = axis[0] - k for i in range(1, k): if avail + axis[i] < 1: break else: avail += axis[i] - 1 else: k_max = k break print(k_max)
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 BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) if n == 1 or n == 2: if arr.count(1) > 0: print(1) else: print(0) continue arr.sort() for i in range(1, (n + 1) // 2 + 1): flag = False for j in range(1, i + 1): if arr[i + j - 2] > j: flag = True break if flag: i -= 1 break print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
def f(kk): ff = True for j in range(kk + 1): if c[j] <= kk + j: ff = False break return ff t = int(input()) for ii in range(t): n = int(input()) l = list(map(int, input().split())) l.sort() c = [] p = 0 for i in range(n): while p < n and l[p] <= i + 1: p += 1 c.append(p) k = 0 while f(k): k += 1 if k > n // 2: k = (n + 1) // 2 print(k)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
def analog(x): synA, synB = x, 1 for round in range(1, x + 1): synA = x - round + 1 while bck[synA] == 0 and synA > 1: synA -= 1 bck[synA] -= 1 if round < x: bck[1] -= 1 return [0, 1][bck[1] >= 0] for dm1 in range(int(input())): n = int(input()) nmbs = sorted([int(x) for x in input().split()]) bckk = [0] * 102 for i in nmbs: bckk[i] += 1 lo, hi = 0, 100 if bckk[1] in {0, 1}: print(bckk[1]) else: while lo < hi: bck = bckk[:] mid = (lo + hi) // 2 if analog(mid) == 1: lo = mid + 1 else: hi = mid print(lo - 1)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER RETURN LIST NUMBER NUMBER VAR NUMBER NUMBER 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
import sys t = int(sys.stdin.readline()) def check(k, n): ci = 0 i = n - 1 ji = 0 j = 0 while j <= i: if a[i] <= k - ji: ji += 1 ci += 1 j += 1 i -= 1 return ci for _ in range(t): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) if n == 1 and a == [1]: print(1) else: l = 0 r = n a.sort() while l <= r: mid = (l + r) // 2 if check(mid, n) >= mid: l = mid + 1 else: r = mid - 1 print(min(l, mid, r))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) lis = [int(x) for x in input().split(" ")] lis.sort() num = lis.count(1) s = min(num, n // 2 + n % 2) flag = False for i in range(1, s + 1): list1 = lis[i - 1 : 2 * i - 1] for j in range(i): if list1[j] > j + 1: flag = True break if flag: s = i - 1 break print(s)
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 STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for num in range(t): n = int(input()) bucket = [0] * (n + 2) a = [int(i) for i in input().split()] for i in a: bucket[i] += 1 if bucket[1] == 0: print(0) continue if bucket[1] == 1: print(1) continue k = 2 flag = [] flag.append(1) while k <= n: if bucket[k] == 0: bucket[flag[-1]] -= 1 if bucket[flag[-1]] == 1: z = flag.pop() bucket[k] = 1 if bucket[1] < k: break if bucket[1] < k: break if bucket[k] >= 2: flag.append(k) k += 1 print(k - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR IF VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) x = input() y = str(x).split() y = [int(i) for i in y] y.sort() b = n for e in range(n): if y[e] > 1: b = e break for k in range(b, -1, -1): yy = [i for i in y] v = 0 for i in range(k): a = k - i if yy[0] > a: v += 1 break else: if yy[-1] <= a: del yy[-1] else: for d in range(1, len(yy)): if yy[d] > a: del yy[d - 1] break if len(yy) > 0: c = yy[0] + a del yy[0] yy.append(c) if v == 0: print(k) break
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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
def check(arr, t): j = 0 temp = t count = 0 for i in range(len(arr) - 1, -1, -1): if arr[i] <= t: arr[i] = 0 arr[j] += t j += 1 t -= 1 count += 1 if count == temp: return True return False def ans(arr): start = 0 end = 10 * 5 arr.sort() current = 0 while start <= end: temp = arr.copy() mid = (start + end) // 2 if check(temp, mid) is True: current = mid start = mid + 1 else: end = mid - 1 return current test_cases = int(input()) while test_cases != 0: d = input() d2 = list(map(int, input().split())) print(ans(d2)) test_cases -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN 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 NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
n = int(input()) for i in range(n): x = int(input()) lst = list(map(int, input().split())) lst.sort() for k in range(int((x + 1) // 2), -1, -1): s = 0 for j in range(k): if lst[k - 1 + j] <= j + 1: s += 1 if s == k: print(k) break
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer — the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) l = list(map(int, input().split())) l.sort() if l.count(1) == 0: print(0) continue c = min(l.count(1), (n + 1) // 2) l.remove(1) for j in range(1, c + 1): if l.count(1) == 0: print(j) break l.remove(1) d = [y for y in l if y <= j + 1] if d == []: print(j) break else: l.remove(max(d))
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 NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR