description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): new_array = [] new_array2 = [] freq_hash = {} for value in A1: freq_hash[value] = freq_hash.get(value, 0) + 1 for value in A2: if freq_hash.get(value, 0) != 0: for i in range(freq_hash[value]): new_array.append(value) freq_hash.pop(value) for key in freq_hash.keys(): for i in range(freq_hash[key]): new_array2.append(key) new_array2.sort() new_array += new_array2 return new_array return []
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN LIST
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, arr1, n, arr2, m): d = {} for i in arr1: if i in d: d[i] += 1 else: d[i] = 1 res = [] for i in arr2: if i in d: for j in range(d[i]): res.append(i) del d[i] rem_l = [] for i in d: for j in range(d[i]): rem_l.append(i) rem_l.sort() res = res + rem_l return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): result2 = [] result = [] A1_dict = {} for i in range(len(A1)): if A1[i] not in A1_dict: A1_dict[A1[i]] = 1 else: A1_dict[A1[i]] += 1 for i in range(len(A2)): if A2[i] in A1_dict: v = A1_dict[A2[i]] for j in range(v): result.append(A2[i]) A1_dict[A2[i]] = 0 for key, value in A1_dict.items(): if value != 0: v = A1_dict[key] for i in range(v): result2.append(key) result2.sort() for i in result2: result.append(i) return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): d = {} for i in A1: if i not in d: d[i] = 1 else: d[i] += 1 l = [] for i in A2: if i in d: l.extend([i] * d[i]) s = set(A2) k = [] for i in d: if i not in s: k.extend([i] * d[i]) return l + sorted(k)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): d = {} for i in A1: if i not in d: d[i] = 1 else: d[i] += 1 l = [] for i in A2: if i in d: for _ in range(d[i]): l += [i] del d[i] l1 = [] for i in d: for j in range(d[i]): l1 += [i] l1.sort() l += l1 return l
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR LIST VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): hmap = {} for i in range(N): if A1[i] in hmap: hmap[A1[i]] += 1 else: hmap[A1[i]] = 1 ans = [] for i in range(M): if A2[i] in hmap: ans += hmap[A2[i]] * [A2[i]] hmap[A2[i]] = 0 myKeys = list(hmap.keys()) myKeys.sort() for i in myKeys: if i != 0: ans += [i] * hmap[i] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR BIN_OP LIST VAR VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): d = {} for i in A1: d[i] = d.get(i, 0) + 1 a1 = set(A1) a2 = set(A2) a = a1.intersection(a2) b = a1 - a B = [] for i in b: B.extend([i] * d[i]) B.sort() ret = [] for i in A2: if i in a: a.remove(i) ret.extend([i] * d[i]) ret.extend(B) return ret
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): hashmap1 = {} for j in range(M): if A2[j] not in hashmap1: hashmap1[A2[j]] = 1 else: hashmap1[A2[j]] += 1 A1_dummy = [] hashmap = {} n = 0 for i in range(N): if A1[i] not in hashmap: hashmap[A1[i]] = 1 else: hashmap[A1[i]] += 1 if A1[i] not in hashmap1: A1_dummy.append(A1[i]) n += 1 result = [] for j in range(M): if A2[j] in hashmap: result += [A2[j]] * hashmap[A2[j]] N -= hashmap[A2[j]] self.HeapSort(A1_dummy, n) result += A1_dummy return result def heapify(self, arr, n, i): smallest = i left = 2 * i + 1 right = 2 * i + 2 if left < n and arr[left] < arr[smallest]: smallest = left if right < n and arr[right] < arr[smallest]: smallest = right if smallest != i: arr[i], arr[smallest] = arr[smallest], arr[i] self.heapify(arr, n, smallest) def buildHeap(self, arr, n): for i in range(n // 2 - 1, -1, -1): self.heapify(arr, n, i) return def HeapSort(self, arr, n): if n == 0: return arr res = [] self.buildHeap(arr, n) while n > 1: arr[0], arr[n - 1] = arr[n - 1], arr[0] res.append(arr.pop()) n -= 1 self.heapify(arr, n, 0) res.append(arr.pop()) arr[:] = res[:] return
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, arr1, N, arr2, M): indexHash = {} def getPositionInArr2(a): if a in indexHash: return indexHash[a] return 10**5 + a for i, a in enumerate(arr2): indexHash[a] = i return sorted(arr1, key=getPositionInArr2)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN BIN_OP BIN_OP NUMBER NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): k = [] l = {} s = {} for i in A2: s[i] = 1 for i in A1: if s.get(i, 0) == 1: l[i] = l.get(i, 0) + 1 else: k.append(i) ans = [] for i in A2: if l.get(i, -1) != -1: ans += [i] * l[i] k.sort() ans += k return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M, priority=None): sA1 = [] sA2 = [] if N == 0 or N == 1: return A1 if priority == None: priority = {} for i in range(M): if A2[i] not in priority: priority[A2[i]] = len(priority) pivot = A1[0] if pivot in priority: for i in range(1, N): if A1[i] in priority: if priority[A1[i]] < priority[pivot]: sA1.append(A1[i]) else: sA2.append(A1[i]) else: sA2.append(A1[i]) else: for i in range(1, N): if A1[i] in priority: sA1.append(A1[i]) elif A1[i] < pivot: sA1.append(A1[i]) else: sA2.append(A1[i]) ob = Solution() r1 = ob.relativeSort(sA1, len(sA1), A2, M, priority) r1.append(pivot) r2 = ob.relativeSort(sA2, len(sA2), A2, M, priority) return r1 + r2
CLASS_DEF FUNC_DEF NONE ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NONE ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): freq = {} a = [] temp = 0 for i in A1: freq[i] = freq.get(i, 0) + 1 for j in A2: if j in freq: a.extend(freq[j] * [j]) freq[j] = 0 l = len(a) b = [] for i in freq: if freq[i] != 0: b.extend([i] * freq[i]) a = a + sorted(b) return a
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): count = [0] * (max(A1) + 1) result = [] for num in A1: count[num] += 1 for num in A2: while count[num] > 0: result.append(num) count[num] -= 1 for num in range(len(count)): while count[num] > 0: result.append(num) count[num] -= 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): h = dict() for i in A1: if str(i) in h.keys(): h[str(i)] += 1 else: h.update({str(i): 1}) s_arr = [] for i in A2: flag = False try: for j in range(h[str(i)]): s_arr.append(i) flag = True if flag == False: s_arr.append(i) h[str(i)] = 0 except Exception as e: continue x = [] for i in h.keys(): if h[i] != 0: for j in range(h[i]): x.append(int(i)) x.sort() for i in x: s_arr.append(i) return s_arr
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR DICT FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): dict_freq = {} for x in A1: if x in dict_freq: dict_freq[x] += 1 else: dict_freq[x] = 1 result = [] for x in A2: if x in dict_freq: freq = dict_freq[x] result += [x] * freq del dict_freq[x] remaining = sorted(dict_freq.keys()) for x in remaining: freq = dict_freq[x] result += [x] * freq return result
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): d = {} for i in A1: if i in d: d[i] += 1 else: d[i] = 1 ans = [] an = [] arr = set(A1) - set(A2) for i in arr: if i in d: an.extend([i] * d[i]) for i in A2: if i in d: ans.extend([i] * d[i]) ll = list(an) ll.sort() ans.extend(ll) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): dict1 = {} dict2 = {} ans = [] for i in A2: dict1[i] = True for i in A1: if i in dict2: dict2[i] += 1 else: dict2[i] = 1 A1.sort() for i in A2: if i in dict2: while dict2[i] > 0: ans.append(i) dict2[i] -= 1 for i in A1: if i not in dict1: ans.append(i) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): dic1 = {} for i in range(N): if A1[i] in dic1: dic1[A1[i]] += 1 else: dic1[A1[i]] = 1 A1.sort() a = [] for i in range(M): if A2[i] in dic1: a = a + [A2[i]] * dic1[A2[i]] dic1.pop(A2[i]) for i in A1: if i in dic1: a.append(i) return a
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two integer arrays A1[ ] and A2[ ] of size N and M respectively. Sort the first array A1[ ] such that all the relative positions of the elements in the first array are the same as the elements in the second array A2[ ]. See example for better understanding. Note: If elements are repeated in the second array, consider their first occurance only. Example 1: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3} Output: 2 2 1 1 8 8 3 5 6 7 9 Explanation: Array elements of A1[] are sorted according to A2[]. So 2 comes first then 1 comes, then comes 8, then finally 3 comes, now we append remaining elements in sorted order. Example 2: Input: N = 11 M = 4 A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {99, 22, 444, 56} Output: 1 1 2 2 3 5 6 7 8 8 9 Explanation: No A1[] elements are in A2[] so we cannot sort A1[] according to A2[]. Hence we sort the elements in non-decreasing order. Your Task: You don't need to read input or print anything. Your task is to complete the function sortA1ByA2() which takes the array A1[ ], array A2[ ] and their respective size N and M as input parameters and returns the sorted array A1[ ] such that the relative positions of the elements in A1[ ] are same as the elements in A2[ ]. For the elements not present in A2[ ] but in A1[ ], it appends them at the last in increasing order. Expected Time Complexity: O(N * Log(N)). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N, M ≤ 10^{6} 1 ≤ A1[i], A2[i] ≤ 10^{6}
class Solution: def relativeSort(self, A1, N, A2, M): def bin(lb, ub, n, l): if lb <= ub: mid = (lb + ub) // 2 if l[mid] == n: return mid elif l[mid] < n: return bin(mid + 1, ub, n, l) else: return bin(lb, mid - 1, n, l) return -1 A1.sort() l = [] i = 0 while i < M: r = bin(0, N - 1, A2[i], A1) if r == -1: i += 1 else: l.append(A1.pop(r)) N -= 1 l.extend(A1) return l
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() def binarysearch(arr, target): start = 0 end = len(arr) - 1 ans = 0 while start < end: mid = (start + end) // 2 if arr[mid] == target: return mid elif arr[mid] < target: start = mid + 1 ans = mid + 1 else: end = mid - 1 return ans if arr[ans] >= target else ans + 1 opt = [] for i in range(len(arr1)): ans = binarysearch(arr2, arr1[i]) while ans < n2 and arr2[ans] == arr1[i]: ans += 1 opt.append(ans) return opt
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() ans = [] def lastIndex(num): start, end = 0, len(arr2) - 1 while start <= end: mid = start + (end - start) // 2 if arr2[mid] <= num: start = mid + 1 else: end = mid - 1 return start for num in arr1: ind = lastIndex(num) ans.append(ind) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, m, arr2, n): arr2.sort() result = [] for i in range(m): left, right = 0, n - 1 while left <= right: mid = left + (right - left) // 2 if arr2[mid] > arr1[i]: right = mid - 1 else: left = mid + 1 result.append(left) return result
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def binarysearch(self, arr2, n2, x): ans = -1 low = 0 high = n2 - 1 while low <= high: mid = (low + high) // 2 if arr2[mid] <= x: ans = mid low = mid + 1 elif arr2[mid] > x: high = mid - 1 return ans + 1 def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() lst = [] for i in range(n1): ans = self.binarysearch(arr2, n2, arr1[i]) lst.append(ans) return lst
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr = arr1.copy() arr1.sort() arr2.sort() dic = {} ans = [] prev = 0 i = 0 for elm in arr1: count = 0 while i < n2: if arr2[i] <= elm: count += 1 i += 1 else: prev = prev + count dic[elm] = prev break else: dic[elm] = n2 return [dic[elm] for elm in arr]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, a1, n1, a2, n2): a2.sort() ans = [] for i in range(n1): low = 0 high = n2 - 1 mid = (high + low) // 2 if a1[i] >= a2[high]: ans.append(n2) elif a1[i] < a2[low]: ans.append(0) else: while mid > 0 and mid < n2 - 1: if a1[i] >= a2[mid] and a1[i] < a2[mid + 1]: ans.append(mid + 1) break elif a1[i] < a2[mid]: high = mid mid = (high + low) // 2 else: low = mid mid = (high + low) // 2 if mid == 0 and a1[i] >= a2[0]: ans.append(1) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): r = [n2] * n1 arr2.sort() for i in range(n1): l = 0 h = n2 - 1 while l <= h: m = (l + h) // 2 if arr2[m] > arr1[i]: r[i] = m h = m - 1 elif arr2[m] <= arr1[i]: l = m + 1 return r
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() def search(x): low = 0 high = len(arr2) while low < high: mid = low + (high - low) // 2 if arr2[mid] < x: low = mid + 1 else: high = mid return low ans = [-1] * n1 for i in range(n1): k = search(arr1[i] + 1) - 1 ans[i] = k + 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): def search(arr, i): first = 0 last = len(arr) - 1 while first <= last: mid = (first + last) // 2 if arr[mid] == i: first = mid + 1 elif arr[mid] > i: last = mid - 1 else: first = mid + 1 return last + 1 res = [] arr2.sort() for i in arr1: res.append(search(arr2, i)) return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def upper_bound(self, arr, n, x): l = 0 h = n - 1 while l < h: m = l + (h - l) // 2 if arr[m] <= x: l = m + 1 else: h = m if arr[h] > x: return h if arr[l] > x: return l return -1 def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): ans = [] arr2.sort() for i in range(n1): val = self.upper_bound(arr2, n2, arr1[i]) ans.append(val if val != -1 else n2) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): ans = [] arr2.sort() for i in range(n1): count = 0 s = 0 e = n2 - 1 mid = 0 while s <= e: mid = (s + e) // 2 if arr2[mid] <= arr1[i]: s = mid + 1 else: e = mid - 1 ans.append(s) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): def bin(arr, n, x): s = 0 s1 = n - 1 while s <= s1: mid = s + (s1 - s) // 2 if arr[mid] <= x: s = mid + 1 else: s1 = mid - 1 return s1 arr2.sort() ans = [] for i in range(n1): pos = bin(arr2, n2, arr1[i]) ans.append(pos + 1) return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr = arr1.copy() arr1.sort() arr2.sort() d = {} c = j = 0 for i in arr1: while j < n2: if arr2[j] > i: break c += 1 j += 1 d[i] = c arr3 = [] for i in arr: arr3.append(d[i]) return arr3
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): res = [] arr2.sort() def binary(target, n2): low = 0 high = n2 - 1 ans = 0 while low <= high: mid = low + (high - low) // 2 if arr2[mid] > target: ans = mid high = mid - 1 else: if mid + 1 >= n2: ans = mid + 1 low = mid + 1 return ans for i in range(n1): index = binary(arr1[i], n2) res.append(index) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
def mgc(arr, x): st, en = 0, len(arr) - 1 ans = len(arr) while st <= en: mid = (st + en) // 2 if arr[mid] > x: ans = mid en = mid - 1 else: st = mid + 1 if x == len(arr) and arr[0] > x: return 0 return ans class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() ans = [] for i in arr1: ind = mgc(arr2, i) ans.append(ind) return ans
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): ans = [0] * n1 arr2.sort() for i in range(n1): temp = 0 low = 0 high = n2 - 1 while low <= high: mid = (low + high) // 2 if arr2[mid] <= arr1[i]: temp = max(temp, mid + 1) low = mid + 1 else: high = mid - 1 ans[i] = temp return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
def mgc(arr, x): st, en = 0, len(arr) - 1 ans = len(arr) while st <= en: mid = (st + en) // 2 if arr[mid] > x: ans = mid en = mid - 1 else: st = mid + 1 return ans class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): def ind(x, arr): low = 0 high = len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr2[mid] <= x: low = mid + 1 else: high = mid - 1 return low li = [] arr2.sort() for i in range(n1): s = ind(arr1[i], arr2) li.append(s) return li
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() result = [] for x in arr1: index = self.binarySearch(arr2, n2, x) result.append(index) return result def binarySearch(self, arr, n, x): left, right = 0, n - 1 while left <= right: mid = left + (right - left) // 2 if arr[mid] <= x: left = mid + 1 else: right = mid - 1 return left
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): m = [] l = [0] * (max(max(arr1), max(arr2)) + 1) for ele in arr2: l[ele] += 1 p = [] p.append(l[0]) for i in range(1, len(l)): p.append(p[i - 1] + l[i]) for ele in arr1: m.append(p[ele]) return m
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() ar = [] for i in arr1: l = 0 u = n2 - 1 ind = n2 while l <= u: m = (l + u) // 2 if arr2[m] > i: u = m - 1 else: l = m + 1 ar.append(l) return ar
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): copy = arr1[:] arr1.sort() arr2.sort() prev_count = 0 res = {} j = 0 for i in arr1: curr_count = 0 while j < len(arr2): if arr2[j] <= i: curr_count += 1 j += 1 else: break res[i] = curr_count + prev_count prev_count = curr_count + prev_count ans = [] for i in copy: ans.append(res[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def binary(self, k, arr2, end): start = 0 ans = -1 while start <= end: mid = start + (end - start) // 2 if arr2[mid] <= k: ans = mid start = mid + 1 else: end = mid - 1 return ans + 1 def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() for i in range(n1): arr1[i] = self.binary(arr1[i], arr2, n2 - 1) return arr1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): res = [0] * n1 arr2.sort() for i in range(n1): s = 0 e = n2 - 1 if n2 == 1: if arr1[i] >= arr2[0]: res[i] = 1 else: res[i] = 0 if arr1[i] >= arr2[n2 - 1]: res[i] = n2 else: while s <= e: m = (s + e) // 2 if arr2[m] > arr1[i] and arr2[m - 1] <= arr1[i]: res[i] = m break elif arr2[m] <= arr1[i]: s = m + 1 else: e = m - 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def Just_smaller(self, arr2, n, val): start = 0 end = n - 1 mx = -1 while start <= end: middle = (start + end) // 2 if arr2[middle] <= val: mx = middle start = middle + 1 else: end = middle - 1 return mx def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): ans = [] arr2.sort() for i in range(n1): index = self.Just_smaller(arr2, n2, arr1[i]) ans.append(index + 1) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() mini = arr2[0] maxi = arr2[n2 - 1] for i in range(n1): if arr1[i] < mini: arr1[i] = 0 elif arr1[i] > maxi: arr1[i] = n2 else: start = 0 end = n2 - 1 ans = 0 while start <= end: mid = start + (end - start) // 2 if arr2[mid] == arr1[i]: ans = mid start = mid + 1 elif arr2[mid] < arr1[i]: start = mid + 1 elif arr2[mid] > arr1[i]: end = mid - 1 else: arr1[i] = start if ans == 0: arr1[i] = start else: arr1[i] = ans + 1 return arr1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() for i in range(n1): key = arr1[i] l, h = 0, n2 - 1 index = -1 while l <= h: mid = (l + h) // 2 if arr2[mid] <= arr1[i]: index = mid l = mid + 1 elif arr2[mid] > arr1[i]: h = mid - 1 if index == -1: arr1[i] = 0 else: arr1[i] = index + 1 return arr1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): arr2.sort() def check(arr2, n2, i): low = 0 high = n2 - 1 while low <= high: mid = (low + high) // 2 if arr2[mid] >= i: high = mid - 1 else: low = mid + 1 return low def check2(arr2, n2, i): low = 0 high = n2 - 1 while low <= high: mid = (low + high) // 2 if arr2[mid] <= i: low = mid + 1 else: high = mid - 1 return low x = [] for i in arr1: x.append(check2(arr2, n2, i)) return x
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def solve(self, nums, target): l = 0 r = len(nums) - 1 san = len(nums) while l <= r: mid = (l + r) // 2 if nums[mid] <= target: l = mid + 1 else: r = mid - 1 san = mid else: return san def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): ans = [] arr2.sort() for i in arr1: ans.append(self.solve(arr2, i)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): s, sm_till, mx = 0, [], max(max(arr1), max(arr2)) freq = [0] * (mx + 1) for elem in arr2: freq[elem] += 1 for elem in freq: s += elem sm_till.append(s) return [sm_till[elem] for elem in arr1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def binsearch(self, arr, k, n): s = 0 e = n - 1 res = -1 while s <= e: m = (s + e) // 2 if arr[m] == k: res = m s = m + 1 if arr[m] < k: res = m s = m + 1 if arr[m] > k: e = m - 1 if res != -1: return res + 1 else: return 0 def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): ans = [] arr2.sort() for i in range(n1): ans.append(self.binsearch(arr2, arr1[i], n2)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): l = [] m = max(arr2) temp = [(0) for i in range(m + 1)] for i in arr2: temp[i] += 1 for i in range(1, m + 1): temp[i] += temp[i - 1] for i in arr1: if i < m + 1: l.append(temp[i]) else: l.append(temp[-1]) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, array1, n1, array2, n2): n1_size = max(array1) n2_size = max(array2) holder = [(0) for i in range(max(n1_size, n2_size) + 1)] for i in array2: holder[i] += 1 count = 0 for i in range(len(holder)): count += holder[i] holder[i] = count count = 0 for i in range(n1): array1[i] = holder[array1[i]] return array1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Example 1: Input: m = 6, n = 6 arr1[] = {1,2,3,4,7,9} arr2[] = {0,1,2,1,1,4} Output: 4 5 5 6 6 6 Explanation: Number of elements less than or equal to 1, 2, 3, 4, 7, and 9 in the second array are respectively 4,5,5,6,6,6 Example 2: Input: m = 5, n = 7 arr1[] = {4,8,7,5,1} arr2[] = {4,48,3,0,1,1,5} Output: 5 6 6 6 3 Explanation: Number of elements less than or equal to 4, 8, 7, 5, and 1 in the second array are respectively 5,6,6,6,3 Your Task : Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[], m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where i_{th} output represents the count for i_{th} element in arr1.) Expected Time Complexity: O((m + n) * log n). Expected Auxiliary Space: O(m). Constraints: 1<=m,n<=10^5 0<=arr1[i],arr2[j]<=10^5
class Solution: def countEleLessThanOrEqual(self, arr1, n1, arr2, n2): k = [] for i in arr1: k.append(i) arr1.sort() arr2.sort() d = {} j = 0 v = [] for i in range(n1): while j < n2 and arr2[j] <= arr1[i]: j += 1 d[arr1[i]] = j for i in range(n1): v.append(d[k[i]]) return v
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well. Chef has failed miserably in several cooking competitions, which makes him think that he's been out of luck recently. Therefore, he decided to build a magic circle using his N chopsticks (numbered 1 through N), hoping that it would somehow improve his luck. The magic circle consists of N chopsticks placed on an infinite straight line (so it's not actually a circle). The line is drawn on the ground and chopsticks should be stuck vertically into the ground. The chopsticks have lengths x_{1},x_{2},\cdots,x_{N}. For each valid i, the i-th chopstick can be placed on the line at any integer position y_{i}; then it casts a shadow on the range [y_{i},y_{i}+x_{i}] on the line. No two chopsticks may be placed at the same position. The positions of the chopsticks should satisfy an additional condition: for each pair of neighbouring chopsticks i and j (such that there are no other chopsticks between them), the distances between these neighbouring chopsticks | y_{i} - y_{j} \right| must fall in a given range [L,U]. The magic power of the circle depends on the total shadowed length S=| \bigcup_{i} [y_{i},y_{i}+x_{i}] \right|. While Chef expects the circle to have the maximum effect, he must prepare for the worst-case scenario — due to his recent bad luck. Please help Chef find both the minimum and the maximum value of S. ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains three space-separated integers N, L and U. - The second line contains N space-separated integers x_{1},x_{2},\cdots,x_{N}. ------ Output Format ------ For each test case, print a single line containing two space-separated integers S_{\min} and S_{\max}. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ L ≤ U ≤ 10^{9}$ $1 ≤ x_{i} ≤ 10^{9}$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^{5}$ ------ subtasks ------ Subtask #1 (5 points): $T ≤ 5$ $N ≤ 3$ Subtask #2 (15 points): $T ≤ 5$ $N ≤ 11$, and additionally $N > 5$ in at most one test case Subtask #3 (30 points): $N ≤ 200$ Subtask #4 (50 points): original constraints ----- Sample Input 1 ------ 3 2 2 4 8 5 3 1 2 1 2 3 5 3 5 1 9 2 6 6 ----- Sample Output 1 ------ 8 12 3 6 13 22 ----- explanation 1 ------ Example case 1: - To minimize $S$, we may set $y_{1}=0$ and $y_{2}=2$. - To maximize $S$, we may set $y_{1}=4$ and $y_{2}=0$. Example case 2: - To minimize $S$, we may set $y_{1}=2$, $y_{2}=1$ and $y_{3}=0$. - To maximize $S$, we may set $y_{1}=0$, $y_{2}=1$ and $y_{3}=3$. Example case 3: - To minimize $S$, we may set $y_{1}=0$, $y_{2}=3$, $y_{3}=12$, $y_{4}=6$ and $y_{5}=9$. - To maximize $S$, we may set $y_{1}=0$, $y_{2}=20$, $y_{3}=5$, $y_{4}=10$ and $y_{5}=15$.
T = int(input()) for t in range(T): N, L, U = map(int, input().split()) arr = sorted(list(map(int, input().split()))) mini = maxi = arr[-1] pref = min(L, arr[-1]) cnt, mod = 0, [] for i in range(N - 2, -1, -1): mini = max(mini, pref + arr[i]) pref = min(pref + L, mini) if arr[i] >= U: cnt += (arr[i] - U) // U mod.append(arr[i] % U) maxi += U mod.sort(reverse=True) ln = len(mod) right = min(cnt + ln, N - 1 - ln) maxi += min(cnt, N - 1 - ln) * U for i in range(cnt, right): maxi += max(mod[i - cnt], arr[i]) for i in range(right, N - 1 - ln): maxi += arr[i] print(mini, maxi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i = 0 j = 0 k = 0 m = 1000000000 ans = [0, 0, 0] while i < n and k < n and j < n: temp = [a[i], b[j], c[k]] if m > max(temp) - min(temp): ans = temp m = max(temp) - min(temp) if a[i] <= b[j] and a[i] <= c[k]: i += 1 elif b[j] <= c[k] and b[j] <= a[i]: j += 1 else: k += 1 ans.sort(reverse=True) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i_a = 0 i_b = 0 i_c = 0 min_diff_triplet = [-1] * 3 min_diff = 10**5 for x in range(3 * (n - 1) + 1): val_a, val_b, val_c = a[i_a], b[i_b], c[i_c] diff = max(val_a, val_b, val_c) - min(val_a, val_b, val_c) if diff < min_diff: min_diff_triplet = sorted([val_a, val_b, val_c], reverse=True) min_diff = diff next_a = a[i_a + 1] if i_a < n - 1 else 10**5 next_b = b[i_b + 1] if i_b < n - 1 else 10**5 next_c = c[i_c + 1] if i_c < n - 1 else 10**5 if next_a <= next_b and next_a <= next_c: i_a += 1 elif next_b <= next_a and next_b <= next_c: i_b += 1 elif next_c <= next_a and next_c <= next_b: i_c += 1 return min_diff_triplet
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() difference = float("inf") p1, p2, p3 = 0, 0, 0 x, y, z = 0, 0, 0 while p1 < n and p2 < n and p3 < n: mn = min(a[p1], b[p2], c[p3]) mx = max(a[p1], b[p2], c[p3]) diff = mx - mn if diff < difference: difference = diff x = a[p1] y = b[p2] z = c[p3] if a[p1] == mn: p1 += 1 elif b[p2] == mn: p2 += 1 else: p3 += 1 return sorted((x, y, z), reverse=True)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 op = [i for i in range(3)] minDiff = float("INF") while i < n and j < n and k < n: mi = min(a[i], b[j], c[k]) ma = max(a[i], b[j], c[k]) s = sum([a[i], b[j], c[k]]) d = ma - mi if mi == a[i]: i = i + 1 elif mi == b[j]: j = j + 1 elif mi == c[k]: k = k + 1 if d < minDiff: minDiff = d op[0] = ma op[2] = mi op[1] = s - (op[0] + op[2]) return op
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 mini = 100000000.0 while i < n and j < n and k < n: sum = a[i] + b[j] + c[k] mn = min(a[i], b[j], c[k]) mx = max(a[i], b[j], c[k]) if mn == a[i]: i += 1 elif mn == b[j]: j += 1 else: k += 1 if mini > mx - mn: mini = mx - mn a1 = mx a3 = mn a2 = sum - (mx + mn) ans = [a1, a2, a3] return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR VAR RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): def minimum(x, y, z): return min(min(x, y), z) def maximum(x, y, z): return max(max(x, y), z) res_min, res_max, res_mid = 0, 0, 0 a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 diff = float("inf") while i < n and j < n and k < n: sum = a[i] + b[j] + c[k] min_val = minimum(a[i], b[j], c[k]) max_val = maximum(a[i], b[j], c[k]) if min_val == a[i]: i += 1 elif min_val == b[j]: j += 1 else: k += 1 if max_val - min_val < diff: diff = max_val - min_val res_min = min_val res_max = max_val res_mid = sum - (max_val + min_val) return [res_max, res_mid, res_min]
CLASS_DEF FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 diff = 99999 while i < n and j < n and k < n: maxele = max(a[i], max(b[j], c[k])) minele = min(a[i], min(b[j], c[k])) if maxele - minele < diff: diff = maxele - minele ans = [a[i], b[j], c[k]] if minele == a[i]: i += 1 elif minele == b[j]: j += 1 else: k += 1 ans.sort(reverse=True) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
import sys class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i = j = k = 0 rdiff = rsum = sys.maxsize while i < n and j < n and k < n: maxnum = max(a[i], b[j], c[k]) minnum = min(a[i], b[j], c[k]) sum1 = a[i] + b[j] + c[k] midnum = sum1 - maxnum - minnum diff = maxnum - minnum if diff < rdiff: rdiff = diff resmax = maxnum resmin = minnum resmid = midnum if minnum == a[i]: i = i + 1 elif minnum == b[j]: j = j + 1 else: k = k + 1 return resmax, resmid, resmin
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 diff = 99999 while i < n and j < n and k < n: ma = max(a[i], b[j], c[k]) mi = min(a[i], b[j], c[k]) if ma - mi < diff: diff = ma - mi s = a[i] + b[j] + c[k] ans = ma, s - (ma + mi), mi if mi == a[i]: i += 1 elif mi == b[j]: j += 1 else: k += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i = 0 j = 0 k = 0 minm = float("inf") while i < n and j < n and k < n: triplet = [a[i], b[j], c[k]] currMin = min(triplet) currMax = max(triplet) diff = currMax - currMin if diff < minm: minm = diff curr = list(triplet) curr.sort(reverse=True) if currMin == a[i]: i += 1 elif currMin == b[j]: j += 1 elif currMin == c[k]: k += 1 return curr
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
def maximum(a, b, c): return max(max(a, b), c) def minimum(a, b, c): return min(min(a, b), c) class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 res_min, res_max, res_mid = 0, 0, 0 diff = 999999999 while i < n and j < n and k < n: sum = a[i] + b[j] + c[k] min = minimum(a[i], b[j], c[k]) max = maximum(a[i], b[j], c[k]) if min == a[i]: i += 1 elif b[j] == min: j += 1 else: k += 1 if diff > max - min: diff = max - min res_min = min res_max = max res_mid = sum - min - max return res_max, res_mid, res_min t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) ob = Solution() ans = ob.smallestDifferenceTriplet(a, b, c, n) print(ans[0], ans[1], ans[2])
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort(reverse=True) b.sort(reverse=True) c.sort(reverse=True) triplet = [0, 0, 0] diff = float("inf") while a and b and c: arr = [a[-1], b[-1], c[-1]] maxi = max(arr) mini = min(arr) if maxi - mini < diff: diff = maxi - mini triplet = arr if a[-1] == mini: a.pop() if b[-1] == mini: b.pop() if c[-1] == mini: c.pop() return sorted(triplet, reverse=True)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() result1 = 0 result2 = 0 result3 = 0 i = 0 j = 0 k = 0 diff = 100000 while i < n and j < n and k < n: sum = a[i] + b[j] + c[k] x = max(a[i], b[j], c[k]) y = min(a[i], b[j], c[k]) if y == a[i]: i += 1 elif y == b[j]: j += 1 else: k += 1 if diff > x - y: diff = x - y result3 = sum - (x + y) result2 = x result1 = y return result2, result3, result1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): x = y = z = p = q = r = 0 a.sort() b.sort() c.sort() diff = 100000 while p < n and q < n and r < n: mn = min(a[p], b[q], c[r]) mx = max(a[p], b[q], c[r]) d = mx - mn if d < diff: diff = d x = a[p] y = b[q] z = c[r] if mn == a[p]: p += 1 elif mn == b[q]: q += 1 else: r += 1 l = [] l.append(x) l.append(y) l.append(z) l.sort(reverse=True) return l
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def maximum(self, a, b, c): return max(max(a, b), c) def minimum(self, a, b, c): return min(min(a, b), c) def smallestDifferenceTriplet(self, arr1, arr2, arr3, n): arr1.sort() arr2.sort() arr3.sort() res_min = 0 res_max = 0 res_mid = 0 i = 0 j = 0 k = 0 diff = 50 while i < n and j < n and k < n: sum = arr1[i] + arr2[j] + arr3[k] max = self.maximum(arr1[i], arr2[j], arr3[k]) min = self.minimum(arr1[i], arr2[j], arr3[k]) if min == arr1[i]: i += 1 elif min == arr2[j]: j += 1 else: k += 1 if diff > max - min: diff = max - min res_max = max res_mid = sum - (max + min) res_min = min return [res_max, res_mid, res_min]
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): i, j, k = 0, 0, 0 prev_diff = None prev_sum = None a.sort() b.sort() c.sort() while i < n and j < n and k < n: tmp_a = a[i] tmp_b = b[j] tmp_c = c[k] triple_min = min(tmp_a, tmp_b, tmp_c) triple_max = max(tmp_a, tmp_b, tmp_c) triple_sum = sum([tmp_a, tmp_b, tmp_c]) curr_diff = triple_max - triple_min if ( prev_diff is None or prev_diff > curr_diff or prev_diff == curr_diff and prev_sum > triple_sum ): final_min = triple_min final_max = triple_max final_mid = triple_sum - final_min - final_max prev_diff = curr_diff prev_sum = triple_sum if triple_min == tmp_a: i += 1 elif triple_min == tmp_b: j += 1 else: k += 1 return [final_max, final_mid, final_min]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
import sys class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() x, y, z = a[0], b[0], c[0] res = sys.maxsize i, j, k = 0, 0, 0 while i < n and j < n and k < n: mini, maxi = min(a[i], b[j], c[k]), max(a[i], b[j], c[k]) diff = maxi - mini if diff < res: res = diff x, y, z = a[i], b[j], c[k] if a[i] == mini: i += 1 elif b[j] == mini: j += 1 else: k += 1 ans = [x, y, z] ans.sort(reverse=True) return ans
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
import sys class Solution: def smallestDifferenceTriplet(self, a, b, c, n): diff = sys.maxsize a.sort() b.sort() c.sort() ans = [] i = j = k = 0 while i < n and j < n and k < n: mini = min(a[i], b[j], c[k]) maxi = max(a[i], b[j], c[k]) sum = a[i] + b[j] + c[k] if mini == a[i]: i += 1 elif mini == b[j]: j += 1 else: k += 1 if diff > maxi - mini: diff = maxi - mini a1 = maxi a2 = sum - maxi - mini a3 = mini return [a1, a2, a3]
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() ma = float("inf") i, j, k = 0, 0, 0 while i < n and j < n and k < n: mi = min(a[i], b[j], c[k]) diff = max(a[i], b[j], c[k]) - mi if diff < ma: res = [a[i], b[j], c[k]] ma = diff if a[i] == mi: i = i + 1 elif b[j] == mi: j = j + 1 else: k = k + 1 res.sort() return res[::-1]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def binarysearch(self, arr, l, r, k): if l <= r: mid = (l + r) // 2 if arr[mid] <= k: return self.binarysearch(arr, mid + 1, r, k) else: return self.binarysearch(arr, l, mid - 1, k) return r def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() trip = [0, 100000000] for i in range(n): newtrip = [] l = self.binarysearch(b, 0, n - 1, a[i]) newtrip.append(a[i]) if l < n: if l < n - 1: if abs(b[l] - a[i]) > abs(b[l + 1] - a[i]): newtrip.append(b[l + 1]) else: newtrip.append(b[l]) else: newtrip.append(b[l]) l = self.binarysearch(c, 0, n - 1, a[i]) if l < n: if l < n - 1: if abs(c[l] - a[i]) > abs(c[l + 1] - a[i]): newtrip.append(c[l + 1]) else: newtrip.append(c[l]) else: newtrip.append(c[l]) if max(trip) - min(trip) > max(newtrip) - min(newtrip): trip = newtrip.copy() elif max(trip) - min(trip) == max(newtrip) - min(newtrip) and sum( trip ) > sum(newtrip): trip = newtrip.copy() trip.sort(reverse=True) return trip
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 I, J, K = -1, -1, -1 m = max(a[-1], b[-1], c[-1]) - min(a[0], b[0], c[0]) while i < n and j < n and k < n: temp = max(a[i], b[j], c[k]) - min(a[i], b[j], c[k]) if temp < m: m = temp I, J, K = i, j, k if a[i] <= b[j] and a[i] <= c[k]: i += 1 elif b[j] <= a[i] and b[j] <= c[k]: j += 1 else: k += 1 return sorted([a[I], b[J], c[K]], reverse=True)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i = 0 j = 0 k = 0 diff = 99999999999999 while i < n and j < n and k < n: sum1 = sum([a[i], b[j], c[k]]) min1 = min([a[i], b[j], c[k]]) max1 = max([a[i], b[j], c[k]]) if min1 == a[i]: i = i + 1 elif min1 == b[j]: j = j + 1 else: k = k + 1 if diff > max1 - min1: diff = max1 - min1 a1 = max1 b1 = sum1 - (max1 + min1) c1 = min1 return [a1, b1, c1]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
import sys class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() l1 = l2 = l3 = 0 sum = 0 diff = sys.maxsize maxi = 0 mini = 0 res_sum = a[0] + b[0] + c[0] res_max = 0 res_min = 0 res_mid = 0 while l1 < n and l2 < n and l3 < n: maxi = max(a[l1], b[l2], c[l3]) mini = min(a[l1], b[l2], c[l3]) sum = a[l1] + b[l2] + c[l3] if a[l1] == mini: l1 += 1 elif b[l2] == mini: l2 += 1 else: l3 += 1 if diff > maxi - mini: diff = maxi - mini res_sum = sum res_max = maxi res_min = mini return res_max, res_sum - res_max - res_min, res_min
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP VAR VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a, b, c = sorted(a), sorted(b), sorted(c) i, j, k = 0, 0, 0 p, q, r = 0, 0, 0 sum, diff = 0, 123456789 while i < n and j < n and k < n: csum = a[i] + b[j] + c[k] maximum = max(a[i], b[j], c[k]) minimum = min(a[i], b[j], c[k]) cdiff = maximum - minimum if minimum == a[i]: i += 1 elif minimum == b[j]: j += 1 else: k += 1 if cdiff < diff: diff = cdiff sum = csum p = maximum q = sum - (maximum + minimum) r = minimum return [p, q, r]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): merge_sort(a) merge_sort(b) merge_sort(c) i = 0 j = 0 k = 0 diff = 100000000000 temmax = 0 temmin = 0 temmid = 0 while i < n and j < n and k < n: summ = a[i] + b[j] + c[k] mi = min(a[i], b[j], c[k]) ma = max(a[i], b[j], c[k]) if mi == a[i]: i += 1 elif mi == b[j]: j += 1 elif mi == c[k]: k += 1 if ma - mi < diff: diff = ma - mi temmax = ma temmin = mi temmid = summ - (ma + mi) return [temmax, temmid, temmin] def merge_sort(list1): if len(list1) > 1: mid = len(list1) // 2 left_list = list1[:mid] right_list = list1[mid:] merge_sort(left_list) merge_sort(right_list) i = 0 j = 0 k = 0 while i < len(left_list) and j < len(right_list): if left_list[i] < right_list[j]: list1[k] = left_list[i] i += 1 k += 1 else: list1[k] = right_list[j] j += 1 k += 1 while i < len(left_list): list1[k] = left_list[i] i += 1 k += 1 while j < len(right_list): list1[k] = right_list[j] j += 1 k += 1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN LIST VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() results = [] sums = [] diff = max(a[0], b[0], c[0]) - min(a[0], b[0], c[0]) i = 0 j = 0 k = 0 while i < n and j < n and k < n: minimum = min(a[i], b[j], c[k]) temp = max(a[i], b[j], c[k]) - minimum if temp <= diff: results.append([a[i], b[j], c[k]]) diff = temp sums.append(a[i] + b[j] + c[k]) if a[i] == minimum: i += 1 if b[j] == minimum: j += 1 if c[k] == minimum: k += 1 results1 = [] for i in range(len(results)): if max(results[i]) - min(results[i]) <= diff: results1.append(results[i]) if len(results1) == 0: res = [a[0], b[0], c[0]] res.sort(reverse=True) return res else: res = results1[sums.index(min(sums))] res.sort(reverse=True) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def func(x, y, z): mn = min(x, y, z) mx = max(x, y, z) return mx - mn def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 a1, b1, c1 = a[-1], b[-1], c[-1] ans = 1000000 while i < n and j < n and k < n: temp = Solution.func(a[i], b[j], c[k]) if ans > temp: ans = temp a1 = a[i] b1 = b[j] c1 = c[k] elif ans == temp: temp2 = a[i] + b[j] + c[k] temp3 = a1 + b1 + c1 if temp2 < temp3: a1 = a[i] b1 = b[j] c1 = c[k] mn = min(a[i], b[j], c[k]) if a[i] == mn: i += 1 elif b[j] == mn: j += 1 else: k += 1 ans = [a1, b1, c1] ans.sort(reverse=True) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): minDiff = float("inf") first, second, third = 0, 0, 0 a.sort() b.sort() c.sort() ans = [] while first < n and second < n and third < n: mini = min(a[first], b[second], c[third]) curr_diff = max(a[first], b[second], c[third]) - mini if curr_diff < minDiff: ans = [a[first], b[second], c[third]] minDiff = min(minDiff, curr_diff) if a[first] == mini: first += 1 elif b[second] == mini: second += 1 else: third += 1 ans.sort(reverse=True) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): res = [] a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 fi, fj, fk = 0, 0, 0 diff = 99999999 while i < n and j < n and k < n: mn = min(a[i], b[j], c[k]) mx = max(a[i], b[j], c[k]) sm = a[i] + b[j] + c[k] if mx - mn < diff: diff = mx - mn fi, fj, fk = mn, sm - mx - mn, mx if mn == a[i]: i += 1 if mn == b[j]: j += 1 if mn == c[k]: k += 1 return fk, fj, fi
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() ans = float("inf") p1 = p2 = p3 = 0 res1 = res2 = res3 = 0 while p1 < len(a) and p2 < len(b) and p3 < len(c): maxx = max(a[p1], b[p2], c[p3]) minn = min(a[p1], b[p2], c[p3]) if ans > maxx - minn: ans = maxx - minn res1 = maxx res2 = a[p1] + b[p2] + c[p3] - maxx - minn res3 = minn if minn == a[p1]: p1 += 1 elif minn == b[p2]: p2 += 1 else: p3 += 1 return [res1, res2, res3]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i = j = k = 0 p = q = r = 0 summ = a[0] + b[0] + c[0] diff = max(a[p], b[q], c[r]) - min(a[p], b[q], c[r]) while i < n and j < n and k < n: maxi = max(a[i], b[j], c[k]) mini = min(a[i], b[j], c[k]) df = maxi - mini if df < diff: diff = df p, q, r = i, j, k elif df == diff: csum = sum([a[i], b[j], c[k]]) if csum < summ: p, q, r = i, j, k if a[i] == mini: i += 1 elif b[j] == mini: j += 1 else: k += 1 return sorted([a[p], b[q], c[r]], reverse=1)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, Arr1, Arr2, Arr3, N): ans = [] Arr1.sort() Arr2.sort() Arr3.sort() maxo, mino, mido = 0, 0, 0 i, j, k = 0, 0, 0 maxoo, minoo, midoo = 0, 0, 0 diff = 1000001 ludo = 0 non = 0 while i < N and j < N and k < N: sumo = Arr1[i] + Arr2[j] + Arr3[k] mino = min(Arr1[i], Arr2[j], Arr3[k]) maxo = max(Arr1[i], Arr2[j], Arr3[k]) ludo = maxo + mino if Arr1[i] == mino: i += 1 elif Arr2[j] == mino: j += 1 else: k += 1 if diff > maxo - mino and non < ludo: diff = maxo - mino maxoo = maxo minoo = mino midoo = sumo - (maxo + mino) non = ludo ans.append(maxoo) ans.append(midoo) ans.append(minoo) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i = j = k = 0 num1 = num2 = num3 = 0 diff = float("inf") while i < len(a) and j < len(b) and k < len(c): min_val = min(a[i], b[j], c[k]) max_val = max(a[i], b[j], c[k]) sum_val = a[i] + b[j] + c[k] if min_val == a[i]: i += 1 elif min_val == b[j]: j += 1 else: k += 1 if diff > max_val - min_val: diff = max_val - min_val num1 = max_val num2 = sum_val - (min_val + max_val) num3 = min_val return [num1, num2, num3]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN LIST VAR VAR VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, a, b, c, n): a.sort() b.sort() c.sort() i, j, k = 0, 0, 0 temp = [a[i], b[j], c[k]] val = 1000000 ans = [] while i < n and j < n and k < n: temp = [a[i], b[j], c[k]] mi, ma = 0, 0 for idx in range(1, 3): if temp[idx] < temp[mi]: mi = idx if temp[idx] > temp[ma]: ma = idx if temp[ma] - temp[mi] < val: val = temp[ma] - temp[mi] ans = temp.copy() if mi == 0: i += 1 elif mi == 1: j += 1 else: k += 1 ans.sort(reverse=True) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
import sys class Solution: def smallestDifferenceTriplet(self, arr1, arr2, arr3, n): arr1.sort() arr2.sort() arr3.sort() t1, t2, t3 = 0, 0, 0 i, j, k = 0, 0, 0 diff = 1000000 while i < n and j < n and k < n: sum = arr1[i] + arr2[j] + arr3[k] maxi = max(max(arr1[i], arr2[j]), arr3[k]) mini = min(min(arr1[i], arr2[j]), arr3[k]) if diff > maxi - mini: diff = maxi - mini t3 = maxi t2 = sum - (maxi + mini) t1 = mini if mini == arr1[i]: i += 1 elif mini == arr2[j]: j += 1 else: k += 1 return sorted([t1, t2, t3], reverse=True)
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR LIST VAR VAR VAR NUMBER
Three arrays of the same size are given. Find a triplet such that (maximum – minimum) in that triplet is the minimum of all the triplets. A triplet should be selected in a way such that it should have one number from each of the three given arrays. This triplet is the happiest among all the possible triplets. Print the triplet in decreasing order. If there are 2 or more smallest difference triplets, then the one with the smallest sum of its elements should be displayed. Example 1: Input: N=3 a[] = { 5, 2, 8 } b[] = { 10, 7, 12 } c[] = { 9, 14, 6 } Output: 7 6 5 Explanation: The triplet { 5,7,6 } has difference (maximum - minimum)= (7-5) =2 which is minimum of all triplets. Example 2: Input: N=4 a[] = { 15, 12, 18, 9 } b[] = { 10, 17, 13, 8 } c[] = { 14, 16, 11, 5 } Output: 11 10 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function smallestDifferenceTriplet() that takes array arr1 , array arr2 ,array arr3 and integer n as parameters and returns the happiest triplet in an array. Expected Time Complexity: O(NLogN). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def smallestDifferenceTriplet(self, A, B, C, n): i, j, k = 0, 0, 0 A.sort() B.sort() C.sort() nA = len(A) nB = len(B) nC = len(C) ans = 2147483647 diff = 2147483647 while i < nA and j < nB and k < nC: a = A[i] b = B[j] c = C[k] minE = min(a, b, c) if minE == a: i += 1 elif minE == b: j += 1 elif minE == c: k += 1 SUM = a + b + c MAX = max(a, b, c) MIN = min(a, b, c) if diff > MAX - MIN: diff = MAX - MIN res_max = MAX res_mid = SUM - (MAX + MIN) res_min = MIN return res_max, res_mid, res_min
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): def partition(arr, l, r): pivot = arr[r] i = l - 1 for j in range(l, r): if arr[j] <= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i + 1], arr[r] = arr[r], arr[i + 1] return i + 1 def quickSelect(arr, l, r, k): if l <= r: pivot_index = partition(arr, l, r) if pivot_index == k - 1: return arr[pivot_index] elif pivot_index > k - 1: return quickSelect(arr, l, pivot_index - 1, k) else: return quickSelect(arr, pivot_index + 1, r, k) return quickSelect(arr, l, r, k)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
import sys sys.setrecursionlimit(10**8) class Solution: def swap(self, arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def randomPartition(self, arr, l, r): n = r - l + 1 pivot = int(random.random() % n) self.swap(arr, l + pivot, r) return self.partition(arr, l, r) def kthSmallest(self, arr, l, r, k): if k > 0 and k <= r - l + 1: pos = self.randomPartition(arr, l, r) if pos - l == k - 1: return arr[pos] if pos - l > k - 1: return self.kthSmallest(arr, l, pos - 1, k) return self.kthSmallest(arr, pos + 1, r, k - pos + l - 1) return 999999999999 def partition(self, arr, l, r): x = arr[r] i = l for j in range(l, r): if arr[j] <= x: self.swap(arr, i, j) i += 1 self.swap(arr, i, r) return i
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): arr.sort() count = 0 for i in range(len(arr)): count += 1 if count == k: return arr[i]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): if k > 0 and k <= r - l + 1: pos = partition(arr, l, r) if pos - l == k - 1: return arr[pos] elif pos - l > k - 1: return self.kthSmallest(arr, l, pos - 1, k) else: return self.kthSmallest(arr, pos + 1, r, k - pos + l - 1) return None def partition(arr, l, r): x = arr[r] i = l for j in range(l, r): if arr[j] <= x: arr[i], arr[j] = arr[j], arr[i] i += 1 arr[i], arr[r] = arr[r], arr[i] return i
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN NONE FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): counting = [0] * (10**5 + 1) for i in arr: counting[i] += 1 count = 0 for i in range(10**5 + 1): count += counting[i] if count >= k: return i
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR RETURN VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def partionAlgo(self, arr, pivot, low, right): i = low j = low while i <= right: if arr[i] > pivot: i += 1 else: arr[i], arr[j] = arr[j], arr[i] i += 1 j += 1 return arr.index(pivot) def kthSmallestHelper(self, arr, k, left, right): if k > len(arr): return -1 if left == right: return arr[left] pivot_index = self.partionAlgo(arr, arr[right], left, right) if pivot_index == k - 1: return arr[pivot_index] elif pivot_index > k - 1: return self.kthSmallestHelper(arr, k, left, pivot_index - 1) else: return self.kthSmallestHelper(arr, k, pivot_index + 1, right) def kthSmallest(self, arr, l, r, k): return self.kthSmallestHelper(arr, k, l, r)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): d = {} for ele in arr: d[ele] = d.get(ele, 0) + 1 n = 0 for ele in sorted(d.keys()): n += d[ele] if n == k: return ele
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def partition(self, arr, l, r): x = arr[r] i = l for j in range(l, r): if arr[j] <= x: arr[i], arr[j] = arr[j], arr[i] i += 1 arr[i], arr[r] = arr[r], arr[i] return i def kthSmallest(self, arr, l, r, k): if k > 0 and k <= r - l + 1: index = self.partition(arr, l, r) if index - l == k - 1: return arr[index] if index - l > k - 1: return self.kthSmallest(arr, l, index - 1, k) else: return self.kthSmallest(arr, index + 1, r, k - index + l - 1) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN NUMBER
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): def fun(arr, l, r, k): if l == r: return arr[k] else: i = l ele = arr[l] for j in range(l + 1, r + 1): if arr[j] <= ele: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[l], arr[i] = arr[i], arr[l] pivot = i if pivot == k: return arr[k] elif pivot > k: return fun(arr, l, pivot - 1, k) else: return fun(arr, pivot + 1, r, k) return fun(arr, l, r, k - 1)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): l = min(arr) h = max(arr) ans = -1 while l <= h: mid = l + (h - l) // 2 csm = self.countSmaller(arr, mid) if csm >= k - 1: ans = mid h = mid - 1 else: l = mid + 1 if self.exists(arr, ans): return ans else: return self.nextMax(arr, ans) def countSmaller(self, arr, target): csm = 0 for num in arr: if num < target: csm += 1 return csm def exists(self, arr, target): for num in arr: if num == target: return True return False def nextMax(self, arr, target): maxDiff = abs(target - max(arr)) nextMax = max(arr) for num in arr: if abs(num - target) < maxDiff and num > target: nextMax = num maxDiff = abs(num - target) return nextMax
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the K^{th} smallest element in the given array. It is given that all array elements are distinct. Note :- l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the K^{th} smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5} 1 <= K <= N
class Solution: def kthSmallest(self, arr, l, r, k): if l == 0 and r == len(arr) - 1: k -= 1 if l == r: return arr[k] else: i = l ele = arr[l] for j in range(l + 1, r + 1): if arr[j] <= ele: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[l], arr[i] = arr[i], arr[l] pivot = i if pivot == k: return arr[k] elif pivot > k: return self.kthSmallest(arr, l, pivot - 1, k) else: return self.kthSmallest(arr, pivot + 1, r, k)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR