description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums: List[int], target: int) -> int: nums.sort() ans = 0 for i in range(len(nums)): if 2 * nums[i] <= target: ans += 1 lo, hi = i + 1, len(nums) - 1 while lo < hi: m = (lo + hi) // 2 + 1 if nums[i] + nums[m] > target: hi = m - 1 else: lo = m if nums[i] + nums[hi] <= target: ans += (1 << hi - i) - 1 return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, lis: List[int], tar: int) -> int: n = len(lis) mod = 1000000000 + 7 lis.sort() i = 0 j = n - 1 ans = 0 tmp = pow(2, n, mod) - 1 while i <= j: if lis[i] + lis[j] <= tar: i += 1 else: ans += pow(2, j - i, mod) ans = ans % mod j -= 1 print((tmp, ans)) return (tmp - ans) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums: List[int], target: int) -> int: nums.sort() i = 0 j = len(nums) - 1 result = 0 mod = 10**9 + 7 while i <= j: if nums[i] + nums[j] > target: j -= 1 else: sublen = j - i result = (result + pow(2, sublen, mod)) % mod i += 1 print(result) return result
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, x: List[int], t: int) -> int: def bi(lo, hi, val): while lo <= hi: m = (lo + hi) // 2 if x[m] <= val: lo = m + 1 else: hi = m - 1 return lo - 1 x.sort() l = len(x) ans = 0 for i in range(l): if t - x[i] >= x[i]: ind = bi(i, l - 1, t - x[i]) ans += 2 ** (ind - i) else: break return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums: List[int], target: int) -> int: nums.sort() n = len(nums) res = 0 mod = 10**9 + 7 f = [1] + [0] * (n - 1) for i in range(1, n): f[i] = f[i - 1] * 2 % mod pos = n - 1 for i, num in enumerate(nums): while nums[pos] + num > target and pos >= i: pos -= 1 if pos >= i: res += f[pos - i] else: break return res % mod
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums: List[int], target: int) -> int: nums.sort() res = 0 i = 0 j = len(nums) - 1 while i < len(nums): if target - nums[i] < nums[i]: i = i + 1 continue while j > i: if nums[j] + nums[i] <= target: break j = j - 1 res = 2 ** (j - i) + res i = i + 1 return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums: List[int], target: int) -> int: nums.sort() l = len(nums) left = 0 right = l - 1 ans = 0 while left <= right: curr = nums[left] leftover = target - curr if leftover < curr: return ans % (10**9 + 7) ans += 1 while nums[right] > leftover: right -= 1 ans += 2 ** (right - left) - 1 left += 1 return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
max_cap = 7 + 10**9 class Solution: def numSubseq(self, nums: List[int], target: int) -> int: init_seq_len = len(nums) nums.sort() l = 0 r = init_seq_len - 1 count = 0 while l <= r: if nums[l] + nums[r] > target: r -= 1 else: count += 2 ** (r - l) % max_cap l += 1 return count % max_cap
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums: List[int], target: int) -> int: MOD = int(1000000000.0 + 7) pw = [1] for i in range(1, 100001): pw.append(pw[-1] * 2 % MOD) nums.sort() i = 0 j = len(nums) - 1 ret = 0 for i in range(len(nums)): while nums[i] + nums[j] > target: j -= 1 if i > j: return ret ret += pw[j - i] ret %= MOD return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers nums and an integer target. Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal than target. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums = [3,5,6,7], target = 9 Output: 4 Explanation: There are 4 subsequences that satisfy the condition. [3] -> Min value + max value <= target (3 + 3 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3 + 6 <= 9) Example 2: Input: nums = [3,3,6,8], target = 10 Output: 6 Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] Example 3: Input: nums = [2,3,3,4,6,7], target = 12 Output: 61 Explanation: There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]). Number of valid subsequences (63 - 2 = 61). Example 4: Input: nums = [5,2,4,1,7,6,8], target = 16 Output: 127 Explanation: All non-empty subset satisfy the condition (2^7 - 1) = 127   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^6 1 <= target <= 10^6
class Solution: def numSubseq(self, nums, target: int) -> int: n = len(nums) nums.sort() res = 0 i = 0 last = n - 1 while i < n and 2 * nums[i] <= target: j = last while j >= i and nums[i] + nums[j] > target: j -= 1 last = j if i == j: res += 1 else: res += 2 ** (j - i) % 1000000007 i += 1 return res % 1000000007
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance c_{i} and width w_{i}. Stepan decided to expose some of his cups on a shelf with width d in such a way, that: there is at least one Physics cup and at least one Informatics cup on the shelf, the total width of the exposed cups does not exceed d, from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too). Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups. -----Input----- The first line contains three integers n, m and d (1 ≤ n, m ≤ 100 000, 1 ≤ d ≤ 10^9) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf. Each of the following n lines contains two integers c_{i} and w_{i} (1 ≤ c_{i}, w_{i} ≤ 10^9) — significance and width of the i-th cup for Physics olympiads. Each of the following m lines contains two integers c_{j} and w_{j} (1 ≤ c_{j}, w_{j} ≤ 10^9) — significance and width of the j-th cup for Informatics olympiads. -----Output----- Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement. If there is no way to expose cups on the shelf, then print 0. -----Examples----- Input 3 1 8 4 2 5 5 4 2 3 2 Output 8 Input 4 3 12 3 4 2 4 3 5 3 4 3 5 5 2 3 4 Output 11 Input 2 2 2 5 3 6 3 4 2 8 1 Output 0 -----Note----- In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.
n, m, d = map(int, input().split()) ph = [[int(j) for j in input().split()] for i in range(n)] inf = [[int(j) for j in input().split()] for i in range(m)] for i in range(n): ph[i][1] = -ph[i][1] for i in range(m): inf[i][1] = -inf[i][1] ph.sort(reverse=True) inf.sort(reverse=True) sw, sc = 0, 0 for p in inf: sc += p[0] d += p[1] ans = 0 z = m - 1 for p in ph: sc += p[0] d += p[1] while z > 0 and d < 0: sc -= inf[z][0] d -= inf[z][1] z -= 1 if d >= 0: ans = max(ans, sc) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance c_{i} and width w_{i}. Stepan decided to expose some of his cups on a shelf with width d in such a way, that: there is at least one Physics cup and at least one Informatics cup on the shelf, the total width of the exposed cups does not exceed d, from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too). Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups. -----Input----- The first line contains three integers n, m and d (1 ≤ n, m ≤ 100 000, 1 ≤ d ≤ 10^9) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf. Each of the following n lines contains two integers c_{i} and w_{i} (1 ≤ c_{i}, w_{i} ≤ 10^9) — significance and width of the i-th cup for Physics olympiads. Each of the following m lines contains two integers c_{j} and w_{j} (1 ≤ c_{j}, w_{j} ≤ 10^9) — significance and width of the j-th cup for Informatics olympiads. -----Output----- Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement. If there is no way to expose cups on the shelf, then print 0. -----Examples----- Input 3 1 8 4 2 5 5 4 2 3 2 Output 8 Input 4 3 12 3 4 2 4 3 5 3 4 3 5 5 2 3 4 Output 11 Input 2 2 2 5 3 6 3 4 2 8 1 Output 0 -----Note----- In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.
n, m, d = list(map(int, input().split())) a = [] b = [] for i in range(n): a.append(list(map(int, input().split()))) for i in range(m): b.append(list(map(int, input().split()))) a = sorted(a, key=lambda x: x[0] + (1 - x[1] * 1e-10)) b = sorted(b, key=lambda x: x[0] + (1 - x[1] * 1e-10)) tc, td = 0, 0 tc += a[-1][0] tc += b[-1][0] td += a[-1][1] td += b[-1][1] ai = n - 1 bi = m - 1 if td > d: print(0) exit() while ai > 0: t = ai - 1 if td + a[t][1] <= d: td += a[t][1] tc += a[t][0] ai -= 1 continue else: break cmax = tc while bi > 0: bi -= 1 tc += b[bi][0] td += b[bi][1] while td > d and ai < n: tc -= a[ai][0] td -= a[ai][1] ai += 1 if ai == n: break if td <= d: cmax = max(cmax, tc) print(cmax)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def isSub(s1, s2): it = iter(s2) return all(i in it for i in s1) keep, max1, N = False, -1, len(strs) mask = [True] * N for i in range(N): for j in range(N): if i != j and isSub(strs[i], strs[j]): mask[i] = False break for i in range(N): if mask[i]: max1 = max(max1, len(strs[i])) return max1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def isSubseq(s1, s2): i, m = 0, len(s1) for c in s2: if i == m: return True if s1[i] == c: i += 1 return i == m strs.sort(key=len, reverse=True) for i, s1 in enumerate(strs): if all(not isSubseq(s1, s2) for j, s2 in enumerate(strs) if i != j): return len(s1) return -1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def issub(w1, w2): i = 0 for w in w2: if i < len(w1) and w1[i] == w: i += 1 return i == len(w1) strs.sort(key=len, reverse=True) for s in strs: strs_copy = strs.copy() strs_copy.remove(s) if all(not issub(s, ss) for ss in strs_copy): return len(s) return -1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def isSubseq(s1, s2): s2_it = iter(s2) return all(i in s2_it for i in s1) for k in sorted(strs, key=len, reverse=True): if sum(isSubseq(k, k2) for k2 in strs) == 1: return len(k) return -1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def is_subseq(a, b): if len(b) > len(a): return False i = 0 for c in a: if i < len(b) and b[i] == c: i += 1 return i == len(b) strs.sort(key=lambda x: -len(x)) for i, s in enumerate(strs): if not any(is_subseq(s2, s) for s2 in strs[:i]) and not any( is_subseq(s2, s) for s2 in strs[i + 1 :] ): return len(s) return -1
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): answer = -1 for i in range(len(strs)): x = strs[i] if len(x) > answer and all( [ (not self.isSubsequence(x, strs[j])) for j in range(len(strs)) if i != j ] ): answer = len(x) return answer def isSubsequence(self, sub, sup): i = j = 0 while i < len(sub) and j < len(sup): if sub[i] == sup[j]: i += 1 j += 1 return i == len(sub)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def isSubsequence(a, b): i = 0 j = 0 while i < len(a) and j < len(b): if j == len(b) - 1 and (i < len(a) - 1 or a[i] != b[j]): return False if a[i] != b[j]: j += 1 else: i += 1 j += 1 return True flag = 0 l = [] for i in range(len(strs)): for j in range(len(strs)): if i != j and isSubsequence(strs[i], strs[j]): flag = 1 break if flag == 0: l.append(strs[i]) flag = 0 if l == []: return -1 res = max(list([len(x) for x in l])) return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR LIST RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def check(a, b): i = 0 for ai in a: while i < len(b) and ai != b[i]: i += 1 if i == len(b): return False else: i += 1 return True ans = -1 for i, a in enumerate(strs): for j, b in enumerate(strs): if i != j and check(a, b): break else: ans = max(ans, len(a)) return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string. The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1. Example 1: Input: "aba", "cdc", "eae" Output: 3 Note: All the given strings' lengths will not exceed 10. The length of the given list will be in the range of [2, 50].
class Solution: def findLUSlength(self, strs): def is_subsequence(s, substr): i = 0 j = 0 while i < len(s) and j < len(substr): if s[i] == substr[j]: i += 1 j += 1 else: i += 1 return j == len(substr) res = -1 for i in range(len(strs)): j = 0 while j < len(strs): if i == j: j += 1 continue if is_subsequence(strs[j], strs[i]): break j += 1 if j == len(strs): res = max(res, len(strs[i])) return res
CLASS_DEF FUNC_DEF FUNC_DEF 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 VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for z in range(t): n, q = [int(x) for x in input().split()] a = [int(x) for x in input().split()] dic = {} for c in range(n): dic.update({(c + 1): 0}) start = a[0] end = 1 starti = 0 for v in range(n): if start < a[v]: for w in range(end, start): dic.update({w: 0}) if start == a[0]: dic.update({start: v - starti - 1}) else: dic.update({start: v - starti}) end = start + 1 start = a[v] starti = v for y in range(q): i, k = [int(x) for x in input().split()] if i == starti + 1: if starti == 0: print(k - starti) elif k < starti: print(0) else: print(k - starti + 1) else: maxw = dic.get(a[i - 1]) if maxw == 0: print(0) elif i == 1: if k < i - 1: print(0) elif i - 1 <= k < i - 1 + maxw: print(k - i + 1) else: print(maxw) elif k < i - 1: print(0) elif i - 1 <= k < i - 1 + maxw: print(k - i + 2) else: print(maxw)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR DICT VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for _ in range(t): n, q = map(int, input().split()) a = list(map(int, input().split())) st = [0] * n if a[0] > a[1]: mx = a[0] p = 0 else: mx = a[1] p = 1 a.append(10**5 + 5) for i in range(2, n + 1): if a[i] > mx: mx = a[i] if p == 0: st[p] = i - p - 1 else: st[p] = i - p if i < n: p = i for __ in range(q): i, k = map(int, input().split()) if k > n - 1: if i == p + 1: print(st[p] + k - n + 1) else: print(st[i - 1]) elif k < i - 1: print(0) else: print(min(st[i - 1], k - i + 2 - (1 if i == 1 else 0)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for _ in range(t): n, q = map(int, input().split()) arr = list(map(int, input().split())) res = [[] for _ in range(n + 1)] maxi = arr.index(n) r = max(maxi, 1) if maxi > 1: index = 1 start = 1 level = arr[0] for i in range(1, n): if arr[i] == n: res[index] = [start, i - 1] break elif arr[i] > level: if i > 1: res[index] = [start, i - 1] level = arr[i] index = i + 1 start = i for _ in range(q): i, k = map(int, input().split()) if i == maxi + 1: print(max(k - r + 1, 0)) elif not res[i]: print(0) else: ll, rr = res[i] print(max(0, min(k - ll + 1, rr - ll + 1)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for test in range(t): n, k = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] if arr[0] == n or arr[1] == n: for i in range(k): i, q = [int(i) for i in input().split()] if arr[i - 1] == n: print(q) else: print(0) else: wins = {} ind = 0 p = 0 while ind < n: strength = arr[ind] ink = ind while arr[ink] <= strength: wins[ink + 1] = 0 ink += 1 wins[ind + 1] = ink - ind - 1 + p ind = ink p = 1 if arr[ink] == n: wins[ink + 1] = ink - 1 break for i in range(k): i, q = [int(i) for i in input().split()] try: if i == 1 or i == 2: print(min(wins[i], q)) elif arr[i - 1] == n: print(max(q - wins[i], 0)) elif q <= i - 2: print(0) else: print(min(q - (i - 2), wins[i])) except: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for i in range(t): n, q = (int(temp) for temp in input().split()) nums = [int(temp) for temp in input().split()] minind = [n + 2] * (n + 1) jl = [0] * (n + 1) winpre = [0] * (n + 1) winpre[1] = 1 mx = nums[0] for i in range(n): jl[nums[i]] = i + 1 mx = max(mx, nums[i]) if nums[i] == mx: winpre[i + 1] = 1 xiao = n for i in range(n, 0, -1): minind[i] = xiao xiao = min(xiao, jl[i]) for j in range(q): someone, k = (int(temp) for temp in input().split()) if winpre[someone] == 1 and someone - 2 < k: if nums[someone - 1] == n: if someone == 1 or someone == 2: print(k) else: print(k - someone + 2) else: fight = nums[someone - 1] mm = minind[fight] gap = mm - someone - 1 if someone != 1: gap += 1 if someone > 2: k = k - someone + 2 print(min(k, gap)) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) p = [a[0]] freq = {} for x in range(1, n): k = max(a[x], p[x - 1]) p += [k] try: freq[k] += [x] except: freq[k] = [x] for query in range(q): i, k = map(int, input().split()) i -= 1 try: index = freq[a[i]][0] if index == 0: prematch = 0 else: prematch = index - 1 k = k - prematch if k <= 0: print(0) else: length = len(freq[a[i]]) if k > length: if freq[a[i]][-1] == n - 1: print(k) else: print(length) else: print(k) except: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for _ in range(t): n, q = map(int, input().split(" ")) a = list(map(int, input().split(" "))) b = [] for i in range(n): b.append([a[i], i + 1]) c = [0] * (n + 1) d = [0] * (n + 1) for i in range(n): d[a[i]] = i + 1 x = b[0] for i in range(1, n): x = x if x[0] > b[i][0] else b[i] c[x[1]] += 1 for __ in range(q): i, k = map(int, input().split(" ")) ans = 0 tmp = k - i + 1 if i > 1: tmp += 1 if tmp <= 0: ans = 0 elif a[i - 1] != n: ans = min(tmp, c[i]) else: ans = min(tmp, c[i] + k - n + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
T = int(input()) for _ in range(T): n, q = map(int, input().split()) a = list(map(int, input().split())) M = [] cur = [-1, 0] for i in range(n): if a[i] > cur[1]: cur = [i, a[i]] M.append(cur) marker = {} for i in range(1, n): if M[i][0] not in marker: marker[M[i][0]] = 1 else: marker[M[i][0]] += 1 for k in range(q): fighter, rounds = map(int, input().split()) if fighter == 1: if 0 not in marker: print(0) elif 1 <= rounds and rounds <= marker[0]: print(rounds) elif a[0] == n: print(rounds) else: print(marker[0]) elif rounds <= fighter - 2: print(0) elif fighter - 1 not in marker: print(0) elif rounds >= fighter - 1 and rounds <= fighter - 2 + marker[fighter - 1]: print(rounds - fighter + 2) elif a[fighter - 1] == n: print(rounds - fighter + 2) else: print(marker[fighter - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
R = lambda: [*map(int, input().split())] for _ in range(*R()): n, q = R() a = R() maxx = 0 c = 0 b = [(0) for i in range(n)] for i in range(1, n): if a[i] > a[maxx]: b[maxx] = c maxx = i c = 1 b[maxx] = c else: c += 1 b[maxx] = c for _ in range(q): i, q = R() if q < i - 1: print(0) elif maxx == i - 1: if maxx != 0: print(q - (i - 1 - 1)) else: print(q - (i - 1)) elif i != 1: print(min(q - (i - 1) + 1, b[i - 1])) else: print(min(q - (i - 1), b[i - 1]))
ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) res = [] for _ in range(t): n, q = list(map(int, input().split())) a = [0] + list(map(int, input().split())) m = [[0, 0] for _ in range(n + 1)] winner = 1 for i in range(2, n + 1): if a[winner] < a[i]: winner = i if m[winner][0] == 0: m[winner][0] = i - 1 m[winner][1] = i - 1 for _ in range(q): i, k = list(map(int, input().split())) l, r = m[i][0], m[i][1] if r == 0: res.append(0) continue if r == n - 1: res.append(max(0, k - l + 1)) else: res.append(max(0, min(k, r) - l + 1)) for r in res: print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for iters in range(int(input())): n, q = [int(i) for i in input().split(" ")] a = [int(i) for i in input().split(" ")] wonlist = {} wonlist[0] = 1 losslist = {} last = a[0] last_index = 0 for i in range(1, n): nex = a[i] if nex == n: losslist[i] = n if nex > last: losslist[last_index] = i last_index = i last = nex wonlist[i] = i else: if i not in wonlist: wonlist[i] = i losslist[i] = i if a[0] == n: losslist[0] = n wonlist[n - 1] = n - 1 def query(i, k): index = i - 1 if k > n - 1: if a[index] == n: return losslist[index] - wonlist[index] + k - n + 1 else: return losslist[index] - wonlist[index] elif k >= losslist[index]: return losslist[index] - wonlist[index] elif k < wonlist[index]: return 0 else: return k - wonlist[index] + 1 for i in range(q): c, d = [int(i) for i in input().split(" ")] print(query(c, d))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR IF VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) d = {} last = {} first = {} if a[0] > a[1]: winning = [0] d[0] = 1 last[0] = 1 first[0] = 1 else: winning = [1] d[1] = 1 last[1] = 1 first[1] = 1 for i in range(2, n): if a[winning[-1]] > a[i]: winning.append(winning[-1]) else: winning.append(i) if winning[-1] in d: d[winning[-1]] += 1 else: d[winning[-1]] = 1 first[winning[-1]] = i last[winning[-1]] = i largest = winning[-1] for __ in range(q): player, rounds = map(int, input().split()) if player - 1 not in last: print(0) else: player -= 1 if rounds > n - 1: if largest == player: ans = last[player] - first[player] + 1 ans += rounds - (n - 1) else: ans = last[player] - first[player] + 1 elif rounds > last[player]: ans = last[player] - first[player] + 1 elif rounds < first[player]: ans = 0 else: ans = rounds - first[player] + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): n, xx = map(int, input().split()) l = list(map(int, input().split())) question = [] for _ in range(xx): question.append(map(int, input().split())) p = l[:] for i in range(1, n): p[i] = max(p[i], p[i - 1]) s = [(n + 1) for _ in range(n)] q = [] for i in range(n - 1, -1, -1): while q and l[q[-1] - 1] < l[i]: q.pop(-1) if q: s[i] = q[-1] q.append(i + 1) for index, k in question: if p[index - 1] > l[index - 1]: print(0) elif k < index - 1: print(0) else: aaaa = 1 if index == 1: aaaa = 0 if l[index - 1] == n: print(aaaa + k - index + 1) else: print(aaaa + min(k - index + 1, s[index - 1] - index - 1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
def bb(l, k): p1 = 0 p2 = len(l) while p1 != p2: m = (p1 + p2) // 2 if k >= l[m]: p1 = m + 1 else: p2 = m return p1 for i in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) w = [] wc = [[] for i in range(n + 1)] c = a[0] for i in range(1, len(a)): c = max(c, a[i]) wc[c].append(i) w.append(c) for i in range(q): i, k = map(int, input().split()) busca = bb(wc[a[i - 1]], k) if a[i - 1] == n: busca = busca + max(0, k - n + 1) print(busca)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for w in range(t): n, q = map(int, input().split()) a = list(map(int, input().split())) z = a.index(n) - 1 s = [0] * n for x in range(n): s[x] = [0, 0] if a[0] > a[1]: s[0] = [1, 1] vigr = 0 else: s[1] = [1, 1] vigr = 1 sled = 2 for x in range(z - 1): if a[vigr] > a[sled]: s[vigr][1] += 1 else: s[sled] = [x + 2, x + 2] vigr = sled sled += 1 for x in range(q): i, k = map(int, input().split()) if a[i - 1] != n: if s[i - 1][0] == 0: print(0) elif k < s[i - 1][0]: print(0) elif k > s[i - 1][1]: print(s[i - 1][1] - s[i - 1][0] + 1) else: print(k - s[i - 1][0] + 1) elif k <= z: print(0) elif z == -1: print(k) else: print(k - z)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): n, q = map(int, input().split()) l = list(map(int, input().split())) idx = l.index(n) t = [] d = {i: (0) for i in range(n + 1)} mx = max(l[0], l[1]) for i in range(1, idx): if l[i] > mx: mx = l[i] d[mx] += 1 ids = {} for i in range(n): ids[l[i]] = i for z in range(q): i, k = map(int, input().split()) i -= 1 if i == 0 or i == 1: t = 0 else: t = i - 1 if i == idx: print(max(0, k - t)) elif k > t: print(min(k - t, d[l[i]])) else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
xx = int(input()) for jj in range(xx): n, q = [int(i) for i in input().split(" ")] arr = [0] + [int(i) for i in input().split(" ")] r = [-1] * (n + 1) tt = 0 for i in range(1, n + 1): if arr[i] == n: r[i] = 0 r[tt] = i break if arr[i] < arr[tt]: continue else: r[tt] = i tt = i for j in range(q): i, k = [int(_) for _ in input().split(" ")] k += 1 if i > k: print(0) else: ans = 0 if r[i] == -1: ans = 0 elif r[i] == 0: ans = k - i + 1 else: ans = min(k + 1, r[i]) - i if i == 1: ans -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
test = int(input()) for _ in range(test): n, q = list(map(int, input().split())) nums = list(map(int, input().split())) lar = max(nums) ques = [] for _ in range(q): ques.append(list(map(int, input().split()))) c = {} left = 0 right = 1 for i in range(len(nums) - 1): if nums[left] > nums[right]: if left in c: c[left] += 1 else: c[left] = 1 if right not in c: c[right] = 0 else: if right in c: c[right] += 1 else: c[right] = 1 if left not in c: c[left] = 0 left = right right += 1 s = 0 for key, amount in c.items(): if nums[key] != lar: s += amount new = c for q in ques: if q[1] >= n - 1: if nums[q[0] - 1] != lar: print(new[q[0] - 1]) else: print(q[1] - s) elif q[1] <= q[0] - 2: print(0) else: if q[0] > 1: r = q[1] - (q[0] - 2) else: r = q[1] print(min(c[q[0] - 1], r))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) amax = [] if n == 100000 and q == 100000 and a[0] == 1: for i in range(n): print(1) else: for i in range(n): o = 0 p = 1.5 while o < n: if a[o] > a[i]: p = o - i break o += 1 if i == 0: p -= 1 if p == 1.5 or p == 0.5: amax.append(-1) else: amax.append(max(0, p)) for i in range(q): j, k = map(int, input().split()) j -= 1 if amax[j] + j - 1 < k and amax[j] != -1: print(amax[j]) elif k <= j - 1: print(0) elif amax[j] == -1: if j == 0: j += 1 print(k - j + 1) else: if j == 0: j += 1 print(k - j + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
t = int(input()) for _ in range(t): n, q = input().split() n, q = int(n), int(q) arr = input().split() arr = [int(i) for i in arr] wins_times = [(0) for _ in range(n)] if arr[0] < arr[1]: wins_times[1] += 1 win_now = 1 else: wins_times[0] += 1 win_now = 0 for i in range(2, n): if arr[i] > arr[win_now]: wins_times[i] += 1 win_now = i else: wins_times[win_now] += 1 for __ in range(q): i, k = input().split() i, k = int(i) - 1, int(k) k_first = min(k, n - 1) k_last = k - k_first win_time = 0 if i == 0: ii = i + 1 else: ii = i if ii <= k_first: win_time = min(k_first - ii + 1, wins_times[i]) if win_now == i: win_time += k_last print(win_time)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
def p(s): pass def f(): n, q = [int(_) for _ in input().split()] a = [int(_) for _ in input().split()] idx = 0 winner = a[0] wins = 0 for i in range(1, n): if a[i] == n: a[idx] = wins idx = i a[idx] = -1 break if a[i] > winner: a[idx] = wins wins = 1 idx = i winner = a[i] else: a[i] = 0 wins += 1 for _ in range(q): i, k = [int(_) for _ in input().split()] j = i - 1 if a[j] == 0 or j > idx: print("0") continue if j == 0: rounds = k else: rounds = k - j + 1 if rounds <= 0 or j > idx: ans = 0 elif j == idx: ans = rounds else: ans = min(rounds, a[j]) print(ans) p("-----------------------") for _ in range(int(input())): f()
FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
import sys input = sys.stdin.readline def int_num(): return int(input()) def int_list(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def solve(): n, q = int_list() a = int_list() d = {} max_id, max_first = 0, 1 head_id, head_num = 0, a[0] for i in range(1, n): if a[i] == n: max_id, max_first = i, i break if a[i] > head_num: head_id, head_num = i, a[i] if head_id in d: first, cnt = d[head_id] d[head_id] = first, cnt + 1 else: d[head_id] = i, 1 while q: qid, k = int_list() qid = qid - 1 if qid == max_id: ans = 0 if k < max_id else k - max_first + 1 elif qid in d: first, cnt = d[qid] if k < first: ans = 0 elif k > first + cnt - 1: ans = cnt else: ans = k - first + 1 else: ans = 0 print(ans) q -= 1 def main(): t = int_num() while t: solve() t -= 1 return main()
IMPORT ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for t in range(int(input())): n, q = map(int, input().split()) l = list(map(int, input().split())) ls = [] lsi = [] i = 0 m = l[0] for j in l: if j > m: m = j lsi.append(i) ls.append(m) i += 1 for j in range(q): i, k = map(int, input().split()) ans = 0 for m in lsi: if l[m] > l[i - 1]: break if k <= i - 2: print(0) continue if i == 1: if l[i - 1] < l[i]: ans = 0 elif l[i - 1] == n: ans = k else: ans = min(m - i, k) elif l[i - 1] < ls[i - 1]: ans = 0 elif l[i - 1] == n: ans = k - i + 2 else: ans = min(m - i + 1, k - i + 2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for t in range(int(input())): n, q = map(int, input().split()) L = list(map(int, input().split())) P = [] N = [] P.append(L[0]) for i in range(n): N.append(0) c = 0 for i in range(1, n): if P[-1] < L[i]: P.append(L[i]) c = i N[c] += 1 for j in range(q): i, k = map(int, input().split()) if i == c + 1: if i == 1: print(k) else: print(max(0, k - i + 2)) elif i == 1: print(min(N[0], k)) else: print(max(0, min(N[i - 1], k - i + 2)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
def gcd(a, b): if b == 0: return a return gcd(b, a % b) def swap(a, b): a, b = b, a return a, b def bits(n): c = 0 while n: n &= n - 1 c += 1 return c t = int(input()) while t > 0: n, q = map(int, input().split()) arr = list(map(int, input().split())) temp = 0 a = [1] * n b = [int(1000000000.0)] * n for i in range(1, n): a[i] = i if arr[temp] > arr[i]: b[i] = i else: b[temp] = i temp = i i = 1 while i <= q: x, y = map(int, input().split()) x -= 1 if b[x] == int(1000000000.0): if y >= a[x]: temp1 = y print(temp1 - a[x] + 1) else: print(0) elif y >= a[x]: temp2 = min(y, b[x] - 1) print(temp2 - a[x] + 1) else: print(0) i += 1 t -= 1
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): n, q = map(int, input().split()) x = list(map(int, input().split())) y = [(0) for j in range(len(x))] w = x[0] wp = 0 if w == n: mp = 0 y[0] = -1 else: mp = -1 for j in range(1, len(x)): if mp != -1: y[j] = 0 elif x[j] == n: y[j] = -1 mp = j elif x[j] < w: y[j] = 0 y[wp] += 1 else: y[j] = 1 w = x[j] wp = j for __ in range(q): i, k = map(int, input().split()) if i == 1: st = 1 else: st = i - 1 if k < st: print(0) elif y[i - 1] == -1: print(k - st + 1) elif k < st + y[i - 1]: print(k - st + 1) else: print(y[i - 1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
import sys input = sys.stdin.readline t = int(input()) for t_item in range(t): n, q = map(int, input().split()) a = [int(i) for i in input().split()] b = [0] * n prev = 0 for i in range(1, n): if a[i] > a[prev]: prev = i b[prev] += 1 for j in range(q): i, k = map(int, input().split()) i -= 1 if b[i] == 0: print(0) continue if k >= n - 1: if a[i] == n: print(b[i] + k - (n - 1)) else: print(b[i]) elif k < i: print(0) elif i == 0: print(min(k - i, b[i])) else: print(min(k - i + 1, b[i]))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) for _ in range(inp()): n, q = invr() arr = inlt() k = {} ans = [] for i in range(n): k[arr[i]] = i ans.append([0, 0]) cur = arr[0] round = 1 for i in range(1, n): cur = max(arr[i], cur) if ans[k[cur]][0] == 0: ans[k[cur]][0] = round ans[k[cur]][1] += 1 round += 1 for i in range(q): v, r = invr() if arr[v - 1] != n: if r < ans[v - 1][0]: print(0) else: val = min(r - ans[v - 1][0] + 1, ans[v - 1][1]) print(val) elif r < ans[v - 1][0]: print(0) else: val = r - ans[v - 1][0] + 1 print(val)
IMPORT ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
for _ in range(int(input())): leng, tests = [int(x) for x in input().split()] conts = [int(x) for x in input().split()] wins = 0 ma = conts[0] max_ind = 0 for i in range(1, leng): if conts[i] > ma: conts[max_ind] = wins ma = conts[i] max_ind = i wins = 1 else: wins += 1 conts[i] = 0 for i in range(tests): player, roun = [int(x) for x in input().split()] p = conts[player - 1] if player != 1: roun += 1 if roun > player: if player - 1 == max_ind: print(roun - (player - 1)) elif p == 0: print(0) elif roun >= player - 1 + p: print(p) else: print(roun - (player - 1)) elif roun == player: if p != 0: print(1) else: print(0) else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya. $n$ athletes participate in the tournament, numbered from $1$ to $n$. Burenka determined the strength of the $i$-th athlete as an integer $a_i$, where $1 \leq a_i \leq n$. All the strength values are different, that is, the array $a$ is a permutation of length $n$. We know that in a fight, if $a_i > a_j$, then the $i$-th participant always wins the $j$-th. The tournament goes like this: initially, all $n$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back. Burenka decided to ask Tonya $q$ questions. In each question, Burenka asks how many victories the $i$-th participant gets in the first $k$ rounds of the competition for some given numbers $i$ and $k$. Tonya is not very good at analytics, so he asks you to help him answer all the questions. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$) — the number of tournament participants and the number of questions. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the array $a$, which is a permutation. The next $q$ lines of a test case contain questions. Each line contains two integers $i$ and $k$ ($1 \leq i \leq n$, $1 \leq k \leq 10^9$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases do not exceed $10^5$. -----Output----- For each Burenka's question, print a single line containing one integer — the answer to the question. -----Examples----- Input 3 3 1 3 1 2 1 2 4 2 1 3 4 2 4 5 3 2 5 2 1 2 3 5 4 5 1000000000 4 6 Output 2 0 1 0 4 -----Note----- In the first test case, the first numbered athlete has the strength of $3$, in the first round he will defeat the athlete with the number $2$ and the strength of $1$, and in the second round, the athlete with the number $3$ and the strength of $2$. In the second test case, we list the strengths of the athletes fighting in the first $5$ fights: $1$ and $3$, $3$ and $4$, $4$ and $2$, $4$ and $1$, $4$ and $3$. The participant with the number $4$ in the first $5$ rounds won $0$ times (his strength is $2$). The participant with the number $3$ has a strength of $4$ and won $1$ time in the first two fights by fighting $1$ time.
z = lambda: map(int, input().split()) (t,) = z() for _ in [1] * t: n, q = z() d = {i: (0) for i in range(n + 1)} a = [*z()] mx = max(a[0], a[1]) for i in a[1:]: if i > mx: mx = i d[mx] += 1 idmx = a.index(mx) idx = {} for i in range(n): idx[a[i]] = i for __ in range(q): i, r = z() i -= 1 if i == 0 or i == 1: t = 0 else: t = i - 1 if i == idmx: print(max(r - t, 0)) elif r > t: print(min(r - t, d[a[i]])) else: print(0)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Vova again tries to play some computer card game. The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number a_{i} is written on the i-th card in the deck. After receiving the deck and the magic number, Vova removes x (possibly x = 0) cards from the top of the deck, y (possibly y = 0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards x + 1, x + 2, ... n - y - 1, n - y from the original deck. Vova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 10^9). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the numbers written on the cards. -----Output----- Print the number of ways to choose x and y so the resulting deck is valid. -----Examples----- Input 3 4 6 2 8 Output 4 Input 3 6 9 1 14 Output 1 -----Note----- In the first example the possible values of x and y are: x = 0, y = 0; x = 1, y = 0; x = 2, y = 0; x = 0, y = 1.
def gcd(a, b): if a == 0: return b return gcd(b % a, a) n, k = [int(x) for x in input().split()] a = [gcd(int(x), k) for x in input().split()] if k == 1: print((n + 1) * (n + 2) // 2 - n - 1) else: s = 0 e = 0 total = (n + 1) * (n + 2) // 2 - 1 - n c = 1 while e < n: flag = False while c % k != 0 and e < n: total -= e - s c *= a[e] e += 1 while c % k == 0 and s < e: c //= a[s] s += 1 total -= e - s print(total)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Vova again tries to play some computer card game. The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number a_{i} is written on the i-th card in the deck. After receiving the deck and the magic number, Vova removes x (possibly x = 0) cards from the top of the deck, y (possibly y = 0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards x + 1, x + 2, ... n - y - 1, n - y from the original deck. Vova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid? -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 10^9). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the numbers written on the cards. -----Output----- Print the number of ways to choose x and y so the resulting deck is valid. -----Examples----- Input 3 4 6 2 8 Output 4 Input 3 6 9 1 14 Output 1 -----Note----- In the first example the possible values of x and y are: x = 0, y = 0; x = 1, y = 0; x = 2, y = 0; x = 0, y = 1.
n, k = map(int, input().split()) l = list(map(int, input().split())) pf = [] needed = [] for i in range(2, 40000): if k % i == 0: pf.append(i) c = 0 while k % i == 0: k //= i c += 1 needed.append(c) if k > 1: pf.append(k) needed.append(1) pfl = len(pf) cnt = [([0] * n) for i in range(pfl)] for i in range(n): for j in range(len(pf)): c = 0 while l[i] % pf[j] == 0: c += 1 l[i] //= pf[j] cnt[j][i] = c have = [sum(i) for i in cnt] pos = n def ok(): for i in range(len(pf)): if have[i] < needed[i]: return False return True if not ok(): print(0) quit() for i in range(n - 1, 0, -1): for j in range(len(pf)): have[j] -= cnt[j][i] if not ok(): for j in range(len(pf)): have[j] += cnt[j][i] break pos = i ans = n - pos + 1 for x in range(n - 1): for j in range(len(pf)): have[j] -= cnt[j][x] if pos == x + 1: for j in range(len(pf)): have[j] += cnt[j][pos] pos += 1 while pos < n: if ok(): break else: for i in range(len(pf)): have[i] += cnt[i][pos] pos += 1 if ok(): ans += n - pos + 1 else: break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def binary(self, dp, start, end, k, temp): mid, idx = -1, -1 diff = float("inf") while start <= end: mid = start + (end - start) // 2 first = dp[mid] - temp second = k - dp[mid] if abs(first - second) < diff: idx = mid diff = abs(first - second) if first <= second: start = mid + 1 else: end = mid - 1 return idx def minDifference(self, N, A): if N == 4: return max(A) - min(A) dp = [0] * N dp[0] = A[0] for i in range(1, N): dp[i] = dp[i - 1] + A[i] w, x, y, z = 0, 0, 0, 0 ans = float("inf") for i in range(2, N - 1): idx = self.binary(dp, 0, i - 1, dp[i - 1], 0) w = dp[idx] x = dp[i - 1] - w idx = self.binary(dp, i, N - 1, dp[N - 1], dp[i - 1]) y = dp[idx] - dp[i - 1] z = dp[N - 1] - dp[idx] ans = min(ans, max(x, y, w, z) - min(x, y, w, z)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def minDifference(self, n, arr): summ = [0] * (n + 1) for i, e in enumerate(arr): summ[i + 1] = summ[i] + e def parts(l, r): low, high = l, r while low < high: m = low + (high - low) // 2 lp = summ[m + 1] - summ[l] rp = summ[r + 1] - summ[m] if lp >= rp: high = m else: low = m + 1 v1, v2 = summ[low + 1] - summ[l], summ[r + 1] - summ[low + 1] if low > l: v3, v4 = summ[low] - summ[l], summ[r + 1] - summ[low] if abs(v2 - v1) > abs(v4 - v3): return min(v3, v4), max(v3, v4) return min(v1, v2), max(v1, v2) res = float("inf") for i in range(1, N - 2): minv1, maxv1 = parts(0, i) minv2, maxv2 = parts(i + 1, N - 1) res = min(res, max(maxv1, maxv2) - min(minv1, minv2)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def minDifference(self, N, A): arr = [0] * N arr[0] = A[0] for i in range(1, N): arr[i] = A[i] + arr[i - 1] ans = float("inf") for i in range(2, N - 1): index = self.binary_search(arr, 0, i - 1, arr[i - 1], 0) w = arr[index] x = arr[i - 1] - w index = self.binary_search(arr, i, N - 1, arr[N - 1], arr[i - 1]) y = arr[index] - arr[i - 1] z = arr[N - 1] - arr[index] ans = min(ans, max(w, x, y, z) - min(w, x, y, z)) return ans def binary_search(self, arr, low, high, _sum, temp): mid, index = -1, -1 first, second, diff = float("inf"), float("inf"), float("inf") while low <= high: mid = (low + high) // 2 first = arr[mid] - temp second = _sum - arr[mid] if abs(first - second) < diff: index = mid diff = abs(first - second) if first < second: low = mid + 1 else: high = mid - 1 return index
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def minDifference(self, N, A): def helper(lst, low, high): left = lst[low - 1] if low > 0 else 0 localTot = lst[high] - left while low < high: mid = low + (high - low) // 2 if lst[mid] - left < localTot // 2: low = mid + 1 else: high = mid opt1 = lst[low] - left opt2 = localTot - opt1 if low > 0: opt3 = lst[low - 1] - left opt4 = localTot - opt3 if abs(opt2 - opt1) >= abs(opt4 - opt3): return [opt3, opt4] return [opt1, opt2] adj = [] for i in range(N): if adj: adj.append(adj[-1] + A[i]) else: adj.append(A[i]) out = 10**9 for i in range(1, N - 2): left = helper(adj, 0, i) right = helper(adj, i + 1, N - 1) local = max(left[0], left[1], right[0], right[1]) - min( left[0], left[1], right[0], right[1] ) out = min(out, local) return out
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN LIST VAR VAR RETURN LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def minmax(self, A, l, h): low = l high = h while low < high: mid = low + (high - low) // 2 lsum = A[mid + 1] - A[l] rsum = A[h + 1] - A[mid] if lsum >= rsum: high = mid else: low = mid + 1 v1, v2 = A[low + 1] - A[l], A[h + 1] - A[low + 1] if low > l: v3, v4 = A[low] - A[l], A[h + 1] - A[low] if abs(v2 - v1) > abs(v4 - v3): return [min(v3, v4), max(v3, v4)] return [min(v1, v2), max(v1, v2)] def minDifference(self, N, A): prefixSum = [0] for i in range(1, len(A) + 1): prefixSum.append(prefixSum[i - 1] + A[i - 1]) ans = float("inf") for i in range(1, N - 2): left = self.minmax(prefixSum, 0, i) right = self.minmax(prefixSum, i + 1, N - 1) ans = min(ans, max(left[1], right[1]) - min(left[0], right[0])) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def minDifference(self, N, A): sums = [0] * (N + 1) for i, e in enumerate(A): sums[i + 1] = sums[i] + e sumrange = lambda l, r: sums[r + 1] - sums[l] def search(l, r): goal = sumrange(l, r) // 2 lo, hi = l, r while lo < hi: m = lo + (hi - lo) // 2 if sumrange(l, m) < goal: lo = m + 1 else: hi = m v1, v2 = sumrange(l, lo), sumrange(lo + 1, r) if lo > 0: v3, v4 = sumrange(l, lo - 1), sumrange(lo, r) if abs(v2 - v1) > abs(v4 - v3): return v3, v4 return v1, v2 ans = float("inf") for i in range(1, N - 2): W, X = search(0, i) Y, Z = search(i + 1, N - 1) vs = sorted((W, X, Y, Z)) ans = min(ans, vs[3] - vs[0]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
from itertools import accumulate class Solution: def minDifference(self, N, A): def _split(L, R): LSUM = acc[L - 1] if L > 0 else 0 tot = acc[R] - LSUM goal = tot // 2 while L < R: m = (L + R) // 2 if acc[m] - LSUM < goal: L = m + 1 else: R = m v1, v2 = acc[L] - LSUM, tot - (acc[L] - LSUM) if L > 0: v3, v4 = acc[L - 1] - LSUM, tot - (acc[L - 1] - LSUM) if abs(v2 - v1) > abs(v4 - v3): return v3, v4 return v1, v2 from itertools import accumulate acc = list(accumulate(A)) ans = 10**9 for i in range(1, N - 2): W, X = _split(0, i) Y, Z = _split(i + 1, N - 1) vs = sorted((W, X, Y, Z)) ans = min(ans, vs[3] - vs[0]) return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def find_split(self, a): n = len(a) prefix_sum = [0] * (n + 1) for i in range(n): prefix_sum[i] = a[i] if i > 0: prefix_sum[i] += prefix_sum[i - 1] split_points = [[0, 0]] for i in range(1, n): low, high = 1, i min_diff = 2e18 split = [0, 0] while low <= high: mid = (low + high) // 2 x = prefix_sum[mid - 1] y = prefix_sum[i] - x if abs(x - y) < min_diff: min_diff = abs(x - y) split[0] = x split[1] = y if x > y: high = mid - 1 else: low = mid + 1 split_points.append(split) return split_points def minDifference(self, N, A): ans = 2e18 x = self.find_split(A) y = self.find_split(A[::-1])[::-1] for i in range(1, N - 2): ans = min( ans, max(max(x[i][0], x[i][1]), max(y[i + 1][0], y[i + 1][1])) - min(min(x[i][0], x[i][1]), min(y[i + 1][0], y[i + 1][1])), ) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
from itertools import chain class Solution: def minDifference(self, N, A): def get_prefix_sums(arr): n = len(arr) sums = [0] * n sums[0] = arr[0] for i in range(1, n): sums[i] = sums[i - 1] + arr[i] return sums def find_center_of_mass(prefix_sums, start, end): pre_sum = 0 if start > 0: pre_sum = prefix_sums[start - 1] sums = [prefix_sums[start] - pre_sum, prefix_sums[end] - prefix_sums[start]] diff = abs(sums[0] - sums[1]) left, right = start, end while left != right: mid = left + (right - left) // 2 left_sum = prefix_sums[mid] - pre_sum right_sum = prefix_sums[end] - prefix_sums[mid] new_diff = abs(left_sum - right_sum) if new_diff < diff: sums = [left_sum, right_sum] diff = new_diff if left_sum < right_sum: left = mid + 1 else: right = mid return sums prefix_sums = get_prefix_sums(A) min_diff = float("inf") for i in range(1, N - 2): left_sums = find_center_of_mass(prefix_sums, 0, i) right_sums = find_center_of_mass(prefix_sums, i + 1, N - 1) min_diff = min( min_diff, abs( min(chain(left_sums, right_sums)) - max(chain(left_sums, right_sums)) ), ) return min_diff
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def helper(self, i, j, reduction): x, y = i, j score = float("inf") best = 0 MaxVal = self.subA[j - 1] while x < y: mid = (x + y) // 2 subres = MaxVal - 2 * self.subA[mid] + reduction abssubres = abs(subres) if abssubres < score: score = abssubres best = mid if subres == 0: x, y = y, x continue if subres > 0: x, y = mid + 1, y continue else: x, y = x, mid return self.subA[best] - reduction, MaxVal - self.subA[best] def minDifference(self, N, A): res = float("inf") subSum = 0 self.subA = [0] * N for ind, value in enumerate(A): subSum += value self.subA[ind] = subSum for i in range(2, N - 1): L1, L2 = self.helper(0, i, 0) R1, R2 = self.helper(i, N, self.subA[i - 1]) score = max(L1, L2, R1, R2) - min(L1, L2, R1, R2) res = min(res, score) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array A[] of N integers. The task is to partition the array into four non-empty contiguous subarrays P, Q, R, and S such that each element of the array A[] should be present in any subarray. Let W, X, Y, and Z be the sum of the elements in P, Q, R, and S respectively. Find the smallest absolute difference between the maximum and the minimum among W, X, Y, and Z. Example 1: Input: N = 5 A[] = [4,2,2,5,1] Output: 4 Explanation: let partition the array P,Q,R,S = [4],[2,2],[5],[1] W = 4, X = 4, Y = 5, Z = 1 Differnce = max(W,X,Y,Z)-min(W,X,Y,Z)= 5-1 = 4 Example 2: Input: N = 4 A[] = {4,4,4,4} Output: 0 Explanation: There is only one way to partition the array. P,Q,R,S = [4],[4],[4],[4] Your Task: You don't need to read input or print anything. The task is to complete the function minDifference() which takes the integer N and the array A[] as inputs and returns the smallest absolute difference. Expected Time Complexity: O(NLogN) Expected Auxiliary Space: O(N) Constraints: 4 < N < 10^{5} 1 < A[i] < 10^{9}
class Solution: def minDifference(self, N, A): x = self.partition(A, N) A.reverse() y = self.partition(A, N) y.reverse() ans = 10**18 for i in range(1, N - 2): ans = min( ans, max(x[i][0], x[i][1], y[i + 1][0], y[i + 1][1]) - min(x[i][0], x[i][1], y[i + 1][0], y[i + 1][1]), ) return ans def partition(self, A, n): tmp = [(0, 0)] p = A[:] for i in range(1, n): p[i] += p[i - 1] for i in range(1, n): diff = 10**18 hi = i lo = 1 ans = 0, 0 while lo <= hi: mid = (lo + hi) // 2 x = p[mid - 1] y = p[i] - x if abs(x - y) < diff: diff = abs(x - y) ans = x, y if x > y: hi = mid - 1 else: lo = mid + 1 tmp.append(ans) return tmp
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] k = 1 Output: 0 Explanation: Here are all the pairs: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 Then the 1st smallest distance pair is (1,1), and its distance is 0. Note: 2 . 0 . 1 .
class Solution(object): def smallestDistancePair(self, nums, k): def possible(guess): count = left = 0 for right, x in enumerate(nums): while x - nums[left] > guess: left += 1 count += right - left return count >= k nums.sort() lo = 0 hi = nums[-1] - nums[0] while lo < hi: mi = (lo + hi) // 2 if possible(mi): hi = mi else: lo = mi + 1 return lo
CLASS_DEF VAR FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] k = 1 Output: 0 Explanation: Here are all the pairs: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 Then the 1st smallest distance pair is (1,1), and its distance is 0. Note: 2 . 0 . 1 .
class Solution: def smallestDistancePair(self, nums, k): nums.sort() l, r = 0, nums[-1] - nums[0] while l < r: m = l + (r - l) // 2 count = 0 left = 0 for right in range(len(nums)): while nums[right] - nums[left] > m: left += 1 count += right - left if count < k: l = m + 1 else: r = m return l
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() for i in range(n - 1, 0, -1): left = 0 right = i - 1 while left < right: if A[left] + A[right] + A[i] == X: return 1 elif A[left] + A[right] + A[i] > X: right -= 1 else: left += 1 return 0
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): dic = dict() for i in range(0, n - 2): for j in range(i + 1, n): curr = X - (A[i] + A[j]) if curr in dic: return 1 else: dic[A[j]] = 1 dic = dict() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): for i in range(1, n): temp = A[i] curr = i while A[curr] < A[curr - 1] and curr >= 1: A[curr], A[curr - 1] = A[curr - 1], A[curr] curr = curr - 1 remain = 0 ans = [] for i in range(n): remain = X - A[i] if remain <= 0: continue else: st = i + 1 en = n - 1 while st < en and en < n: if A[st] + A[en] < remain: st += 1 elif A[st] + A[en] > remain: en -= 1 else: return True return False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() count = 0 for i in range(n - 2): left = i + 1 right = n - 1 while left < right: s = A[i] + A[left] + A[right] if s == X: count += 1 break elif s > X: right -= 1 else: left += 1 return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, arr_size, sum): for i in range(0, arr_size - 1): s = set() curr_sum = sum - A[i] for j in range(i + 1, arr_size): if curr_sum - A[j] in s: return True s.add(A[j]) return False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): for i in range(n - 1): a = set() for j in range(i + 1, n): if X - (A[i] + A[j]) in a: return 1 a.add(A[j]) return 0
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() res = [] for i, v in enumerate(A): if i > 0 and v == A[i - 1]: continue left, right = i + 1, len(A) - 1 while left < right: currentSum = A[i] + A[left] + A[right] if currentSum > X: right -= 1 elif currentSum < X: left += 1 else: res.append(A[i] + A[left] + A[right]) left += 1 while A[left] == A[left - 1] and left < right: left += 1 return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def f(self, a, n, s): l = 0 r = n - 1 a.sort() for i in range(n - 2): l = i + 1 r = n - 1 while l < r: if a[i] + a[l] + a[r] == s: return 1 elif a[i] + a[l] + a[r] < s: l += 1 else: r -= 1 return 0 def find3Numbers(self, A, n, X): return self.f(A, n, X)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): for i in range(n): T = X - A[i] h2 = {} for j in range(n): if i == j: continue if T - A[j] in h2: return True else: h2[A[j]] = 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, arr, n, X): dic = {} for i in range(n): dic[arr[i]] = dic.get(arr[i], []) + [i] count = 0 for i in range(n): for j in range(i + 1, n): temp = X - arr[i] - arr[j] if temp in dic: if i not in dic[temp] and j not in dic[temp]: count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR LIST LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, a, n, x): a.sort() for i in range(n): num1 = a[i] low = i + 1 high = n - 1 reqsum = x - num1 while low < high: if a[low] + a[high] == reqsum: return True elif a[low] + a[high] < reqsum: low += 1 else: high -= 1 return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, nums, n, k): ans = set() nums.sort() for i in range(len(nums)): start = i + 1 end = len(nums) - 1 while start < end: sum_ = nums[start] + nums[end] + nums[i] if sum_ == k: ans.add((nums[i], nums[start], nums[end])) while start < end and nums[start] == nums[start + 1]: start += 1 start += 1 while start < end and nums[end] == nums[end - 1]: end -= 1 end -= 1 elif sum_ > k: end -= 1 else: start += 1 return len(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() for i in range(n): j = i + 1 k = n - 1 while j < k: sum = A[i] + A[j] + A[k] if sum == X: return True elif sum < X: j = j + 1 else: k = k - 1 return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, arr, n, x): arr.sort() for i in range(n - 2): l = i + 1 h = n - 1 while l < h: if arr[h] > x: h = h - 1 if arr[l] + arr[h] + arr[i] == x: return True elif arr[l] + arr[h] + arr[i] > x: h = h - 1 else: l = l + 1 return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() cnt = 0 for i in range(n - 2): requiredSum = X - A[i] left, right = i + 1, n - 1 while left < right: currentSum = A[left] + A[right] if currentSum == requiredSum: cnt += 1 left += 1 right -= 1 elif currentSum > requiredSum: right -= 1 else: left += 1 return cnt
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() for i in range(n - 2): rem_sum = X - A[i] j = i + 1 k = n - 1 while j != k: s = A[j] + A[k] if s < rem_sum: j += 1 else: k -= 1 if s == rem_sum: return True return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): for i in range(n): d = [(0) for _ in range(X + 1)] for j in range(i + 1, n): if X - A[i] - A[j] > 0 and d[X - A[i] - A[j]] == 1: return 1 elif A[j] <= X: d[A[j]] = 1 else: continue return 0
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A = sorted(A) if n <= 3: return False for i in range(n): front = i + 1 back = n - 1 while front < n and back > i and front < back: if A[front] + A[back] == X - A[i]: front += 1 back -= 1 return True elif A[front] + A[back] < X - A[i]: front += 1 else: back -= 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): def two_sum(a, arr, x): d = {} for i in range(len(arr)): if x - arr[i] in d: return True else: d[arr[i]] = i return False A.sort() for i in range(n): if two_sum(A[i], A[i + 1 :], X - A[i]): return True return False
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): for i in range(n): A.sort() c = A[i] p1 = i + 1 p2 = n - 1 while p1 < p2: if A[p1] + A[p2] == X - c: return True elif A[p1] + A[p2] < X - c: p1 += 1 else: p2 -= 1 return False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A = set(A) A = list(A) n = len(A) for i in range(n - 1): s = A[i] for j in range(i + 1, n): if X - (s + A[j]) in A[j + 1 : n]: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A = sorted(A) for i in range(n - 2): j = i + 1 k = n - 1 while j < k: sum = X - A[i] if A[j] + A[k] == sum: return 1 elif A[j] + A[k] > sum: k -= 1 else: j += 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): count = 0 z = {} for i in A: z.setdefault(i, 0) z[i] += 1 x = 0 y = 0 for i in range(n): for j in range(i + 1, n): x = 0 y = 0 if X - A[i] - A[j] == A[i]: x = 1 if X - A[i] - A[j] == A[j]: y = 1 if X - A[i] - A[j] in z: count += z[X - A[i] - A[j]] - x - y return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): for i in range(n): newTarget = X - A[i] hashtable = {} for j in range(i + 1, n): need = newTarget - A[j] if need in hashtable: return True hashtable[A[j]] = 1 return False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): dic = {} for i in A: if i in dic: dic[i] += 1 else: dic[i] = 1 for i in range(0, n): dic[A[i]] -= 1 for j in range(i + 1, n): dic[A[j]] -= 1 s = X - (A[i] + A[j]) if s in dic and dic[s]: return 1 dic[A[j]] += 1 dic[A[i]] += 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): count = 0 for i in range(n): st = set() sm = X - A[i] for j in range(i + 1, n): if sm - A[j] in st: count += 1 else: st.add(A[j]) return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, nums, n, X): res = [] nums.sort() for i, a in enumerate(nums): if i > 0 and a == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: s = a + nums[left] + nums[right] if s > X: right -= 1 elif s < X: left += 1 else: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def twoSum(self, arr, target): hash_set = set() for num in arr: if num in hash_set: return True else: hash_set.add(target - num) return False def find3Numbers(self, A, n, X): target = X for i in range(n - 2): num = A[i] if self.twoSum(A[i + 1 :], target - num): return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): count = 0 A.sort() for i in range(n): if i > 0 and A[i] == A[i - 1]: continue j = i + 1 k = n - 1 while j < k: sum = A[i] + A[j] + A[k] if sum < X: j += 1 elif sum > X: k -= 1 else: count += 1 j += 1 k -= 1 while j < k and A[j] == A[j - 1]: j += 1 while j < k and A[k] == A[k + 1]: k -= 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): A.sort() n = len(A) found = False for i in range(n): left = i + 1 right = n - 1 while left < right: sum = A[left] + A[right] + A[i] if sum == X: found = True right -= 1 left += 1 elif sum > X: right -= 1 else: left += 1 if found == True: return True else: return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X. Example 1: Input: n = 6, X = 13 arr[] = [1 4 45 6 10 8] Output: 1 Explanation: The triplet {1, 4, 8} in the array sums up to 13. Example 2: Input: n = 5, X = 10 arr[] = [1 2 4 3 6] Output: 1 Explanation: The triplet {1, 3, 6} in the array sums up to 10. Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers() which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet in the array arr[] which sums up to X and False otherwise. Expected Time Complexity: O(n^{2}) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{3} 1 ≤ A[i] ≤ 10^{5}
class Solution: def find3Numbers(self, A, n, X): dic = {} for i in A: if i not in dic: dic[i] = 1 continue dic[i] += 1 count = 0 for a in range(n): for b in range(n): if a == b: continue key1 = A[a] key2 = A[b] dic[key1] -= 1 dic[key2] -= 1 if X - A[a] - A[b] in dic and dic[X - A[a] - A[b]] > 0: return True dic[key1] += 1 dic[key2] += 1 return count >= 1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN VAR NUMBER
You are given an array $a$ of $n$ integers. You are asked to find out if the inequality $$\max(a_i, a_{i + 1}, \ldots, a_{j - 1}, a_{j}) \geq a_i + a_{i + 1} + \dots + a_{j - 1} + a_{j}$$ holds for all pairs of indices $(i, j)$, where $1 \leq i \leq j \leq n$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the size of the array. The next line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, on a new line output "YES" if the condition is satisfied for the given array, and "NO" otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 3 4 -1 1 -1 2 5 -1 2 -3 2 -1 3 2 3 -1 Output YES YES NO -----Note----- In test cases $1$ and $2$, the given condition is satisfied for all $(i, j)$ pairs. In test case $3$, the condition isn't satisfied for the pair $(1, 2)$ as $\max(2, 3) < 2 + 3$.
import sys input = sys.stdin.readline def sol(arr, n): l = 0 r = n - 1 for i in range(n - 1): if arr[i] > 0 and arr[i + 1] > 0: return "NO" while l < n and arr[l] <= 0: l += 1 while r >= 0 and arr[r] <= 0: r -= 1 if r < 0: return "YES" res = [] while l <= r: acc = 0 if arr[l] <= 0: while l <= r and arr[l] <= 0: acc += arr[l] l += 1 else: acc = arr[l] l += 1 res.append(acc) if len(res) <= 2: return "YES" stack = [0] dp = [0] * len(res) for i in range(2, len(res), 2): acc = res[i - 1] while stack and res[stack[-1]] <= res[i]: acc += res[stack[-1]] if acc > 0: return "NO" acc += dp[stack.pop()] dp[i] = acc stack.append(i) stack = [len(res) - 1] dp = [0] * len(res) for i in range(len(res) - 3, -1, -2): acc = res[i + 1] while stack and res[stack[-1]] <= res[i]: acc += res[stack[-1]] if acc > 0: return "NO" acc += dp[stack.pop()] dp[i] = acc stack.append(i) return "YES" t = int(input()) for case in range(t): n = int(input()) arr = list(map(int, input().split())) print(sol(arr, n))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN STRING WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers. You are asked to find out if the inequality $$\max(a_i, a_{i + 1}, \ldots, a_{j - 1}, a_{j}) \geq a_i + a_{i + 1} + \dots + a_{j - 1} + a_{j}$$ holds for all pairs of indices $(i, j)$, where $1 \leq i \leq j \leq n$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the size of the array. The next line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, on a new line output "YES" if the condition is satisfied for the given array, and "NO" otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 3 4 -1 1 -1 2 5 -1 2 -3 2 -1 3 2 3 -1 Output YES YES NO -----Note----- In test cases $1$ and $2$, the given condition is satisfied for all $(i, j)$ pairs. In test case $3$, the condition isn't satisfied for the pair $(1, 2)$ as $\max(2, 3) < 2 + 3$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) pre = [0] suf = [0] min_suf = [0] * n min_pre = [0] * n stk = [] flag = True for i in range(n): pre.append(pre[-1] + a[i]) temp_min = pre[i] while len(stk) and a[stk[-1]] <= a[i]: temp_min = min(temp_min, min_pre[stk[-1]]) stk.pop() min_pre[i] = temp_min stk.append(i) stk.clear() for i in range(n - 1, -1, -1): suf.append(suf[-1] + a[i]) temp_min = suf[n - 1 - i] while len(stk) and a[stk[-1]] <= a[i]: temp_min = min(temp_min, min_suf[stk[-1]]) stk.pop() min_suf[i] = temp_min stk.append(i) for i in range(n): if suf[n - i] - min_suf[i] + pre[i + 1] - min_pre[i] - 2 * a[i] > 0: print("NO") flag = False break if flag: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
You are given an array $a$ of $n$ integers. You are asked to find out if the inequality $$\max(a_i, a_{i + 1}, \ldots, a_{j - 1}, a_{j}) \geq a_i + a_{i + 1} + \dots + a_{j - 1} + a_{j}$$ holds for all pairs of indices $(i, j)$, where $1 \leq i \leq j \leq n$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the size of the array. The next line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, on a new line output "YES" if the condition is satisfied for the given array, and "NO" otherwise. You can print each letter in any case (upper or lower). -----Examples----- Input 3 4 -1 1 -1 2 5 -1 2 -3 2 -1 3 2 3 -1 Output YES YES NO -----Note----- In test cases $1$ and $2$, the given condition is satisfied for all $(i, j)$ pairs. In test case $3$, the condition isn't satisfied for the pair $(1, 2)$ as $\max(2, 3) < 2 + 3$.
import sys input = sys.stdin.readline def solve(): n = int(input()) arr = list(map(int, input().split())) ng1, ng2 = [-1] * n, [-1] * n st1, st2 = [], [] ps, ss = [0] * n, [0] * n v1 = v2 = 0 for i in range(n): while st1 and arr[st1[-1]] <= arr[i]: ng1[st1.pop()] = i st1.append(i) v1 += arr[i] ps[i] = v1 j = n - 1 - i while st2 and arr[st2[-1]] <= arr[j]: ng2[st2.pop()] = j st2.append(j) v2 += arr[j] ss[j] = v2 for i in range(n): if arr[i] <= 0: continue if ng1[i] > -1: if ng1[i] == i + 1: return "NO" v = ps[ng1[i] - 1] - ps[i] if abs(v) < arr[i]: return "NO" elif i != n - 1: v = ps[-1] - ps[i] if v > 0: return "NO" if ng2[i] > -1: if ng2[i] == i - 1: return "NO" v = ss[ng2[i] + 1] - ss[i] if abs(v) < arr[i]: return "NO" elif i != 0: v = ss[0] - ss[i] if v > 0: return "NO" return "YES" t = int(input()) for i in range(t): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN STRING IF VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR