description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: sorted_arr = sorted(arr) n = len(arr) median = sorted_arr[(n - 1) // 2] results = [] start_idx, end_idx = 0, n - 1 while k > 0 and start_idx <= end_idx: if abs(sorted_arr[start_idx] - median) < abs(sorted_arr[end_idx] - median): results.append(sorted_arr[end_idx]) end_idx -= 1 elif abs(sorted_arr[start_idx] - median) > abs( sorted_arr[end_idx] - median ): results.append(sorted_arr[start_idx]) start_idx += 1 elif sorted_arr[start_idx] > sorted_arr[end_idx]: results.append(sorted_arr[start_idx]) start_idx += 1 else: results.append(sorted_arr[end_idx]) end_idx -= 1 k -= 1 return results
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() n = len(arr) res = [] median = arr[(n - 1) // 2] l = [] arr.reverse() for i in range(n): l.append([arr[i], abs(median - arr[i])]) l.sort(reverse=True, key=lambda x: x[1]) i = 0 while i < k - 1 or i < n - 1 and l[i][1] == l[i + 1][1]: res.append(l[i][0]) i += 1 res.append(l[i][0]) return res[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: sarr = sorted(arr) L = len(arr) if L / 2 % 1 == 0: median = sarr[int((L - 1) / 2)] else: median = sarr[int(L / 2 - 0.5)] absm = [abs(a - median) for a in arr] tarr = [[absm[i], arr[i]] for i in range(L)] tarr = sorted(tarr, reverse=True) res = [tarr[i][1] for i in range(L) if i < k] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr = sorted(arr) self.median = arr[(len(arr) - 1) // 2] count = 1 i, j = 0, len(arr) - 1 values = [] while count <= k: if self.key_func(arr[i]) > self.key_func(arr[j]): values.append(arr[i]) i += 1 elif self.key_func(arr[i]) < self.key_func(arr[j]): values.append(arr[j]) j -= 1 elif arr[i] > arr[j]: values.append(arr[i]) i += 1 else: values.append(arr[j]) j -= 1 count += 1 return values def key_func(self, elem): return abs(elem - self.median)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() start, end = 0, len(arr) - 1 res = [] l = len(arr) med = arr[end // 2] is_greater = lambda x, y: abs(x - med) > abs(y - med) while end >= start: if is_greater(arr[start], arr[end]): res.append(arr[start]) start += 1 else: res.append(arr[end]) end -= 1 return res[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: n = len(arr) arr.sort(reverse=True) m = arr[ceil((n - 1) / 2)] arr.sort(key=lambda x: abs(x - m), reverse=True) return arr[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() n = len(arr) m = arr[(n - 1) // 2] res = [] i, j = 0, n - 1 while k > 0: if arr[j] - m >= m - arr[i]: res.append(arr[j]) j -= 1 else: res.append(arr[i]) i += 1 k -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() mean = arr[(len(arr) - 1) // 2] arr2 = list(map(lambda x: abs(x - mean), arr)) arr3 = list(zip(arr, arr2)) arr4 = sorted(arr3, key=lambda kv: (-kv[1], -kv[0])) return [arr4[i][0] for i in range(0, k)]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: orderarr = sorted(arr) index = (len(arr) - 1) // 2 mid = orderarr[index] dic = defaultdict(list) for i in range(len(orderarr) - 1, -1, -1): curabs = abs(orderarr[i] - mid) dic[curabs].append(orderarr[i]) res = [] keys = sorted(dic.keys()) for i in range(len(keys) - 1, -1, -1): for j in dic[keys[i]]: res.append(j) if len(res) == k: return res return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: x = sorted(arr) m = x[int((len(x) - 1) / 2)] i = 0 j = len(arr) - 1 strong = [] while i <= j: if abs(x[i] - m) <= abs(x[j] - m): strong.append(x[j]) j -= 1 else: strong.append(x[i]) i += 1 return strong[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: s = sorted(arr) i = (len(arr) - 1) // 2 m = s[i] strengths = [] for item in s: st = abs(item - m) strengths.append((st, item)) sorted_strengths = sorted(strengths, reverse=True) sorted_vals = [] for ss in sorted_strengths: sorted_vals.append(ss[1]) return sorted_vals[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: median = sorted(arr)[(len(arr) - 1) // 2] median_sorted_array = sorted( ((i, val) for i, val in enumerate(arr)), key=lambda x: (abs(x[1] - median), x[1]), reverse=True, ) return [x[1] for x in median_sorted_array[:k]]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER RETURN VAR NUMBER VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] l, r = 0, len(arr) - 1 ret = [] while k > 0 and l <= r: print((l, r)) al, ar = abs(arr[l] - m), abs(arr[r] - m) if al == ar: if arr[l] >= arr[r]: ret.append(arr[l]) l += 1 else: ret.append(arr[r]) r -= 1 elif al > ar: ret.append(arr[l]) l += 1 else: ret.append(arr[r]) r -= 1 k -= 1 return ret
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] strongs = [] for num in arr: strongs.append((abs(num - m), num)) strongs.sort(reverse=True) return [x[1] for x in strongs[:k]]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: n = len(arr) arr = sorted(arr) med_idx = (n - 1) // 2 med_val = arr[med_idx] dist = [] for i in range(len(arr)): dist.append((abs(arr[i] - med_val), arr[i], i)) dist.sort(key=lambda x: (x[0], x[1]), reverse=True) ans = [] for i in range(len(dist)): if i == k: break ans.append(arr[dist[i][2]]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: sorted_arr = sorted(arr) m = sorted_arr[(len(arr) - 1) // 2] num_dist = [(e, abs(e - m)) for e in sorted_arr] num_dist.sort(key=lambda x: x[0], reverse=True) num_dist.sort(key=lambda x: x[1], reverse=True) ans = [e[0] for e in num_dist] return ans[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: n = len(arr) arr = sorted(arr) m = -1 if n % 2 == 0: m = arr[(n - 1) // 2] else: m = arr[n // 2] arr = sorted(arr, key=lambda x: (abs(x - m), x)) return arr[-k:]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: if not arr: return [] elif len(arr) <= k: return arr arr.sort() n = len(arr) median = arr[int((n - 1) / 2)] arr = [(abs(i - median), i) for i in arr] arr.sort(reverse=True) return [i[1] for i in arr[:k]]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN LIST IF FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() median = arr[(len(arr) - 1) // 2] list = [] i = 0 j = len(arr) - 1 while i <= j and k > 0: if abs(arr[i] - median) > abs(arr[j] - median): list.append(arr[i]) i += 1 else: list.append(arr[j]) j -= 1 k -= 1 return list
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: nums = sorted(arr) median = nums[(len(nums) - 1) // 2] arr.sort(key=lambda x: (abs(x - median), x), reverse=True) res = [] for num in arr: res.append(num) k -= 1 if k == 0: break return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: n = len(arr) arr = sorted(arr) mid = arr[(n - 1) // 2] def mykey(x): return abs(x - mid), x arr = sorted(arr, key=lambda x: (abs(x - mid), x)) return arr[n - k :]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr, k): n = len(arr) arr = sorted(arr) m = arr[(n - 1) // 2] i, j = 0, n - 1 while k > 0: if arr[j] - m >= m - arr[i]: j -= 1 else: i += 1 k -= 1 return arr[:i] + arr[j + 1 :]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] l = [] i = 0 j = len(arr) - 1 while i <= j: if abs(arr[i] - m) > abs(arr[j] - m): l.append(arr[i]) i = i + 1 elif abs(arr[i] - m) <= abs(arr[j] - m): l.append(arr[j]) j = j - 1 if len(l) == k: return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: sort_arr = sorted(arr) n = len(arr) m = sort_arr[(n - 1) // 2] result = sorted(sort_arr, key=lambda x: [abs(x - m), x], reverse=True) return result[0:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest2(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] dp = [[float("inf") for i in range(len(arr))] for i in range(len(arr))] for i in range(len(arr)): for j in range(len(arr)): if abs(arr[i] - m) > abs(arr[j] - m): strong = arr[i] elif abs(arr[i] - m) == abs(arr[j] - m): strong = arr[i] if arr[i] > arr[j] else arr[j] else: strong = arr[j] dp[i][j] = strong for line in dp: print(line) print() def getStrongest(self, arr: List[int], k: int) -> List[int]: med = sorted(arr)[(len(arr) - 1) // 2] return sorted(arr, key=lambda x: (abs(x - med), x))[-k:]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: if len(arr) <= k: return arr arr.sort() median = arr[(len(arr) - 1) // 2] i = 0 j = len(arr) - 1 result = [] while k > 0: if self.isStronger(arr[i], arr[j], median): result.append(arr[i]) i += 1 else: result.append(arr[j]) j -= 1 k -= 1 return result def isStronger(self, a, b, median): return ( abs(a - median) > abs(b - median) or abs(a - median) == abs(b - median) and a > b )
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: svals = collections.defaultdict(list) median = sorted(arr)[(len(arr) - 1) // 2] for i in range(len(arr)): svals[abs(arr[i] - median)].append(arr[i]) svals = sorted(svals.items(), key=lambda x: -x[0]) res = [] count = 0 for tuplee in svals: flag = True for i in sorted(tuplee[1], reverse=True): res.append(i) count += 1 if count == k: flag = False break if not flag: break return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() i, j = 0, len(arr) - 1 median = arr[(len(arr) - 1) // 2] while len(arr) + i - j <= k: if median - arr[i] > arr[j] - median: i = i + 1 else: j = j - 1 return arr[:i] + arr[j + 1 :]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() median = arr[(len(arr) - 1) // 2] count = 0 arry = [] while arr and len(arry) < k: if arr[-1] - median >= median - arr[0]: arry.append(arr[-1]) del arr[-1] else: arry.append(arr[0]) del arr[0] return arry
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() median = arr[(len(arr) - 1) // 2] arr.sort(key=lambda x: (abs(x - median), x), reverse=True) print((arr, median)) return arr[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest1(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] d = {} for i in range(len(arr) - 1, -1, -1): tmp = abs(arr[i] - m) d.setdefault(tmp, []) d[tmp].append(arr[i]) res = [] for key in sorted(d.keys(), reverse=True): res.extend(d[key]) if len(res) >= k: break return res[:k] def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() la = len(arr) m = arr[(la - 1) // 2] i, j, res = 0, la - 1, [] while len(res) < k: rt = abs(arr[j] - m) lt = abs(arr[i] - m) if rt == lt or rt > lt: res.append(arr[j]) j -= 1 else: res.append(arr[i]) i += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
def median(arr: List[int]) -> int: arr.sort() m = int((len(arr) - 1) / 2) return arr[m] class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: m = median(arr) strength = [abs(x - m) for x in arr] zipped = list(zip(strength, arr)) zipped.sort(key=lambda x: (x[0], x[1])) return [x[1] for x in zipped[len(arr) - k :]]
FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def _get_median(self, arr): mid = int((len(arr) - 1) / 2) return arr[mid] def getStrongest(self, arr: List[int], k: int) -> List[int]: arr = sorted(arr) median = self._get_median(arr) arr = sorted([(abs(x - median), x) for x in arr], reverse=True)[:k] return [x for rank, x in arr]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: if not k or not arr: return [] arr = sorted(arr) m = arr[(len(arr) - 1) // 2] l = 0 r = len(arr) - 1 ans = [] while k > 0 and l <= r: if abs(arr[l] - m) > abs(arr[r] - m): ans.append(arr[l]) l += 1 elif abs(arr[l] - m) == abs(arr[r] - m): if arr[l] > arr[r]: ans.append(arr[l]) l += 1 else: ans.append(arr[r]) r -= 1 else: ans.append(arr[r]) r -= 1 k -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() back = arr[:] n = len(arr) med = arr[(n - 1) // 2] ans = [] for i in range(n): arr[i] = abs(arr[i] - med) l, r = 0, n - 1 while l <= r and len(ans) < k: if arr[l] < arr[r]: ans += [back[r]] r -= 1 elif arr[l] > arr[r]: ans += [back[l]] l += 1 else: ans += [back[r]] r -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR LIST VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR LIST VAR VAR VAR NUMBER VAR LIST VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() ans = [] lt = 0 pt = len(arr) - 1 m = arr[(len(arr) - 1) // 2] for i in range(k): left = abs(arr[lt] - m) right = abs(arr[pt] - m) if right >= left: ans.append(arr[pt]) pt -= 1 else: ans.append(arr[lt]) lt += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: temp = list(arr) temp.sort() median = 0 median = temp[(len(temp) - 1) // 2] l = 0 r = len(arr) - 1 res = [] while k > 0: if median - temp[l] <= temp[r] - median: res.append(temp[r]) r -= 1 else: res.append(temp[l]) l += 1 k -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: n = len(arr) median = sorted(arr)[(n - 1) // 2] arr_pair = [(x, abs(x - median)) for x in arr] arr_pair.sort(key=lambda x: (x[1], x[0])) return [x[0] for x in arr_pair[-k:]]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() n = len(arr) m = arr[int((n - 1) / 2)] result = [] l, r = 0, n - 1 count = 0 while l <= r and len(result) < k: if abs(arr[r] - m) > abs(arr[l] - m): result.append(arr[r]) r -= 1 elif abs(arr[l] - m) > abs(arr[r] - m): result.append(arr[l]) l += 1 else: result.append(arr[r]) r -= 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: if len(arr) == 1 and k == 1: return arr ans = [] arr.sort() m = arr[(len(arr) - 1) // 2] i, j = 0, len(arr) - 1 while i <= j: if abs(arr[j] - m) >= abs(arr[i] - m): ans.append(arr[j]) j -= 1 else: ans.append(arr[i]) i += 1 print(ans) return ans[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] temp, res = [], [] for i in range(len(arr)): temp.append(abs(arr[i] - m)) zipped_pairs = zip(temp, arr) z = [x for _, x in sorted(zipped_pairs)] return z[len(z) - k :]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() m = arr[(len(arr) - 1) // 2] return heapq.nlargest(k, [n for n in arr[::-1]], key=lambda x: abs(x - m))
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: ls = len(arr) arr.sort() median = arr[(ls - 1) // 2] print(median) res = [] i = 0 for num in arr: if i == k: heapq.heappush(res, (abs(num - median), num)) heapq.heappop(res) else: heapq.heappush(res, (abs(num - median), num)) i += 1 return [x[1] for x in res]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: out = [] arr.sort() l = 0 h = len(arr) - 1 m = arr[int(h / 2)] while l <= h: if abs(arr[l] - m) > abs(arr[h] - m): out.append(arr[l]) l += 1 else: out.append(arr[h]) h -= 1 return out[0:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() med = arr[int((len(arr) - 1) / 2)] print(med) f = [] while len(f) < k: rv = abs(arr[-1] - med) lv = abs(arr[0] - med) if rv >= lv: f.append(arr.pop(-1)) else: f.append(arr.pop(0)) return f
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def get_median(self, arr): arr.sort() n = len(arr) return arr[(n - 1) // 2] def getStrongest(self, arr: List[int], k: int) -> List[int]: StrengthIndexAndVal = collections.namedtuple( "StrengthIndexAndVal", ("strength", "index", "val") ) if len(arr) == 0: return [] m = self.get_median(arr) heap = [] for i, val in enumerate(arr): siv = StrengthIndexAndVal(abs(val - m), i, val) if len(heap) == k: heapq.heappushpop(heap, siv) else: heapq.heappush(heap, siv) return [x.val for x in heap]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING STRING STRING IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: if k >= len(arr): return arr median = self.get_median(arr) results = [(math.fabs(arr[x] - median), arr[x]) for x in range(len(arr))] results.sort() return [results[-i - 1][1] for i in range(k)] def get_median(self, arr): arr_sorted = sorted(arr) idx = int((len(arr) - 1) / 2) return arr_sorted[idx]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, l: List[int], k: int) -> List[int]: l.sort() m = l[(len(l) - 1) // 2] def fun(a, b): if abs(a - m) < abs(b - m): return 1 elif abs(a - m) > abs(b - m): return -1 else: return -1 l = sorted(l, key=cmp_to_key(fun)) l1 = [] for i in range(k): l1.append(l[i]) return l1
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() lastPointer = -1 startPointer = 0 mInd = (len(arr) - 1) // 2 m = arr[mInd] res = [] for i in range(k): if abs(arr[lastPointer] - m) >= abs(arr[startPointer] - m): res.append(arr[lastPointer]) lastPointer -= 1 else: res.append(arr[startPointer]) startPointer += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() p = (len(arr) - 1) // 2 med = arr[p] print(med) a1 = arr[:p] a2 = arr[p:] ans = [] while k > 0: if len(a2) > 0 and len(a1) > 0: diff = abs(a2[-1] - med) - abs(a1[0] - med) if diff >= 0: ans.append(a2.pop(-1)) else: ans.append(a1.pop(0)) elif len(a2) > 0: ans.append(a2.pop(-1)) elif len(a1) > 0: ans.append(a1.pop(0)) k -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: x = sorted(arr) l = len(arr) median = x[(l - 1) // 2] jj = sorted( list(range(len(arr))), key=lambda y: (abs(arr[y] - median), arr[y]), reverse=True, ) n = [arr[i] for i in jj] return n[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() res = [] val = arr[(len(arr) - 1) // 2] arr.sort(key=lambda x: (abs(x - val), x), reverse=True) return arr[:k]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, A: List[int], k: int) -> List[int]: A.sort() z = [] if len(A) % 2 == 1: n = A[(len(A) - 1) // 2] else: n = A[len(A) // 2 - 1] for i in A: z.append([abs(i - n), i]) z.sort(key=lambda x: x[0]) x = [j for i, j in z] return x[len(x) - k :]
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() median = arr[int((len(arr) - 1) / 2)] strong = {} for x in arr: val = abs(x - median) if val not in list(strong.keys()): strong[abs(x - median)] = [x] else: strong[abs(x - median)].append(x) strong = sorted(list(strong.items()), reverse=True) strongest = [] for x in strong: for val in sorted(x[1], reverse=True): if len(strongest) == k: break strongest.append(val) return strongest
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: n = len(arr) heap = [] for num in arr: if len(heap) >= n // 2 + 1: heapq.heappushpop(heap, num) else: heapq.heappush(heap, num) median = heap[0] heap = [] for num in arr: if len(heap) >= k: heapq.heappushpop(heap, (abs(median - num), num)) else: heapq.heappush(heap, (abs(median - num), num)) return [num for _, num in heap]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr.sort() n = len(arr) median = arr[(n - 1) // 2] i = 0 j = n - 1 ans = [] for _ in range(k): s1 = abs(arr[i] - median) s2 = abs(arr[j] - median) if s1 > s2: ans.append(arr[i]) i += 1 elif s1 == s2: if arr[i] > arr[j]: ans.append(arr[i]) i += 1 else: ans.append(arr[j]) j -= 1 else: ans.append(arr[j]) j -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: N = len(arr) arr.sort() median = arr[(N - 1) // 2] heap = [] for n in arr: heapq.heappush(heap, (-abs(n - median), -n, n)) ans = [] for _ in range(k): ans.append(heapq.heappop(heap)[-1]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr and an integer k. A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. Return a list of the strongest k values in the array. return the answer in any arbitrary order. Median is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position ((n - 1) / 2) in the sorted list (0-indexed). For arr = [6, -3, 7, 2, 11], n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6. For arr = [-7, 22, 17, 3], n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.   Example 1: Input: arr = [1,2,3,4,5], k = 2 Output: [5,1] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also accepted answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1. Example 2: Input: arr = [1,1,3,5,5], k = 2 Output: [5,5] Explanation: Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5]. Example 3: Input: arr = [6,7,11,7,6,8], k = 5 Output: [11,8,6,6,7] Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is accepted. Example 4: Input: arr = [6,-3,7,2,11], k = 3 Output: [-3,11,2] Example 5: Input: arr = [-7,22,17,3], k = 2 Output: [22,17]   Constraints: 1 <= arr.length <= 10^5 -10^5 <= arr[i] <= 10^5 1 <= k <= arr.length
class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: if len(arr) == 1: return arr arr.sort() med_loc = (len(arr) - 1) // 2 median = arr[med_loc] count = 0 ans = [] while count < k: if abs(arr[0] - median) > abs(arr[-1] - median): ans.append(arr[0]) arr.pop(0) else: ans.append(arr[-1]) arr.pop() count += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution(object): def threeSumClosest(self, nums, target): size = len(nums) if size < 3: return 0 nums.sort() i = 0 ans = nums[0] + nums[1] + nums[size - 1] while i < size - 2: tmp = target - nums[i] j = i + 1 k = size - 1 while j < k: if nums[j] + nums[k] == tmp: return target if nums[j] + nums[k] > tmp: if nums[j] + nums[j + 1] >= tmp: if nums[j] + nums[j + 1] - tmp < abs(ans - target): ans = nums[i] + nums[j] + nums[j + 1] break tmpans = nums[i] + nums[j] + nums[k] if tmpans - target < abs(ans - target): ans = tmpans k -= 1 else: if nums[k] + nums[k - 1] <= tmp: if tmp - nums[k] - nums[k - 1] < abs(ans - target): ans = nums[i] + nums[k - 1] + nums[k] break tmpans = nums[i] + nums[j] + nums[k] if target - tmpans < abs(ans - target): ans = tmpans j += 1 i += 1 if ans == target: return target return ans
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER WHILE 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 IF BIN_OP VAR VAR VAR VAR VAR RETURN VAR IF BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, array, target): array.sort() n = len(array) best_sum = float("inf") for i in range(n - 2): if i > 0 and array[i] == array[i - 1]: continue diff = target - array[i] x, y = i + 1, n - 1 best_diff = float("inf") while x < y: curr_diff = array[x] + array[y] if curr_diff == diff: return target elif curr_diff < diff: x += 1 else: y -= 1 if abs(curr_diff - diff) < abs(best_diff - diff): best_diff = curr_diff curr_sum = array[i] + best_diff if abs(curr_sum - target) < abs(best_sum - target): best_sum = curr_sum return best_sum
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): if len(nums) <= 3: return sum(nums) nums.sort() diff = float("inf") for idx, num in enumerate(nums[:-2]): if idx >= 1 and num == nums[idx - 1]: continue start, end = idx + 1, len(nums) - 1 while start < end: temp_sum = nums[start] + nums[end] new_target = target - num if temp_sum == new_target: return target elif temp_sum > new_target: end -= 1 else: start += 1 if abs(new_target - temp_sum) < diff: if new_target - temp_sum < 0: sign = -1 else: sign = 1 diff = abs(new_target - temp_sum) return target - sign * diff
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER 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 VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): if len(nums) < 3: return sum(nums) elif len(nums) == 3: return sum(nums) nums = sorted(nums) res = nums[0] + nums[1] + nums[2] for i in range(len(nums) - 2): if i != 0 and nums[i] == nums[i - 1]: continue j = i + 1 k = len(nums) - 1 while j < k: s = nums[i] + nums[j] + nums[k] if s == target: return target if s < target: j += 1 else: k -= 1 if abs(s - target) < abs(res - target): res = s return res
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER 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 RETURN VAR IF VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): nums = sorted(nums) res, diff = None, None for first in range(len(nums) - 2): if first and nums[first] == nums[first - 1]: continue second, third = first + 1, len(nums) - 1 if diff is None: res = nums[first] + nums[second] + nums[third] diff = abs(res - target) while second < third: three_sum = nums[first] + nums[second] + nums[third] if three_sum == target: return target elif three_sum < target: partial = nums[first] + nums[third] while second < third and nums[second] + partial < target: second += 1 tmp = nums[second - 1] + partial if abs(tmp - target) < diff: diff = abs(tmp - target) res = tmp else: partial = nums[first] + nums[second] while second < third and partial + nums[third] > target: third -= 1 tmp = partial + nums[third + 1] if abs(tmp - target) < diff: diff = abs(tmp - target) res = tmp return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NONE NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): arr, i, closest, dist = nums, 0, None, None arr.sort() if len(arr) == 3: return arr[0] + arr[1] + arr[2] while i < len(arr) - 2: if i > 0 and arr[i] > target: return closest if i == 0 or arr[i] != arr[i - 1]: j, k = i + 1, len(arr) - 1 while j < k: val = arr[i] + arr[j] + arr[k] if val == target: return val temp_dist = abs(target - val) if dist == None or temp_dist < dist: closest = val dist = temp_dist if val < target: j += 1 else: k -= 1 i += 1 return closest
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER NONE NONE EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR RETURN VAR IF VAR NUMBER VAR 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 RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): nums.sort() result = nums[0] + nums[1] + nums[2] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue if nums[i] + nums[i + 1] + nums[i + 2] > target + abs(result - target): return result st = i + 1 ed = len(nums) - 1 while st < ed: dis = nums[i] + nums[st] + nums[ed] - target if dis == 0: return target elif dis > 0: while st < ed and nums[i] + nums[st] + nums[ed] - target > 0: ed -= 1 if abs(nums[i] + nums[st] + nums[ed + 1] - target) < abs( result - target ): result = nums[i] + nums[st] + nums[ed + 1] else: while st < ed and nums[i] + nums[st] + nums[ed] - target < 0: st += 1 if abs(nums[i] + nums[st - 1] + nums[ed] - target) < abs( result - target ): result = nums[i] + nums[st - 1] + nums[ed] return result
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN 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 BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): nums = sorted(nums) size = len(nums) res = None diff = None for i in range(size - 2): if i and nums[i] == nums[i - 1]: continue left, right = i + 1, size - 1 if not diff: res = nums[i] + nums[left] + nums[right] diff = abs(res - target) while left < right: sums = nums[i] + nums[left] + nums[right] if sums == target: return target elif sums < target: partial = nums[i] + nums[right] while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and partial + nums[left] < target: left += 1 tmp = partial + nums[left - 1] if abs(tmp - target) < diff: res, diff = tmp, abs(tmp - target) else: partial = nums[i] + nums[left] while left < right and nums[right] == nums[right - 1]: right -= 1 while left < right and partial + nums[right] > target: right -= 1 tmp = partial + nums[right + 1] if abs(tmp - target) < diff: res, diff = tmp, abs(tmp - target) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution: def threeSumClosest(self, nums, target): res = 0 nlen = len(nums) if nlen < 3: for num in nums: res += num return res lastdiff = sys.maxsize previous = None nums = sorted(nums) for idx in range(nlen - 2): num = nums[idx] if num == previous: continue i = idx + 1 j = nlen - 1 previous = num cur_sum = num while i < j: diff = 0 cur_sum += nums[i] + nums[j] if cur_sum == target: return cur_sum if cur_sum < target: diff = target - cur_sum i += 1 else: diff = cur_sum - target j -= 1 if diff < lastdiff: lastdiff = diff res = cur_sum cur_sum = num return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution(object): def threeSumMulti(self, A, target): count = [0] * 101 ans = 0 for n in A: count[n] += 1 for x in range(101): for y in range(x + 1, 101): z = target - x - y if x < y < z <= 100: ans += count[x] * count[y] * count[z] ans %= 10**9 + 7 for x in range(101): z = target - 2 * x if 0 <= z <= 100 and x != z: ans += count[x] * (count[x] - 1) // 2 * count[z] ans %= 10**9 + 7 if not target % 3: x = target // 3 if 0 <= x <= 100: ans += count[x] * (count[x] - 1) * (count[x] - 2) // 6 ans %= 10**9 + 7 return ans
CLASS_DEF VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: counter = collections.Counter(A) i, res, l, ckey = 0, 0, len(counter), sorted(list(counter.keys())) if target % 3 == 0: res += math.comb(counter[target // 3], 3) for i in range(l): ni = ckey[i] nk = target - 2 * ni if ni != nk and nk >= 0: res += math.comb(counter[ni], 2) * counter[nk] for i in range(l): for j in range(i + 1, l): ni, nj = ckey[i], ckey[j] nk = target - ni - nj if ni < nj < nk <= 100: res += counter[ni] * counter[nj] * counter[nk] return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: c = collections.Counter(A) res = 0 for i, j in itertools.combinations_with_replacement(c, 2): k = target - i - j if i == j == k: res += c[i] * (c[i] - 1) * (c[i] - 2) / 6 elif i == j != k: res += c[i] * (c[i] - 1) / 2 * c[k] elif k > i and k > j: res += c[i] * c[j] * c[k] return int(res % (10**9 + 7))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: res = 0 mod = int(1000000000.0 + 7) numbers = dict() sums = dict() for num in A: res = (res + sums.get(target - num, 0)) % mod for number, count in list(numbers.items()): sums[number + num] = sums.get(number + num, 0) + count numbers[num] = numbers.get(num, 0) + 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: count = collections.Counter(A) valuelist = list(count.keys()) valuelist.sort() res = 0 for i in range(len(valuelist)): for j in range(i, len(valuelist)): a = valuelist[i] b = valuelist[j] c = target - a - b if c not in count: continue if a == b == c: res += count[a] * (count[a] - 1) * (count[a] - 2) // 6 elif a == b and b != c: res += count[a] * (count[a] - 1) // 2 * count[c] elif a < b and b < c: res += count[a] * count[b] * count[c] return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: A.sort() N = len(A) ans = 0 counts = self.get_counts(A) for i in range(N - 2): two_sum_target = target - A[i] counts[A[i]] -= 1 ans += self.two_sum(A, i + 1, two_sum_target, counts) return ans % (10**9 + 7) def two_sum(self, A, i, target, counts): j = len(A) - 1 ans = 0 while i < j: if A[i] + A[j] < target: i += counts[A[i]] elif A[i] + A[j] > target: j -= counts[A[j]] else: if A[i] == A[j]: v = counts[A[i]] ans += v * (v - 1) // 2 else: v1 = counts[A[i]] v2 = counts[A[j]] ans += v1 * v2 i += counts[A[i]] j -= counts[A[j]] return ans def get_counts(self, A): d = {} for num in A: if num not in d: d[num] = 0 d[num] += 1 return d
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: def find_n_sum(arr, target, N, l, r, path, output): if arr[l] * N > target or arr[r] * N < target: return if N == 2: s, e = l, r while s < e: total = arr[s] + arr[e] if total < target or s > l and arr[s - 1] == arr[s]: s += 1 elif total > target or e < r and arr[e] == arr[e + 1]: e -= 1 else: output.add(tuple(path + [arr[s], arr[e]])) s += 1 e -= 1 else: for i in range(l, r + 1 - (N - 1)): if i > l and arr[l] == arr[l - 1]: continue find_n_sum( arr, target - arr[i], N - 1, i, r, path + [arr[i]], output ) combos = set() res = 0 counter = Counter(A) new_arr = [] for k in sorted(counter.keys()): new_arr += [k] * min(counter[k], 3) find_n_sum(new_arr, target, 3, 0, len(new_arr) - 1, [], combos) for lst in combos: cnt = 1 temp = Counter(lst) for k in temp: for i in range(temp[k]): cnt *= (counter[k] - i) / (i + 1) res = (res + cnt) % (10**9 + 7) return int(res) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN IF VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP LIST VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER LIST VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: d1 = collections.defaultdict(int) d2 = collections.defaultdict(int) N = len(A) MOD = 10**9 + 7 cnt = 0 for i in range(N - 1, -1, -1): n = A[i] cnt += d2[target - n] cnt %= MOD for nn, nncnt in list(d1.items()): d2[nn + n] += nncnt d1[n] += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: MOD = 1000000007 count = [0] * 101 for x in A: count[x] += 1 ans = 0 for x in range(101): for y in range(x + 1, 101): z = target - x - y if y < z <= 100: ans += count[x] * count[y] * count[z] ans %= MOD for x in range(101): z = target - 2 * x if x < z <= 100: ans += count[x] * (count[x] - 1) // 2 * count[z] ans %= MOD for x in range(101): if (target - x) % 2 == 0: y = (target - x) // 2 if x < y <= 100: ans += count[x] * count[y] * (count[y] - 1) // 2 ans %= MOD if target % 3 == 0: x = target // 3 if 0 <= x <= 100: ans += count[x] * (count[x] - 1) * (count[x] - 2) // 6 ans %= MOD return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: ADict = {} A = sorted(A) print(A) for a in A: if a in ADict: ADict[a] += 1 else: ADict[a] = 1 ijSame = False keys = sorted(ADict) totWays = 0 for i in range(len(keys)): if keys[i] > target: break elif ADict[keys[i]] < 3: continue elif keys[i] * 3 == target: numOcc = ADict[keys[i]] totWays += numOcc * (numOcc - 1) * (numOcc - 2) / 6 if len(keys) == 1: return int(totWays % (10**9 + 7)) for i in range(len(keys)): if keys[i] > target: break elif ADict[keys[i]] < 2: continue else: sum = keys[i] * 2 remain = target - sum if not remain == keys[i] and remain in keys: numDoubleOcc = ADict[keys[i]] numSingleOcc = ADict[remain] totWays += numDoubleOcc * (numDoubleOcc - 1) / 2 * numSingleOcc if len(keys) == 2: return int(totWays % (10**9 + 7)) for i in range(len(keys)): if keys[i] > target: break for j in range(i + 1, len(keys)): sum = keys[i] + keys[j] if sum > target: break remaining = target - sum if remaining in keys[j + 1 :]: totWays += ADict[keys[i]] * ADict[keys[j]] * ADict[remaining] return int(totWays % (10**9 + 7))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSum(self, A: List[int], target: int) -> List[List[int]]: triples = [] i = 0 k = len(A) - 1 while i <= k: j = i k = len(A) - 1 while j <= k: if A[j] + A[k] < target - A[i]: j += 1 elif A[j] + A[k] > target - A[i]: k -= 1 else: triples.append([A[i], A[j], A[k]]) j += 1 k -= 1 i += 1 return triples def threeSumMulti(self, A: List[int], target: int) -> int: res = 0 count = {} for num in A: count[num] = count.get(num, 0) + 1 sorted_A_no_dup = sorted([num for num in count]) all_triples = self.threeSum(sorted_A_no_dup, target) for triple in all_triples: a, b, c = triple[0], triple[1], triple[2] if a != b and b != c: res += count[a] * count[b] * count[c] elif a == b and b != c: res += count[a] * (count[a] - 1) * count[c] // 2 elif a != b and b == c: res += count[a] * count[b] * (count[b] - 1) // 2 elif a == b and b == c: res += count[a] * (count[a] - 1) * (count[a] - 2) // 6 res %= 10**9 + 7 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: A.sort() leftDict = defaultdict(int) rightDict = Counter(A[1:]) ALen = len(A) MOD = 10**9 + 7 leftDict[A[0]] += 1 ans = 0 for i in range(1, ALen - 1): Ai = A[i] rightDict[Ai] -= 1 for left in leftDict: ans += leftDict[left] * rightDict[target - Ai - left] leftDict[Ai] += 1 ans %= MOD return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: d = Counter(A) keys = sorted(d.keys()) count = 0 for i, a in enumerate(keys): if a <= target // 3: for b in keys[i + 1 :]: if target - a - b <= b: break elif target - a - b in d: count += d[a] * d[b] * d[target - a - b] else: break for k in keys: if target != 3 * k and target - 2 * k in d: count += math.comb(d[k], 2) * d[target - 2 * k] if 3 * (target // 3) == target: count += math.comb(d[target // 3], 3) return count % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR IF VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A, target: int) -> int: M = 1000000007 record = {} for i in A: if i in record.keys(): record[i] = record[i] + 1 else: record[i] = 1 k_list = list(record.keys()) k_list.sort() n = len(k_list) res = 0 for i in range(n): for j in range(i, n): residual = target - k_list[i] - k_list[j] if residual < k_list[j]: continue if i == j: if record[k_list[i]] == 1: continue elif residual in record.keys(): if residual == k_list[j]: res = res + int( record[k_list[i]] * (record[k_list[i]] - 1) * (record[k_list[i]] - 2) / 6 ) res = res % M else: multi = record[residual] res = res + int( record[k_list[i]] * (record[k_list[i]] - 1) / 2 * multi ) res = res % M else: continue elif residual in record.keys(): if residual == k_list[j]: res = res + int( record[k_list[i]] * record[k_list[j]] * (record[k_list[j]] - 1) / 2 ) res = res % M else: multi = record[residual] res = res + int(record[k_list[i]] * record[k_list[j]] * multi) res = res % M else: continue return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: counter = [0] * 101 for num in A: counter[num] += 1 count = 0 MOD = int(10**9 + 7) for i in range(len(A) - 2): counter[A[i]] -= 1 residual = target - A[i] for Aj in range(101): Ak = residual - Aj if Ak >= 0 and Ak <= 100 and Aj <= Ak: if Aj != Ak: count += counter[Aj] * counter[Ak] else: count += counter[Aj] * (counter[Aj] - 1) // 2 return count % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def threeSumMulti(self, A: List[int], target: int) -> int: cnt1 = collections.Counter() cnt2 = collections.Counter() result = 0 M = 10**9 + 7 for a in A: cnt2[a] += 1 for a in A: cnt2[a] -= 1 if cnt2[a] == 0: cnt2.pop(a) for i, j in cnt1.items(): k = target - i - a result = (result + j * cnt2[k]) % M cnt1[a] += 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < k and A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 109 + 7.   Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (A[i], A[j], A[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times. Example 2: Input: A = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: A[i] = 1, A[j] = A[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.   Constraints: 3 <= A.length <= 3000 0 <= A[i] <= 100 0 <= target <= 300
class Solution: def calculate_weight(self, x, y, z, freq): if x == y and y == z: return freq[x] * (freq[x] - 1) * (freq[x] - 2) // 6 if x == y: return freq[x] * (freq[x] - 1) // 2 * freq[z] if x == z: return freq[x] * (freq[x] - 1) // 2 * freq[y] if y == z: return freq[z] * (freq[z] - 1) // 2 * freq[x] return freq[x] * freq[y] * freq[z] def calculate_freq(self, A): freq = {} for k in A: if k in list(freq.keys()): freq[k] += 1 else: freq[k] = 1 return freq def threeSumMulti(self, A: List[int], target: int) -> int: freq = self.calculate_freq(A) unique_values = list(freq.keys()) count = 0 two_sum_combinations = {} for x in unique_values: for y in unique_values: maxval = max(x, y) minval = min(x, y) if x + y not in two_sum_combinations: two_sum_combinations[x + y] = set() two_sum_combinations[x + y].add((maxval, minval)) for z in unique_values: if target - z in two_sum_combinations: for x, y in two_sum_combinations[target - z]: if z <= y: weight = self.calculate_weight(x, y, z, freq) print((x, y, z, weight)) count += weight return count % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): if len(nums) < 2 or k <= 0 or t < 0: return False if t == 0: visited = set() for i, n in enumerate(nums): if n in visited: return True visited.add(n) if i >= k: visited.remove(nums[i - k]) return False bucket = {} for i, n in enumerate(nums): b = n // t if b in bucket: return True if b + 1 in bucket and abs(bucket[b + 1] - n) <= t: return True if b - 1 in bucket and abs(bucket[b - 1] - n) <= t: return True bucket[b] = n if i >= k: del bucket[nums[i - k] // t] return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): if t < 0: return False n = len(nums) buckets = {} w = t + 1 for i in range(n): bucket_num = nums[i] // w if bucket_num in buckets: return True if bucket_num - 1 in buckets and abs(nums[i] - buckets[bucket_num - 1]) < w: return True if bucket_num + 1 in buckets and abs(nums[i] - buckets[bucket_num + 1]) < w: return True buckets[bucket_num] = nums[i] if i >= k: del buckets[nums[i - k] // w] return False
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): if len(nums) == 0 or len(nums) == 1 or k < 1 or t < 0: return False d = collections.OrderedDict() for i in nums: key = i if not t else i // t for j in (d.get(key - 1), d.get(key), d.get(key + 1)): if j != None and abs(j - i) <= t: return True if len(d) == k: d.popitem(False) d[key] = i return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NONE FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): buckets = {} for i, v in enumerate(nums): bucket_num, offset = (v // t, 1) if t else (v, 0) for idx in range(bucket_num - offset, bucket_num + offset + 1): if idx in buckets and abs(buckets[idx] - nums[i]) <= t: return True buckets[bucket_num] = nums[i] if len(buckets) > k: del buckets[nums[i - k] // t if t else nums[i - k]] return False
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): nums2 = [(n, i) for i, n in enumerate(nums)] nums2 = sorted(nums2) print(nums2) for i in range(1, len(nums2)): j = i - 1 a, n = nums2[j] b, m = nums2[i] while abs(a - b) <= t: if abs(n - m) <= k: return True j -= 1 if j < 0: break a, n = nums2[j] return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): if k < 1 or t < 0: return False m = len(nums) if m == 0: return False buckets = {} _min = 0 for i in range(m): num = nums[i] - _min bucket = num // (t + 1) flag = ( bucket in buckets or bucket - 1 in buckets and abs(buckets[bucket - 1] - num) <= t or bucket + 1 in buckets and abs(buckets[bucket + 1] - num) <= t ) if flag: return True if len(buckets) >= k: del buckets[(nums[i - k] - _min) // (t + 1)] buckets[bucket] = num return False
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
class Solution: def equalSol(self, nums, k): if k == 0: return False d = {} for i in range(len(nums)): if nums[i] in d.keys() and abs(i - d[nums[i]]) <= k: return True else: d[nums[i]] = i return False def containsNearbyAlmostDuplicate(self, nums, k, t): if k == 0: return False if t == 0: return self.equalSol(nums, k) for i in range(len(nums)): for j in range(1, k + 1): if i + j < len(nums) and abs(nums[i] - nums[i + j]) <= t: return True if i - j >= 0 and abs(nums[i] - nums[i - j]) <= t: return True return False
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true Example 2: Input: nums = [1,0,1,1], k = 1, t = 2 Output: true Example 3: Input: nums = [1,5,9,1,5,9], k = 2, t = 3 Output: false
import sys class Solution(object): def containsNearbyAlmostDuplicate(self, nums, k, t): if k < 1 or t < 0: return False import sys bh = {} w = t + 1 for i in range(len(nums)): n_i = nums[i] n_i_w = n_i // w if n_i_w in bh: return True if n_i_w - 1 in bh and abs(n_i - bh[n_i_w - 1]) < w: return True if n_i_w + 1 in bh and abs(n_i - bh[n_i_w + 1]) < w: return True bh[n_i_w] = nums[i] if len(bh) > k: del bh[nums[i - k] // w] return False
IMPORT CLASS_DEF VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IMPORT ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string. Example 1: Input: s = "abpcplea", d = ["ale","apple","monkey","plea"] Output: "apple" Example 2: Input: s = "abpcplea", d = ["a","b","c"] Output: "a" Note: All the strings in the input will only contain lower-case letters. The size of the dictionary won't exceed 1,000. The length of all the strings in the input won't exceed 1,000.
class Solution: def findLongestWord(self, s, d): def isMatch(s, w): index = 0 for c in w: if c in s: index = s.find(c, index) + 1 if index == 0: return False else: return False return True candidates = [] for word in d: if isMatch(s, word): candidates.append(word) candidates = sorted(candidates, key=lambda w: (len(w), w)) if not candidates: return "" else: maxLen = len(candidates[-1]) for c in candidates: if len(c) == maxLen: return c
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string. Example 1: Input: s = "abpcplea", d = ["ale","apple","monkey","plea"] Output: "apple" Example 2: Input: s = "abpcplea", d = ["a","b","c"] Output: "a" Note: All the strings in the input will only contain lower-case letters. The size of the dictionary won't exceed 1,000. The length of all the strings in the input won't exceed 1,000.
class Solution: def findLongestWord(self, s, d): def find(a, b): p = 0 for c in b: p = a.find(c, p) + 1 if p == 0: return False return True ans = "" for word in d: if find(s, word): if len(word) > len(ans): ans = word elif len(word) == len(ans) and word < ans: ans = word return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string. Example 1: Input: s = "abpcplea", d = ["ale","apple","monkey","plea"] Output: "apple" Example 2: Input: s = "abpcplea", d = ["a","b","c"] Output: "a" Note: All the strings in the input will only contain lower-case letters. The size of the dictionary won't exceed 1,000. The length of all the strings in the input won't exceed 1,000.
class Solution: def findLongestWord(self, s, d): word_dict = collections.defaultdict(list) for i, word in enumerate(d): word_dict[word[0]].append((i, word)) res = "" for c in s: words_startswithc = word_dict[c] word_dict[c] = [] for i, word in words_startswithc: if len(word) == 1: if len(d[i]) == len(res): res = min(res, d[i]) else: res = d[i] if len(d[i]) > len(res) else res else: word_dict[word[1]].append((i, word[1:])) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST FOR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string. Example 1: Input: s = "abpcplea", d = ["ale","apple","monkey","plea"] Output: "apple" Example 2: Input: s = "abpcplea", d = ["a","b","c"] Output: "a" Note: All the strings in the input will only contain lower-case letters. The size of the dictionary won't exceed 1,000. The length of all the strings in the input won't exceed 1,000.
class Solution: def findLongestWord(self, s, d): result = "" for word in d: lo = 0 for l in word: lo = s.find(l, lo) + 1 if lo == 0: break if lo > 0 and len(word) >= len(result): if len(word) == len(result): result = word if word < result else result else: result = word return result
CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
DZY has a sequence a, consisting of n integers. We'll call a sequence a_{i}, a_{i} + 1, ..., a_{j} (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The next line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- In a single line print the answer to the problem — the maximum length of the required subsegment. -----Examples----- Input 6 7 2 3 1 5 6 Output 5 -----Note----- You can choose subsegment a_2, a_3, a_4, a_5, a_6 and change its 3rd element (that is a_4) to 4.
n = int(input()) a = list(map(int, input().split())) fr = [0] * n bck = [0] * n fr[0] = 1 bck[n - 1] = 1 ans = 1 if n == 1: print(ans) else: for i in range(1, n): if a[i] <= a[i - 1]: fr[i] = 1 else: fr[i] += fr[i - 1] + 1 for i in range(n - 2, -1, -1): if a[i] >= a[i + 1]: bck[i] = 1 else: bck[i] += bck[i + 1] + 1 ans = max(fr) ans = max(bck[1] + 1, ans) ans = max(ans, fr[n - 2] + 1) for i in range(1, n - 1): if i + 1 < n and i - 1 >= 0: if a[i + 1] - a[i - 1] > 1: ans = max(fr[i - 1] + bck[i + 1] + 1, ans) else: ans = max(fr[i - 1] + 1, ans) ans = max(bck[i + 1] + 1, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
DZY has a sequence a, consisting of n integers. We'll call a sequence a_{i}, a_{i} + 1, ..., a_{j} (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The next line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- In a single line print the answer to the problem — the maximum length of the required subsegment. -----Examples----- Input 6 7 2 3 1 5 6 Output 5 -----Note----- You can choose subsegment a_2, a_3, a_4, a_5, a_6 and change its 3rd element (that is a_4) to 4.
import sys def inputArray(): tp = str(input()).split() return list(map(int, tp)) n = int(input()) input = inputArray() if n == 1: print("1") sys.exit() left = [(1) for x in range(n)] right = [(1) for x in range(n)] for i in range(1, n, 1): if input[i - 1] < input[i]: left[i] = left[i - 1] + 1 for i in range(n - 2, -1, -1): if input[i] < input[i + 1]: right[i] = right[i + 1] + 1 ans = 1 for i in range(n): if i == 0: ans = max(ans, right[i + 1] + 1) continue if i == n - 1: ans = max(ans, left[i - 1] + 1) continue ans = max(ans, right[i + 1] + 1) ans = max(ans, left[i - 1] + 1) if input[i + 1] - input[i - 1] >= 2: ans = max(ans, left[i - 1] + 1 + right[i + 1]) print(ans)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
DZY has a sequence a, consisting of n integers. We'll call a sequence a_{i}, a_{i} + 1, ..., a_{j} (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The next line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- In a single line print the answer to the problem — the maximum length of the required subsegment. -----Examples----- Input 6 7 2 3 1 5 6 Output 5 -----Note----- You can choose subsegment a_2, a_3, a_4, a_5, a_6 and change its 3rd element (that is a_4) to 4.
n = int(input()) l = list(map(int, input().split())) i, j = 0, 0 used_flip = False max_length = 1 prev_value = l[0] while i < n: if i - 1 >= 0 and l[i - 1] >= l[i]: used_flip = False prev_value = l[j] elif i + 1 < n and l[i + 1] <= l[i]: used_flip = True j, prev_value = i + 1, l[i + 1] max_length = max(max_length, 2) while j + 1 < n: if l[j + 1] > prev_value: prev_value = l[j + 1] j = j + 1 elif not used_flip: if i == j or l[j + 1] - l[j - 1] >= 2: prev_value = l[j + 1] j, used_flip = j + 1, True else: prev_value = l[j] + 1 j, used_flip = j + 1, True else: break max_length = max(max_length, j - i + 1) i += 1 print(max_length)
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 NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
DZY has a sequence a, consisting of n integers. We'll call a sequence a_{i}, a_{i} + 1, ..., a_{j} (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The next line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- In a single line print the answer to the problem — the maximum length of the required subsegment. -----Examples----- Input 6 7 2 3 1 5 6 Output 5 -----Note----- You can choose subsegment a_2, a_3, a_4, a_5, a_6 and change its 3rd element (that is a_4) to 4.
n = int(input()) sequence = list(map(int, input().split())) max_length = 0 def divide_sequences(): global max_length result = [] cur_sequence = [] def add_sequence_to_result(): global max_length result.append(cur_sequence) max_length = max(len(cur_sequence), max_length) last_elem = 0 for k in range(len(sequence)): if sequence[k] > last_elem: cur_sequence.append(sequence[k]) else: add_sequence_to_result() cur_sequence = list() cur_sequence.append(sequence[k]) last_elem = sequence[k] if len(cur_sequence) > 0: add_sequence_to_result() return result divided_sequences = divide_sequences() if len(divided_sequences) > 1: max_length += 1 def can_merge(seq_one_index, seq_two_index): seq_one = divided_sequences[seq_one_index] seq_two = divided_sequences[seq_two_index] if len(seq_one) == 1 or len(seq_two) == 1: return True if seq_one[-2] + 1 < seq_two[0] or seq_one[-1] + 1 < seq_two[1]: return True return False divided_sequences_length = len(divided_sequences) def can_merge_three(seq_one_index, seq_two_index): seq_one = divided_sequences[seq_one_index] seq_two = divided_sequences[seq_two_index] return seq_two[0] - seq_one[-1] > 0 for k in range(1, divided_sequences_length): if can_merge(k - 1, k): max_length = max( max_length, len(divided_sequences[k]) + len(divided_sequences[k - 1]) ) if ( len(divided_sequences[k]) == 1 and k < divided_sequences_length - 1 and can_merge_three(k - 1, k + 1) ): max_length = max( max_length, len(divided_sequences[k + 1]) + len(divided_sequences[k - 1]) + 1, ) print(max_length)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR