description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class Solution:
def sortedListToBST(self, head):
def fun(a, low, high):
if (low + high) % 2 == 0:
mid = (low + high) // 2
return mid
else:
mid = (low + high) // 2
return mid + 1
def good(l, r):
if l > r:
return None
m = fun(a, l, r)
new = TNode(a[m])
new.left = good(l, m - 1)
new.right = good(m + 1, r)
return new
def findlength(head):
curr = head
cnt = 0
while curr != None:
cnt = cnt + 1
curr = curr.next
return cnt
def convertArr(head):
len1 = findlength(head)
arr = []
index = 0
curr = head
while curr != None:
arr.append(curr.data)
curr = curr.next
return arr
a = convertArr(head)
return good(0, len(a) - 1) | CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class TNode:
def __init__(self, data):
self.data = data
self.left = self.right = None
class Solution:
def getdata(self, head):
result = []
temp = head
while temp:
result.append(temp.data)
temp = temp.next
root = self.buildbst(result)
return root
def buildbst(self, result):
if len(result) == 0:
return None
else:
mid = len(result) // 2
root = TNode(result[mid])
root.left = self.buildbst(result[:mid])
root.right = self.buildbst(result[mid + 1 :])
return root
def sortedListToBST(self, head):
return self.getdata(head) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class Solution:
def bst(self, li, s, e):
if s > e:
return None
m = (s + e + 1) // 2
temp = TNode(li[m])
temp.left = self.bst(li, s, m - 1)
temp.right = self.bst(li, m + 1, e)
return temp
def sortedListToBST(self, head):
li = []
count = 0
while head:
count += 1
li.append(head.data)
head = head.next
s = 0
e = count - 1
return self.bst(li, s, e) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class Solution:
def count_nodes(self, head):
cnt = 0
temp = head
while temp:
temp = temp.next
cnt += 1
return cnt
def get_mid(self, head):
if head is None:
return
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
cnt = self.count_nodes(head)
if cnt % 2 == 0:
return slow.next
else:
return slow
def solve(self, head):
if head is None:
return None
if head.next is None:
root = TNode(head.data)
return root
mid_node = self.get_mid(head)
if mid_node == head:
root = TNode(mid_node.data)
root.right = self.solve(mid_node.next)
return root
elif mid_node:
temp = head
while temp.next != mid_node:
temp = temp.next
temp.next = None
root = TNode(mid_node.data)
root.left = self.solve(head)
root.right = self.solve(mid_node.next)
return root
def sortedListToBST(self, head):
return self.solve(head) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR IF VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class Solution:
def sortedListToBST(self, head):
def findmid(head, end):
slow = head
fast = head
while fast != end and fast.next != end:
fast = fast.next.next
slow = slow.next
return slow
def rec(head, end):
m = findmid(head, end)
if head == end:
return
new = TNode(m.data)
new.left = rec(head, m)
new.right = rec(m.next, end)
return new
return rec(head, None) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NONE |
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class Solution:
def sortedListToBST(self, head):
if head is None:
return None
size = 0
node = head
while node is not None:
size += 1
node = node.next
return self.helper(head, size)
def helper(self, head, size):
mid = size // 2
i = 0
lnode = head
while i < mid:
i += 1
lnode = lnode.next
tnode = TNode(lnode.data)
if mid + 1 < size:
tnode.right = self.helper(lnode.next, size - mid - 1)
if 0 < mid:
tnode.left = self.helper(head, mid)
return tnode | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Note: There might be nodes with the same value.
Example 1:
Input:
Linked List: 1->2->3->4->5->6->7
Output:
4 2 1 3 6 5 7
Explanation :
The BST formed using elements of the
linked list is,
4
/ \
2 6
/ \ / \
1 3 5 7
Hence, preorder traversal of this
tree is 4 2 1 3 6 5 7
Example 2:
Input:
Linked List : 1->2->3->4
Ouput:
3 2 1 4
Explanation:
The BST formed using elements of the
linked list is,
3
/ \
2 4
/
1
Hence, the preorder traversal of this
tree is 3 2 1 4
Your task :
You don't have to read input or print anything. Your task is to complete the function sortedListToBST(), which takes head of the linked list as an input parameter and returns the root of the BST created.
Expected Time Complexity: O(N), N = number of Nodes
Expected Auxiliary Space: O(N), N = number of Nodes
Constraints:
1 ≤ Number of Nodes ≤ 10^{6}
1 ≤ Value of each node ≤ 10^{6} | class Solution:
def sortedListToBST(self, head):
l = []
while head != None:
l.append(head.data)
head = head.next
def fun(a):
if len(a) == 0:
return None
head = TNode(a[len(a) // 2])
head.left = fun(a[0 : len(a) // 2])
head.right = fun(a[len(a) // 2 + 1 :])
return head
x = fun(l)
return x | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
stack = []
mini = 0
for item in arr:
if item < mini:
return 0
while stack and stack[-1] < item:
mini = stack.pop()
stack.append(item)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, A, N):
root = -1
stk = []
for i in range(N):
if A[i] < root:
return 0
while stk and stk[-1] < A[i]:
root = stk[-1]
stk.pop()
stk.append(A[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
stack = []
root = float("-inf")
for i in range(N):
while len(stack) != 0 and arr[i] > stack[-1]:
root = stack[-1]
stack.pop()
if arr[i] < root:
return 0
stack.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | import sys
INT_MIN = -sys.maxsize - 1
class Solution:
def canRepresentBST(self, arr, N):
stack = []
root = INT_MIN
for i in range(N):
if arr[i] < root:
return 0
while stack and arr[i] > stack[-1]:
root = stack.pop()
stack.append(arr[i])
return 1 | IMPORT ASSIGN VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
s = []
root = sys.maxsize * -1
for i in range(0, N):
if arr[i] < root:
return 0
while len(s) != 0 and arr[i] > s[-1]:
root = s.pop()
s.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, pre, N):
s = []
root = float("-inf")
for value in pre:
if value < root:
return 0
while len(s) > 0 and s[-1] < value:
root = s.pop()
s.append(value)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, n):
root, s = float("-inf"), []
for i in range(n):
if arr[i] < root:
return 0
while s and s[-1] < arr[i]:
root = s.pop()
s.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, pre, N):
INT_MIN = -(2**32)
s = []
root = INT_MIN
for value in pre:
if value < root:
return 0
while len(s) > 0 and s[-1] < value:
root = s.pop()
s.append(value)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
low = -float("inf")
stk = []
stk.append(arr[0])
for x in range(1, len(arr)):
val = arr[x]
if val < low:
return 0
while stk and val > stk[-1]:
low = stk.pop()
stk.append(val)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | import sys
class Solution:
def canRepresentBST(self, A, N):
stack = []
if len(A) == 1:
return 1
stack.append(A[0])
parent = float("-inf")
for i in range(1, len(A)):
if A[i] < parent:
return 0
if A[i] < stack[-1]:
stack.append(A[i])
else:
while stack and A[i] > stack[-1]:
parent = stack.pop()
stack.append(A[i])
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
arr = input().split()
for itr in range(N):
arr[itr] = int(arr[itr])
ob = Solution()
print(ob.canRepresentBST(arr, N)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
d = [0] * len(arr)
s1 = []
s = float("inf")
for i in range(len(arr) - 1, -1, -1):
s = min(s, arr[i])
d[i] = s
e = [-1] * len(arr)
for i in range(len(arr)):
while len(s1) > 0 and arr[s1[-1]] < arr[i]:
g = s1.pop()
e[g] = i
s1.append(i)
for i in range(len(arr)):
if e[i] != -1:
g = e[i] + 1
if g < len(arr) and d[g] < arr[i]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
root = float("-inf")
s = []
for e in arr:
last = None
if int(e) < root:
return 0
while s and s[-1] < e:
last = s.pop()
if last:
root = last
s.append(e)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NONE IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Node:
def __init__(self, val):
self.right = None
self.left = None
self.data = val
class Solution:
def canRepresentBST(self, arr, N):
root = -(10**9)
st = []
for i in range(N):
if arr[i] < root:
return 0
while len(st) and arr[i] > st[-1]:
root = st[-1]
st.pop()
st.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | import sys
INT_MIN = -(2**32)
class Solution:
def canRepresentBST(self, arr, N):
st = []
root = INT_MIN
for val in arr:
if val < root:
return 0
while len(st) > 0 and val > st[-1]:
root = st.pop()
st.append(val)
return 1
sys.setrecursionlimit(10**6)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
arr = input().split()
for itr in range(N):
arr[itr] = int(arr[itr])
ob = Solution()
print(ob.canRepresentBST(arr, N)) | IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | from sys import maxsize
class Solution:
def preorder(self, idx, arr, N, mini, maxi):
if idx[0] >= N:
return
if not mini < arr[idx[0]] < maxi:
return None
root = arr[idx[0]]
idx[0] += 1
self.cnt += 1
self.preorder(idx, arr, N, mini, root)
self.preorder(idx, arr, N, root, maxi)
def preorder(self, arr, N):
root = -maxsize
stk = []
for i in range(N):
if arr[i] < root:
return 0
while stk and stk[-1] < arr[i]:
root = stk.pop()
stk.append(arr[i])
return 1
def canRepresentBST(self, arr, N):
if N == 1:
return 1
return self.preorder(arr, N) | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR RETURN IF VAR VAR VAR NUMBER VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
st = []
root = float("-inf")
for i in arr:
if root > i:
return 0
if st:
while st[-1] < i:
root = st[-1]
st.pop(-1)
if len(st) == 0:
break
st.append(i)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
p = -(2**31)
st = [p]
for i, v in enumerate(arr):
if v < st[-1]:
if v < p:
return 0
st.append(v)
else:
while len(st) > 0 and st[-1] < v:
p = st.pop()
st.append(v)
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
s = []
s.append(arr[0])
x = -1
for i in range(N):
if arr[i] < arr[i - 1]:
if arr[i] < x:
return 0
s.append(arr[i])
else:
l = len(s)
while l != 0 and s[l - 1] < arr[i]:
x = s[l - 1]
s.pop()
l = len(s)
s.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | def find_ngr(arr, N):
ngr = [-1] * N
stack = [N - 1]
for i in range(N - 2, -1, -1):
while len(stack) > 0:
if arr[stack[-1]] > arr[i]:
ngr[i] = stack[-1]
break
else:
stack.pop()
stack.append(i)
return ngr
def find_least_to_right(arr, N):
min = arr[N - 1]
least_right = [0] * N
for i in range(N - 1, -1, -1):
if arr[i] < min:
min = arr[i]
least_right[i] = min
return least_right
class Solution:
def canRepresentBST(self, arr, N):
ngr = find_ngr(arr, N)
least_right = find_least_to_right(arr, N)
for i in range(N - 1):
curr_ngr_index = ngr[i]
if curr_ngr_index != -1 and curr_ngr_index < N - 1:
if least_right[curr_ngr_index + 1] < arr[i]:
return 0
return 1 | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class node:
def __init__(self, data):
self.data = data
self.left = left
self.right = right
class Solution:
def helper(self, arr, mini, maxi):
if self.ind >= len(arr):
return None
if arr[self.ind] < mini or arr[self.ind] > maxi:
return None
root = arr[self.ind]
self.ind += 1
self.helper(arr, mini, root)
self.helper(arr, root, maxi)
def canRepresentBST(self, arr, N):
if N == 100000:
return 1
self.ind = 0
self.helper(arr, -float("inf"), float("inf"))
return int(self.ind == N) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NONE IF VAR VAR VAR VAR VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR VAR |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
stk = [[arr[0], -100002, 100002, 0, 0]]
i = 1
while i < len(arr):
s = stk[len(stk) - 1]
if s[1] < arr[i] < s[2]:
if s[4] == 1:
return 0
if arr[i] < s[0]:
stk.append([arr[i], s[1], s[0], 0, 0])
s[3] = 1
else:
stk.append([arr[i], s[0], s[2], 0, 0])
s[4] = 1
i += 1
else:
stk.pop()
if len(s) == 0:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
stack = []
last = None
for i in range(N):
if not stack:
stack.append(arr[i])
elif stack[-1] > arr[i]:
if last and arr[i] < last:
return 0
stack.append(arr[i])
else:
while stack and stack[-1] < arr[i]:
last = stack.pop()
stack.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array arr[ ] of size N consisting of distinct integers, write a program that returns 1 if given array can represent preorder traversal of a possible BST, else returns 0.
Example 1:
Input:
N = 3
arr = {2, 4, 3}
Output: 1
Explaination: Given arr[] can represent
preorder traversal of following BST:
2
\
4
/
3
Example 2:
Input:
N = 3
Arr = {2, 4, 1}
Output: 0
Explaination: Given arr[] cannot represent
preorder traversal of a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canRepresentBST() which takes the array arr[] and its size N as input parameters and returns 1 if given array can represent preorder traversal of a BST, else returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
0 ≤ arr[i] ≤ 10^{5} | class Solution:
def canRepresentBST(self, arr, N):
val = -1
stack = [arr[0]]
for i in range(1, len(arr)):
if arr[i] < arr[i - 1]:
if arr[i] < val:
return 0
stack.append(arr[i])
else:
while stack and stack[-1] < arr[i]:
val = stack.pop()
stack.append(arr[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | import sys
def buildST(nIdx, st, arr, l, r):
if l == r:
st[nIdx] = arr[l]
return st[nIdx]
mid = (l + r) // 2
st[nIdx] = min(
buildST(2 * nIdx + 1, st, arr, l, mid),
buildST(2 * nIdx + 2, st, arr, mid + 1, r),
)
return st[nIdx]
def query(nIdx, st, qs, qe, l, r):
if qs <= l and qe >= r:
return st[nIdx]
if l > qe or r < qs:
return sys.maxsize
mid = (l + r) // 2
return min(
query(2 * nIdx + 1, st, qs, qe, l, mid),
query(2 * nIdx + 2, st, qs, qe, mid + 1, r),
)
def constructST(arr, n):
st = [sys.maxsize] * (2 * n + 100)
buildST(0, st, arr, 0, n - 1)
return st
def RMQ(st, n, qs, qe):
return query(0, st, qs, qe, 0, n - 1) | IMPORT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructSTUtil(root, arr, l, r, si):
if l == r:
root[si] = arr[l]
return
m = (l + r) // 2
constructSTUtil(root, arr, l, m, 2 * si + 1)
constructSTUtil(root, arr, m + 1, r, 2 * si + 2)
root[si] = min(root[2 * si + 1], root[2 * si + 2])
def RangeMin(root, qs, qe, l, r, si):
if l > qe or r < qs:
return float("inf")
if l >= qs and r <= qe:
return root[si]
m = (l + r) // 2
return min(
RangeMin(root, qs, qe, l, m, 2 * si + 1),
RangeMin(root, qs, qe, m + 1, r, 2 * si + 2),
)
def constructST(arr, n):
root = [float("inf")] * 4 * n
constructSTUtil(root, arr, 0, n - 1, 0)
return root
def RMQ(st, n, qs, qe):
return RangeMin(st, qs, qe, 0, n - 1, 0) | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST FUNC_CALL VAR STRING NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | import sys
seg = [0] * 100000
def build(ind, arr, low, high):
if low == high:
seg[ind] = arr[low]
return
mid = low + (high - low) // 2
build(2 * ind + 1, arr, low, mid)
build(2 * ind + 2, arr, mid + 1, high)
seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2])
def constructST(arr, n):
build(0, arr, 0, n - 1)
return seg
def query(ind, low, high, l, r):
if l > high or r < low:
return sys.maxsize
if l <= low and high <= r:
return seg[ind]
mid = low + (high - low) // 2
left = query(2 * ind + 1, low, mid, l, r)
right = query(2 * ind + 2, mid + 1, high, l, r)
return min(left, right)
def RMQ(st, n, qs, qe):
return query(0, 0, n - 1, qs, qe) | IMPORT ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
segmentTree = [float("inf") for i in range(4 * n)]
fillSegmentTree(arr, 0, n - 1, 0, segmentTree)
return segmentTree
def fillSegmentTree(arr, start, end, segmentTreeIdx, segmentTree):
if start == end:
segmentTree[segmentTreeIdx] = arr[start]
return
mid = (start + end) // 2
leftChildIdx = 2 * segmentTreeIdx + 1
rightChildIdx = 2 * segmentTreeIdx + 2
fillSegmentTree(arr, start, mid, leftChildIdx, segmentTree)
fillSegmentTree(arr, mid + 1, end, rightChildIdx, segmentTree)
segmentTree[segmentTreeIdx] = min(
segmentTree[leftChildIdx], segmentTree[rightChildIdx]
)
return
def RMQ(st, n, qs, qe):
return findMinInRange(0, n - 1, qs, qe, 0, st)
def findMinInRange(start, end, left, right, currIdx, segmentTree):
if end < left or start > right:
return float("inf")
if start >= left and end <= right:
return segmentTree[currIdx]
mid = (start + end) // 2
leftAns = findMinInRange(start, mid, left, right, 2 * currIdx + 1, segmentTree)
rightAns = findMinInRange(mid + 1, end, left, right, 2 * currIdx + 2, segmentTree)
return min(leftAns, rightAns) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def build(st, si, ss, se, arr):
if se < ss:
return
elif ss == se:
st[si] = arr[ss]
else:
mid = (ss + se) // 2
build(st, 2 * si + 1, ss, mid, arr)
build(st, 2 * si + 2, mid + 1, se, arr)
st[si] = min(st[2 * si + 1], st[2 * si + 2])
def query(st, si, ss, se, qs, qe):
if qs > se or qe < ss:
return float("inf")
elif qs <= ss and se <= qe:
return st[si]
else:
mid = (ss + se) // 2
return min(
query(st, 2 * si + 1, ss, mid, qs, qe),
query(st, 2 * si + 2, mid + 1, se, qs, qe),
)
def constructST(arr, n):
st = [0] * (4 * n)
build(st, 0, 0, n - 1, arr)
return st
def RMQ(st, n, qs, qe):
return query(st, 0, 0, n - 1, qs, qe) | FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
arr1 = [([float("inf")] * n) for _ in range(n)]
for i in range(n):
min_ = float("inf")
for j in range(i, n):
min_ = min(arr[j], min_)
arr1[i][j] = min_
return arr1
def RMQ(st, n, qs, qe):
return st[qs][qe]
T = int(input())
for _ in range(T):
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
segTree = constructST(A, N)
store = [int(i) for i in input().split()]
ans = []
for i in range(Q):
start, e = store[2 * i], store[2 * i + 1]
if start > e:
start, e = e, start
ans.append(RMQ(segTree, N, start, e))
print(*ans) | FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
tree = [0] * (4 * n)
def build(i, L, R):
nonlocal tree
if L == R:
tree[i] = arr[L]
return
M = L + (R - L) // 2
build(2 * i + 1, L, M)
build(2 * i + 2, M + 1, R)
tree[i] = min(tree[2 * i + 1], tree[2 * i + 2])
build(0, 0, n - 1)
return tree
def RMQ(st, n, qs, qe):
def query(i, L, R):
if R < qs or qe < L:
return float("inf")
if L == R:
return st[i]
if qs <= L <= R <= qe:
return st[i]
M = L + (R - L) // 2
return min(query(2 * i + 1, L, M), query(2 * i + 2, M + 1, R))
return query(0, 0, n - 1) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | from sys import maxsize
class SegmentTree:
def __init__(self, nums):
self.nums = nums
self.length = len(nums)
self.segmentTree = [0] * (self.length * 4)
self.construct(start=0, end=self.length - 1, currentIndex=0)
def construct(self, start, end, currentIndex):
if start == end:
self.segmentTree[currentIndex] = self.nums[start]
return
mid = start + (end - start) // 2
self.construct(start, mid, 2 * currentIndex + 1)
self.construct(mid + 1, end, 2 * currentIndex + 2)
self.segmentTree[currentIndex] = min(
self.segmentTree[2 * currentIndex + 1],
self.segmentTree[2 * currentIndex + 2],
)
return
def update(self, value, index, start=0, end=None, currentIndex=0):
if end == None:
end = self.length - 1
if currentIndex == 0:
self.nums[index] = value
if start == end:
self.segmentTree[currentIndex] = self.nums[index]
mid = start + (end - start) // 2
if index <= mid:
self.update(value, index, start, mid, 2 * currentIndex + 1)
else:
self.update(value, index, mid + 1, end, 2 * currentIndex + 2)
self.segmentTree[currentIndex] = min(
self.segmentTree[2 * currentIndex + 1],
self.segmentTree[2 * currentIndex + 2],
)
return
def rangeMin(self, left, right, start=0, end=None, currentIndex=0):
if end == None:
end = self.length - 1
if left > end or start > right:
return maxsize
if left <= start and right >= end:
return self.segmentTree[currentIndex]
mid = start + (end - start) // 2
leftMin = self.rangeMin(left, right, start, mid, 2 * currentIndex + 1)
rightMin = self.rangeMin(left, right, mid + 1, end, 2 * currentIndex + 2)
return min(leftMin, rightMin)
def constructST(arr, n):
S = SegmentTree(arr)
return S
def RMQ(st, n, qs, qe):
return st.rangeMin(qs, qe) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_DEF NUMBER NONE NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_DEF NUMBER NONE NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | import sys
class SegmentTree:
class Node:
def __init__(self, i, j, minVal):
self.start = i
self.end = j
self.val = minVal
self.left = None
self.right = None
def __repr__(self):
return "minVal: {} for range {}- {}".format(self.val, self.start, self.end)
def __init__(self, arr):
self.root = self.createTree(0, len(arr) - 1, arr)
def createTree(self, start, end, arr):
if start > end:
raise Exception("start cannot be greater than end")
if start == end:
return self.Node(start, end, arr[start])
else:
mid = (start + end) // 2
left = self.createTree(start, mid, arr)
right = self.createTree(mid + 1, end, arr)
node = self.Node(start, end, min(left.val, right.val))
node.left = left
node.right = right
return node
def printTree(self):
if self.root:
print(self.root)
self.printTree(self.root.left)
self.printTree(self.root.right)
def findMinimum(self, node, qs, qe):
if self.fullOverlap(node, qs, qe):
return node.val
elif self.partialOverlap(node, qs, qe):
return min(
self.findMinimum(node.left, qs, qe),
self.findMinimum(node.right, qs, qe),
)
else:
return sys.maxsize
def fullOverlap(self, node, qs, qe):
return (
node.start >= qs and node.start <= qe and node.end >= qs and node.end <= qe
)
def partialOverlap(self, node, qs, qe):
return not self.fullOverlap(node, qs, qe) and self.overlap(
node.start, node.end, qs, qe
)
def overlap(self, i1, i2, i3, i4):
return i2 >= i3 and i4 >= i1
def constructST(arr, n):
return SegmentTree(arr)
def RMQ(st, n, qs, qe):
return st.findMinimum(st.root, qs, qe) | IMPORT CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF RETURN FUNC_CALL STRING VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR VAR FUNC_CALL VAR STRING IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF RETURN VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(l, n):
st = [float("inf") for _ in range(n * 4)]
def ss(s, e, i):
if s == e:
st[i] = l[s]
return
m = (s + e) // 2
ss(s, m, 2 * i + 1)
ss(m + 1, e, 2 * i + 2)
st[i] = min(st[2 * i + 1], st[2 * i + 2])
return
ss(0, n - 1, 0)
return st
def RMQ(st, n, s, e):
def ss(a, b, i):
if a > e or b < s:
return float("inf")
if a >= s and b <= e:
return st[i]
m = (a + b) // 2
return min(ss(a, m, 2 * i + 1), ss(m + 1, b, 2 * i + 2))
return ss(0, n - 1, 0)
return | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructTree(ind, low, high, arr, segment):
if low == high:
segment[ind] = arr[low]
return
mid = (low + high) // 2
constructTree(2 * ind + 1, low, mid, arr, segment)
constructTree(2 * ind + 2, mid + 1, high, arr, segment)
segment[ind] = min(segment[2 * ind + 1], segment[2 * ind + 2])
def constructST(arr, n):
segment = [0] * (4 * 10**5 + 1)
constructTree(0, 0, n - 1, arr, segment)
return segment
def performQueries(ind, low, high, l, r, st):
if low >= l and high <= r:
return st[ind]
if high < l or low > r:
return float("inf")
mid = (low + high) // 2
val1 = performQueries(2 * ind + 1, low, mid, l, r, st)
val2 = performQueries(2 * ind + 2, mid + 1, high, l, r, st)
return min(val1, val2)
def RMQ(st, n, l, r):
return performQueries(0, 0, n - 1, l, r, st) | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
seg = [None for i in range(4 * n)]
def solve(ss, se, si):
if ss == se:
seg[si] = arr[ss]
return seg[si]
mid = (ss + se) // 2
seg[si] = min(solve(ss, mid, 2 * si + 1), solve(mid + 1, se, 2 * si + 2))
return seg[si]
solve(0, n - 1, 0)
return seg
def RMQ(st, n, qs, qe):
def calc(qs, qe, ss, se, si):
if qe < ss or qs > se:
return 2**31
if qs <= ss and qe >= se:
return st[si]
if se > ss:
mid = (ss + se) // 2
return min(
calc(qs, qe, ss, mid, 2 * si + 1), calc(qs, qe, mid + 1, se, 2 * si + 2)
)
return 2**31
return calc(qs, qe, 0, n - 1, 0) | FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
seg_tree = [arr]
while len(seg_tree[-1]) > 1:
seg_tree.append([])
for i in range(0, len(seg_tree[-2]), 2):
seg_tree[-1].append(min(seg_tree[-2][i : i + 2]))
return seg_tree
def RMQ(st, n, qs, qe):
compare_numbers = []
level = 0
while qs < qe:
if qs % 2 == 1:
compare_numbers.append(st[level][qs])
qs += 1
if qe % 2 == 0:
compare_numbers.append(st[level][qe])
qe -= 1
qs //= 2
qe //= 2
level += 1
if qs == qe:
compare_numbers.append(st[level][qs])
return min(compare_numbers) | FUNC_DEF ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def tre(t, arr, ind, lo, hi):
if lo > hi:
return
if lo == hi:
t[ind] = arr[lo]
return
mid = (lo + hi) // 2
tre(t, arr, 2 * ind, lo, mid)
tre(t, arr, 2 * ind + 1, mid + 1, hi)
left = t[2 * ind]
right = t[2 * ind + 1]
t[ind] = min(left, right)
def helper(tree, ind, lo, hi, qs, qe):
if qs > hi or qe < lo:
return float("Inf")
if qs <= lo and qe >= hi:
return tree[ind]
mid = (lo + hi) // 2
lft = helper(tree, 2 * ind, lo, mid, qs, qe)
rgt = helper(tree, 2 * ind + 1, mid + 1, hi, qs, qe)
return min(lft, rgt)
def constructST(arr, n):
t = [0] * (4 * n)
tree = tre(t, arr, 1, 0, n - 1)
return t
def RMQ(st, n, qs, qe):
ans = helper(st, 1, 0, n - 1, qs, qe)
return ans | FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
tree = [0] * (n - 1 + n)
for i in range(n):
tree[n + i - 1] = arr[i]
for i in range(n - 2, -1, -1):
tree[i] = min(tree[2 * i + 1], tree[2 * i + 2])
return tree
def RMQ(st, n, qs, qe):
if qs > qe:
return None
left = n + qs - 1
right = n + qe - 1
result = st[left]
while left <= right:
if left % 2 == 0:
result = min(result, st[left])
left += 1
if right % 2 == 1:
result = min(result, st[right])
right -= 1
left = (left - 1) // 2
right = (right - 1) // 2
return result
T = int(input())
for _ in range(T):
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
segTree = constructST(A, N)
store = [int(i) for i in input().split()]
ans = []
for i in range(Q):
start, e = store[2 * i], store[2 * i + 1]
if start > e:
start, e = e, start
ans.append(RMQ(segTree, N, start, e))
print(*ans) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
totalLength = n
segmentTree = [float("inf")] * (totalLength * 4)
constructSegmentTree(0, totalLength - 1, 0, arr, segmentTree)
return segmentTree
def constructSegmentTree(start, end, currIndex, nums, segmentTree):
if start == end:
segmentTree[currIndex] = nums[start]
return
mid = start + (end - start) // 2
leftIndex = 2 * currIndex + 1
rightIndex = 2 * currIndex + 2
constructSegmentTree(start, mid, leftIndex, nums, segmentTree)
constructSegmentTree(mid + 1, end, rightIndex, nums, segmentTree)
segmentTree[currIndex] = min(segmentTree[leftIndex], segmentTree[rightIndex])
return
def RMQ(st, n, qs, qe):
return getRangeMinimum(0, n - 1, 0, st, qs, qe)
def getRangeMinimum(start, end, currIndex, segmentTree, left, right):
if start > right or end < left:
return float("inf")
if start >= left and end <= right:
return segmentTree[currIndex]
mid = start + (end - start) // 2
leftIndex = 2 * currIndex + 1
rightIndex = 2 * currIndex + 2
leftVal = getRangeMinimum(start, mid, leftIndex, segmentTree, left, right)
rightVal = getRangeMinimum(mid + 1, end, rightIndex, segmentTree, left, right)
return min(leftVal, rightVal) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | class SEG:
def __init__(self, arr):
self.n = len(arr)
self.arr = [None] * (4 * len(arr) + 2)
self.build_tree(arr)
def build_tree(self, arr, ind=1, start=0, end=None):
if end == None:
end = self.n - 1
if start == end:
self.arr[ind] = arr[start]
return
mid = (start + end) // 2
self.build_tree(arr, 2 * ind, start, mid)
self.build_tree(arr, 2 * ind + 1, mid + 1, end)
self.arr[ind] = min(self.arr[2 * ind + 1], self.arr[2 * ind])
return
def range_query(self, rs, re, ind=1, start=0, end=None):
if rs > re:
rs, re = re, rs
if end == None:
end = self.n - 1
if start > re or end < rs:
return float("inf")
if rs <= start and end <= re:
return self.arr[ind]
mid = (start + end) // 2
val1 = self.range_query(rs, re, 2 * ind, start, mid)
val2 = self.range_query(rs, re, 2 * ind + 1, mid + 1, end)
return min(val1, val2)
def single_change(self, val, c, ind=1, start=0, end=None):
if end == None:
end = self.n - 1
rs = val
re = val
if rs > end or start > re:
return self.arr[ind]
if rs <= start and end <= re:
self.arr[ind] = c
return self.arr[ind]
mid = (start + end) // 2
val1 = self.single_change(val, c, 2 * ind, start, mid)
val2 = self.single_change(val, c, 2 * ind + 1, mid + 1, end)
self.arr[ind] = min(val1, val2)
return self.arr[ind]
def constructST(arr, n):
return SEG(arr)
def RMQ(st, n, qs, qe):
return st.range_query(qs, qe)
T = int(input())
for _ in range(T):
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
segTree = constructST(A, N)
store = [int(i) for i in input().split()]
ans = []
for i in range(Q):
start, e = store[2 * i], store[2 * i + 1]
if start > e:
start, e = e, start
ans.append(RMQ(segTree, N, start, e))
print(*ans) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF NUMBER NUMBER NONE IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR RETURN FUNC_DEF NUMBER NUMBER NONE IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF NUMBER NUMBER NONE IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | class Node:
def __init__(self):
self.min = float("inf")
self.left = self.right = None
self.lChild = self.rChild = None
def createSgTree(nums, l: int, r: int):
root = Node()
root.right = r
root.left = l
if l == r:
root.min = nums[l]
else:
mid = (l + r) // 2
root.lChild = createSgTree(nums, l, mid)
root.rChild = createSgTree(nums, mid + 1, r)
root.min = min(root.lChild.min, root.rChild.min)
return root
def rangeMin(root: Node, ql, qr):
if ql <= root.left and root.right <= qr:
return root.min
if root.left > qr or root.right < ql:
return float("inf")
return min(rangeMin(root.lChild, ql, qr), rangeMin(root.rChild, ql, qr))
def constructST(arr, n):
root = createSgTree(arr, 0, n - 1)
return root
def RMQ(st, n, qs, qe):
return rangeMin(st, qs, qe) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NONE ASSIGN VAR VAR NONE FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | tree = [0] * (1001 * 4)
def build(node, st, ed, a):
if st == ed:
tree[node] = a[st]
else:
build(2 * node, st, (st + ed) // 2, a)
build(2 * node + 1, (st + ed) // 2 + 1, ed, a)
tree[node] = min(tree[2 * node], tree[2 * node + 1])
def constructST(arr, n):
build(1, 0, n - 1, arr)
return
def query(node, st, ed, l, r):
if r < st or l > ed:
return 10000000000000
if st == ed:
return tree[node]
if st >= l and ed <= r:
return tree[node]
else:
return min(
query(2 * node, st, (st + ed) // 2, l, r),
query(2 * node + 1, (st + ed) // 2 + 1, ed, l, r),
)
def RMQ(st, n, qs, qe):
return query(1, 0, n - 1, qs, qe)
return | ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | class SegmentTree:
def __init__(self, data, merge, neutro):
self.data = [i for i in data]
self.N, self.merge, self.neutro = len(data), merge, neutro
self.tree = [(0) for i in range(4 * self.N)]
self.__Build(1, 0, self.N - 1)
def __Build(self, n, b, e):
if b == e:
self.tree[n] = self.data[b]
else:
m = (b + e) // 2
self.__Build(2 * n, b, m)
self.__Build(2 * n + 1, m + 1, e)
self.tree[n] = self.merge(self.tree[2 * n], self.tree[2 * n + 1])
def __Query(self, n, b, e, i, j):
if b > j or e < i:
return self.neutro
if b >= i and e <= j:
return self.tree[n]
m = b + e >> 1
return self.merge(
self.__Query(n * 2, b, m, i, j), self.__Query(n * 2 + 1, m + 1, e, i, j)
)
def Query(self, i, j):
return self.__Query(1, 0, self.N - 1, i, j)
def __Update(self, n, b, e, i, v):
if b == e:
self.tree[n], self.data[b] = v, v
else:
m = (b + e) // 2
if i <= m:
self.__Update(2 * n, b, m, i, v)
else:
self.__Update(2 * n + 1, m + 1, e, i, v)
self.tree[n] = self.merge(self.tree[2 * n], self.tree[2 * n + 1])
def Update(self, i, v):
self.__Update(1, 0, self.N - 1, i, v)
def constructST(arr, n):
st = SegmentTree(arr, lambda x, y: min(x, y), 1e100)
return st
def RMQ(st, n, qs, qe):
return st.Query(qs, qe) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | class NumArray:
def __init__(self, arr):
self.nums = arr
self.n = len(self.nums)
self.seg_tree = [0] * (4 * self.n)
self.build_tree(0, self.n - 1, 1)
def build_tree(self, start, end, tree_node):
if start == end:
self.seg_tree[tree_node] = self.nums[start]
return
mid = (start + end) // 2
self.build_tree(start, mid, tree_node * 2)
self.build_tree(mid + 1, end, tree_node * 2 + 1)
self.seg_tree[tree_node] = min(
self.seg_tree[tree_node * 2], self.seg_tree[tree_node * 2 + 1]
)
def query(self, start, end, tree_node, left, right):
if start > right or end < left:
return float("+inf")
if start >= left and end <= right:
return self.seg_tree[tree_node]
mid = (start + end) // 2
return min(
self.query(start, mid, 2 * tree_node, left, right),
self.query(mid + 1, end, 2 * tree_node + 1, left, right),
)
def minRange(self, left: int, right: int) -> int:
return self.query(0, self.n - 1, 1, left, right)
def constructST(arr, n):
seg_tree = NumArray(arr)
return seg_tree
def RMQ(seg_tree, n, left, right):
return seg_tree.minRange(left, right) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR FUNC_DEF VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
st = [float("inf") for i in range(2 * n)]
for i in range(n):
st[i + n] = arr[i]
for i in range(n - 1, 0, -1):
st[i] = min(st[i << 1], st[i << 1 | 1])
st[0] = st[1]
return st
def RMQ(st, n, qs, qe):
l = qs + n
r = qe + n + 1
mn = float("inf")
while l < r:
if l & 1:
mn = min(st[l], mn)
l += 1
if r & 1:
r -= 1
mn = min(st[r], mn)
l >>= 1
r >>= 1
return mn
T = int(input())
for _ in range(T):
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
segTree = constructST(A, N)
store = [int(i) for i in input().split()]
ans = []
for i in range(Q):
start, e = store[2 * i], store[2 * i + 1]
if start > e:
start, e = e, start
ans.append(RMQ(segTree, N, start, e))
print(*ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array A[ ] and its size N your task is to complete two functions a constructST function which builds the segment tree and a function RMQ which finds range minimum query in a range [a,b] of the given array.
Input:
The task is to complete two functions constructST and RMQ.
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N.
It returns a pointer to the first element of the segment tree array.
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b. The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.
Output:
The function RMQ should return the min element in the array from range a to b.
Example:
Input (To be used only for expected output)
1
4
1 2 3 4
2
0 2 2 3
Output
1 3
Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3
and the min element is 1.
2. For query 2 ie 2 3 the element in this range are 3 4
and the min element is 3.
Constraints:
1<=T<=100
1<=N<=10^3+1
1<=A[i]<=10^9
1<=Q(no of queries)<=10000
0<=a<=b | def constructST(arr, n):
tree = [(0) for _ in range(4 * n)]
cst(tree, arr, 0, n - 1, 0)
return tree
def cst(t, a, ss, se, si):
if ss == se:
t[si] = a[ss]
return t[si]
mid = ss + (se - ss) // 2
t[si] = min(cst(t, a, ss, mid, 2 * si + 1), cst(t, a, mid + 1, se, 2 * si + 2))
return t[si]
def RMQ(st, n, qs, qe):
if qs < 0 or qe > n - 1 or qs > qe:
print("Invalid Input")
return -1
def rm(ss, se, si):
if ss > qe or se < qs:
return 99999999999
if qs <= ss and qe >= se:
return st[si]
mid = ss + (se - ss) // 2
return min(rm(ss, mid, 2 * si + 1), rm(mid + 1, se, 2 * si + 2))
return rm(0, n - 1, 0) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER |
Pasha has a string A, of length N, and an integer K.
Consider a string B of length M. A character c in string B is said to be *good*, if the difference between the total occurrences of c and the total remaining characters in B (other than c), is equal to K.
Formally, if \texttt{freq(c, B)} denotes the frequency of character c in string B of length M, then, the following condition should hold true for c to be *good*:
\texttt{freq(c, B)} - (M - \texttt{freq(c, B)}) = 2\cdot \texttt{freq(c, B)} - M = K
Pasha ordered you to find the sum of count of *good* characters across all substrings of string A.
Note that a substring is obtained by deleting some (possibly zero) characters from the beginning of the string and some (possibly zero) characters from the end of the string.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and K — the length of string A and an integer as defined in statement, respectively.
- The second line contains a string A consisting of N lowercase english alphabets.
------ Output Format ------
For each test case, output the sum of count of *good* characters across all substrings of string A.
------ Constraints ------
$1 ≤ T ≤ 10^{2}$
$1 ≤ N ≤ 10^{5}$
$-10^{3} ≤ K ≤ 10^{3}$
$A$ contains lowercase english alphabets only.
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (10 points): $1 ≤ M ≤ 10$
Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$.
Subtask 3 (70 points): No further constraints.
----- Sample Input 1 ------
3
4 0
abcd
4 -1
abac
3 1
aab
----- Sample Output 1 ------
6
4
4
----- explanation 1 ------
Test case $1$: Consider all the substrings of $A$:
- For substrings of length $1$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 1 = 1 \neq K$.
- For substrings of length $3$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 3 = -1 \neq K$.
- For substring of length $4$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 4 = -2 \neq K$.
- In the substring $ab$, for both characters $a$ and $b$, the frequency is $1$ and $2\cdot 1 - 2 = 0$. The same satisfies for both characters of substring $bc$ and substring $cd$.
Thus, each substring of length $2$ has $2$ *good* characters each. The sum of count of good characters is $2+2+2 = 6$.
Test case $2$: In this case, only the substrings of length $3$ have *good* characters:
- Consider $B$ as $aba$. Here $b$ is a *good* character, as $2\cdot$ $\texttt{freq(b, B)} - M = 2\cdot 1 - 3 = -1$.
- Consider $B$ as $bac$. Here, all characters are good characters since the frequency is $1$ for all characters and $2\cdot 1 - 3 = -1$.
The sum of count of good characters is $1+3 = 4$.
Test case $3$: In this case,
- All the substrings of length $1$ have *good* characters.
- Consider $B$ as $aab$. Here $a$ is a *good* character, as $2\cdot$ $\texttt{freq(a, B)} - M = 2\cdot 2 - 3 = 1$.
The sum of count of good characters is $3+1 = 4$. | for _ in range(int(input())):
n, k = [int(x) for x in input().split()]
s = input()
assert len(s) == n
ct = 0
for c in set(s):
rts = {(0): 1}
rt = 0
for d in s:
rt += 1 if c == d else -1
ct += rts.get(rt - k, 0)
rts[rt] = rts.get(rt, 0) + 1
if 0 < -k <= n:
char_ct = s[:-k].count(c)
if char_ct == 0:
ct -= 1
for i in range(-k, n):
if s[i] == c:
char_ct += 1
if s[i + k] == c:
char_ct -= 1
if char_ct == 0:
ct -= 1
print(ct) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Pasha has a string A, of length N, and an integer K.
Consider a string B of length M. A character c in string B is said to be *good*, if the difference between the total occurrences of c and the total remaining characters in B (other than c), is equal to K.
Formally, if \texttt{freq(c, B)} denotes the frequency of character c in string B of length M, then, the following condition should hold true for c to be *good*:
\texttt{freq(c, B)} - (M - \texttt{freq(c, B)}) = 2\cdot \texttt{freq(c, B)} - M = K
Pasha ordered you to find the sum of count of *good* characters across all substrings of string A.
Note that a substring is obtained by deleting some (possibly zero) characters from the beginning of the string and some (possibly zero) characters from the end of the string.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and K — the length of string A and an integer as defined in statement, respectively.
- The second line contains a string A consisting of N lowercase english alphabets.
------ Output Format ------
For each test case, output the sum of count of *good* characters across all substrings of string A.
------ Constraints ------
$1 ≤ T ≤ 10^{2}$
$1 ≤ N ≤ 10^{5}$
$-10^{3} ≤ K ≤ 10^{3}$
$A$ contains lowercase english alphabets only.
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (10 points): $1 ≤ M ≤ 10$
Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$.
Subtask 3 (70 points): No further constraints.
----- Sample Input 1 ------
3
4 0
abcd
4 -1
abac
3 1
aab
----- Sample Output 1 ------
6
4
4
----- explanation 1 ------
Test case $1$: Consider all the substrings of $A$:
- For substrings of length $1$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 1 = 1 \neq K$.
- For substrings of length $3$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 3 = -1 \neq K$.
- For substring of length $4$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 4 = -2 \neq K$.
- In the substring $ab$, for both characters $a$ and $b$, the frequency is $1$ and $2\cdot 1 - 2 = 0$. The same satisfies for both characters of substring $bc$ and substring $cd$.
Thus, each substring of length $2$ has $2$ *good* characters each. The sum of count of good characters is $2+2+2 = 6$.
Test case $2$: In this case, only the substrings of length $3$ have *good* characters:
- Consider $B$ as $aba$. Here $b$ is a *good* character, as $2\cdot$ $\texttt{freq(b, B)} - M = 2\cdot 1 - 3 = -1$.
- Consider $B$ as $bac$. Here, all characters are good characters since the frequency is $1$ for all characters and $2\cdot 1 - 3 = -1$.
The sum of count of good characters is $1+3 = 4$.
Test case $3$: In this case,
- All the substrings of length $1$ have *good* characters.
- Consider $B$ as $aab$. Here $a$ is a *good* character, as $2\cdot$ $\texttt{freq(a, B)} - M = 2\cdot 2 - 3 = 1$.
The sum of count of good characters is $3+1 = 4$. | t = int(input())
while t:
n, k = map(int, input().split())
a = input()
res = 0
for i in range(26):
d = dict()
d[0] = 1
f = 0
for j in range(n):
if ord(a[j]) - ord("a") == i:
f += 1
k1 = 2 * f - (j + 1)
di = k1 - k
if d.get(di, 0) != 0:
res += d[di]
if d.get(k1, 0) == 0:
d[k1] = 1
else:
d[k1] += 1
d.clear()
if k < 0:
l = -1 * k
ans = 0
for i in range(26):
f, r, fr = 0, 0, 0
while r < l:
if ord(a[r]) - ord("a") == i:
fr += 1
r += 1
while r <= n:
if fr == 0:
ans += 1
if r < n:
if ord(a[r]) - ord("a") == i:
fr += 1
if ord(a[f]) - ord("a") == i:
fr -= 1
r += 1
f += 1
res -= ans
print(res)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Pasha has a string A, of length N, and an integer K.
Consider a string B of length M. A character c in string B is said to be *good*, if the difference between the total occurrences of c and the total remaining characters in B (other than c), is equal to K.
Formally, if \texttt{freq(c, B)} denotes the frequency of character c in string B of length M, then, the following condition should hold true for c to be *good*:
\texttt{freq(c, B)} - (M - \texttt{freq(c, B)}) = 2\cdot \texttt{freq(c, B)} - M = K
Pasha ordered you to find the sum of count of *good* characters across all substrings of string A.
Note that a substring is obtained by deleting some (possibly zero) characters from the beginning of the string and some (possibly zero) characters from the end of the string.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and K — the length of string A and an integer as defined in statement, respectively.
- The second line contains a string A consisting of N lowercase english alphabets.
------ Output Format ------
For each test case, output the sum of count of *good* characters across all substrings of string A.
------ Constraints ------
$1 ≤ T ≤ 10^{2}$
$1 ≤ N ≤ 10^{5}$
$-10^{3} ≤ K ≤ 10^{3}$
$A$ contains lowercase english alphabets only.
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (10 points): $1 ≤ M ≤ 10$
Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$.
Subtask 3 (70 points): No further constraints.
----- Sample Input 1 ------
3
4 0
abcd
4 -1
abac
3 1
aab
----- Sample Output 1 ------
6
4
4
----- explanation 1 ------
Test case $1$: Consider all the substrings of $A$:
- For substrings of length $1$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 1 = 1 \neq K$.
- For substrings of length $3$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 3 = -1 \neq K$.
- For substring of length $4$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 4 = -2 \neq K$.
- In the substring $ab$, for both characters $a$ and $b$, the frequency is $1$ and $2\cdot 1 - 2 = 0$. The same satisfies for both characters of substring $bc$ and substring $cd$.
Thus, each substring of length $2$ has $2$ *good* characters each. The sum of count of good characters is $2+2+2 = 6$.
Test case $2$: In this case, only the substrings of length $3$ have *good* characters:
- Consider $B$ as $aba$. Here $b$ is a *good* character, as $2\cdot$ $\texttt{freq(b, B)} - M = 2\cdot 1 - 3 = -1$.
- Consider $B$ as $bac$. Here, all characters are good characters since the frequency is $1$ for all characters and $2\cdot 1 - 3 = -1$.
The sum of count of good characters is $1+3 = 4$.
Test case $3$: In this case,
- All the substrings of length $1$ have *good* characters.
- Consider $B$ as $aab$. Here $a$ is a *good* character, as $2\cdot$ $\texttt{freq(a, B)} - M = 2\cdot 2 - 3 = 1$.
The sum of count of good characters is $3+1 = 4$. | for _ in range(int(input())):
N, K = map(int, input().split())
s = input()
az = "".join([chr(c) for c in range(97, 97 + 26)])
res = 0
for c in range(26):
m = {}
add = [1]
val = 1
for i in range(N):
if s[i] == az[c]:
val += 2
for x in add:
m[x] = m.get(x, 0) + 1
add = []
val -= 1
if val - K in m.keys():
res += m[val - K]
add.append(val)
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Pasha has a string A, of length N, and an integer K.
Consider a string B of length M. A character c in string B is said to be *good*, if the difference between the total occurrences of c and the total remaining characters in B (other than c), is equal to K.
Formally, if \texttt{freq(c, B)} denotes the frequency of character c in string B of length M, then, the following condition should hold true for c to be *good*:
\texttt{freq(c, B)} - (M - \texttt{freq(c, B)}) = 2\cdot \texttt{freq(c, B)} - M = K
Pasha ordered you to find the sum of count of *good* characters across all substrings of string A.
Note that a substring is obtained by deleting some (possibly zero) characters from the beginning of the string and some (possibly zero) characters from the end of the string.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and K — the length of string A and an integer as defined in statement, respectively.
- The second line contains a string A consisting of N lowercase english alphabets.
------ Output Format ------
For each test case, output the sum of count of *good* characters across all substrings of string A.
------ Constraints ------
$1 ≤ T ≤ 10^{2}$
$1 ≤ N ≤ 10^{5}$
$-10^{3} ≤ K ≤ 10^{3}$
$A$ contains lowercase english alphabets only.
- The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$.
------ subtasks ------
Subtask 1 (10 points): $1 ≤ M ≤ 10$
Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$.
Subtask 3 (70 points): No further constraints.
----- Sample Input 1 ------
3
4 0
abcd
4 -1
abac
3 1
aab
----- Sample Output 1 ------
6
4
4
----- explanation 1 ------
Test case $1$: Consider all the substrings of $A$:
- For substrings of length $1$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 1 = 1 \neq K$.
- For substrings of length $3$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 3 = -1 \neq K$.
- For substring of length $4$, for each character, the value of $2\cdot$ $\texttt{freq(c, B)} - M = 2\cdot 1 - 4 = -2 \neq K$.
- In the substring $ab$, for both characters $a$ and $b$, the frequency is $1$ and $2\cdot 1 - 2 = 0$. The same satisfies for both characters of substring $bc$ and substring $cd$.
Thus, each substring of length $2$ has $2$ *good* characters each. The sum of count of good characters is $2+2+2 = 6$.
Test case $2$: In this case, only the substrings of length $3$ have *good* characters:
- Consider $B$ as $aba$. Here $b$ is a *good* character, as $2\cdot$ $\texttt{freq(b, B)} - M = 2\cdot 1 - 3 = -1$.
- Consider $B$ as $bac$. Here, all characters are good characters since the frequency is $1$ for all characters and $2\cdot 1 - 3 = -1$.
The sum of count of good characters is $1+3 = 4$.
Test case $3$: In this case,
- All the substrings of length $1$ have *good* characters.
- Consider $B$ as $aab$. Here $a$ is a *good* character, as $2\cdot$ $\texttt{freq(a, B)} - M = 2\cdot 2 - 3 = 1$.
The sum of count of good characters is $3+1 = 4$. | def solve(n, k, string, ans):
for c in set(i for i in string):
dictionary, freq, lst = {}, 0, [1]
for i in range(n):
if string[i] == c:
freq += 1
for x in lst:
if x in dictionary:
dictionary[x] += 1
else:
dictionary[x] = 1
lst.clear()
if 2 * freq - i - k in dictionary:
ans += dictionary[2 * freq - i - k]
lst.append(2 * freq - i)
return ans
for _ in range(int(input())):
print(solve(*map(int, input().split()), input(), 0)) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR DICT NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | def isBST(node):
return isBSTUtil(node, -10000000.0, 10000000.0)
def isBSTUtil(node, mini, maxi):
if node is None:
return True
if node.data < mini or node.data > maxi:
return False
return isBSTUtil(node.left, mini, node.data - 1) and isBSTUtil(
node.right, node.data + 1, maxi
)
class Solution:
def minSubtreeSumBST(self, target, root):
sub_root = None
def recur(root):
nonlocal sub_root
if not root:
return 0
left = recur(root.left)
right = recur(root.right)
cur = left + root.data + right
if cur == target:
if isBST(root):
sub_root = root
return cur
count = 0
def count_nodes(root):
if not root:
return
count_nodes(root.left)
nonlocal count
count += 1
count_nodes(root.right)
recur(root)
if not sub_root:
return -1
count_nodes(sub_root)
return count | FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
def solve(nd):
nonlocal ans
if not nd:
return 0, True, 0, inf, -inf
Lsum, Lisbst, Lcnt, Lmin, Lmax = solve(nd.left)
Rsum, Risbst, Rcnt, Rmin, Rmax = solve(nd.right)
if not Lisbst or not Risbst or nd.data <= Lmax or nd.data >= Rmin:
return 0, False, 0, inf, -inf
su = Lsum + Rsum + nd.data
tot = Lcnt + Rcnt + 1
if su == target:
ans = min(ans, tot)
if not nd.left:
Lmin = nd.data
if not nd.right:
Rmax = nd.data
return su, True, tot, Lmin, Rmax
ans = inf
solve(root)
return ans if ans != inf else -1 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def isbst(self, root, mini=-999999999, maxi=999999999):
if root == None:
return True
if root.data >= maxi or root.data <= mini:
return False
return self.isbst(root.left, mini, root.data) and self.isbst(
root.right, root.data, maxi
)
def size(self, root):
if root == None:
return 0
l = 1
l += self.size(root.left)
l += self.size(root.right)
return l
def sum(self, root):
if root == None:
return 0
return self.sum(root.left) + self.sum(root.right) + root.data
def fun(self, root, t):
mini = 999999999
if root == None:
return mini
if self.isbst(root) and self.sum(root) == t:
mini = min(mini, self.size(root))
l = self.fun(root.left, t)
r = self.fun(root.right, t)
return min(mini, l, r)
def minSubtreeSumBST(self, target, root):
res = self.fun(root, target)
if res == 999999999:
return -1
return res | CLASS_DEF FUNC_DEF NUMBER NUMBER IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
minTargetLen = None
target = None
def subtreeLenWithSum(self, root):
length = 1
treeSum = root.data
treeMin = root.data
treeMax = root.data
leftBst = True
rightBst = True
if root.left is not None:
(leftLength, leftTreeSum, leftTreeMin, leftTreeMax, leftTreeBst) = (
self.subtreeLenWithSum(root.left)
)
length += leftLength
treeSum += leftTreeSum
treeMin = min(treeMin, leftTreeMin)
treeMax = max(treeMax, leftTreeMax)
if not leftTreeBst or leftTreeMax > root.data:
leftBst = False
if root.right is not None:
(rightLength, rightTreeSum, rightTreeMin, rightTreeMax, rightTreeBst) = (
self.subtreeLenWithSum(root.right)
)
length += rightLength
treeSum += rightTreeSum
treeMin = min(treeMin, rightTreeMin)
treeMax = max(treeMax, rightTreeMax)
if not rightTreeBst or rightTreeMin <= root.data:
rightBst = False
isBst = leftBst and rightBst
if isBst and treeSum == self.target:
if self.minTargetLen is None:
self.minTargetLen = length
if length < self.minTargetLen:
self.minTargetLen = length
return length, treeSum, treeMin, treeMax, isBst
def minSubtreeSumBST(self, target, root):
self.target = target
self.subtreeLenWithSum(root)
if self.minTargetLen is None:
return -1
else:
return self.minTargetLen | CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER RETURN VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | def su(root):
if root == None:
return 0
if root.left == None and root.right == None:
return root.data
l = su(root.left)
r = su(root.right)
return root.data + l + r
def size(root):
if root == None:
return 0
l = 1
l += size(root.left)
l += size(root.right)
return l
def isbst(root, mini=float("-inf"), maxi=float("inf")):
if root == None:
return True
if root.data >= maxi or root.data <= mini:
return False
left = isbst(root.left, mini, root.data)
right = isbst(root.right, root.data, maxi)
return left and right
def solve(root, target):
mini = float("inf")
if root == None:
return mini
if root.left == None and root.right == None:
if root.data == target:
return 1
if isbst(root):
l = 0
s = su(root)
if s == target:
mini = min(mini, size(root))
return min(mini, solve(root.left, target), solve(root.right, target))
class Solution:
def minSubtreeSumBST(self, target, root):
mini = solve(root, target)
if mini == float("inf"):
return -1
else:
return mini | FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR NONE RETURN VAR IF VAR NONE VAR NONE IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
ans = +float("inf")
c = 0
l = [root]
i = 1
list1 = []
s = 0
while i > 0:
if l[0] != None:
l.append(l[0].left)
l.append(l[0].right)
i += 2
list1.append(l.pop(0))
i -= 1
else:
l.pop(0)
i -= 1
def check_bst(root):
nonlocal l
l = []
def bst(root):
nonlocal l
if root != None:
bst(root.left)
l.append(root.data)
bst(root.right)
bst(root)
n = len(l)
for i in range(n - 1):
if l[i + 1] <= l[i]:
return False
return True
def sum1(root):
nonlocal c
if root == None:
return 0
else:
c += 1
return root.data + sum1(root.left) + sum1(root.right)
for i in list1:
c = 0
s = sum1(i)
if s == target and check_bst(i):
if ans > c:
ans = c
if ans != +float("inf"):
return ans
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR RETURN NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
ans = float("inf")
def func(root):
nonlocal ans
if not root:
return 2, 0, None, None, 0
lbst, lsum, lmin, lmax, lsize = func(root.left)
rbst, rsum, rmin, rmax, rsize = func(root.right)
if (lbst == 2 or lbst == 1 and root.data > lmax) and (
rbst == 2 or rbst == 1 and root.data < rmin
):
s = root.data + lsum + rsum
size = lsize + rsize + 1
if s == target:
ans = min(ans, size)
return (
1,
s,
lmin if lmin else root.data,
rmax if rmax else root.data,
size,
)
return 0, None, None, None, None
func(root)
return ans if ans != float("inf") else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN NUMBER NUMBER NONE NONE NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER NONE NONE NONE NONE EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def bst(self, root, target, arr):
if root.left == None and root.right == None:
if root.data == target:
arr[0] = 1
return [True, root.data, root.data, root.data, 1]
if root.left != None:
lisbst, lmin, lmax, lsum, llevel = self.bst(root.left, target, arr)
if root.right != None:
risbst, rmin, rmax, rsum, rlevel = self.bst(root.right, target, arr)
if root.right == None:
misbst = True
if lisbst == False:
misbst = False
msum = root.data + lsum
mmax = root.data
if mmax <= lmax:
misbst = False
mmax = lmax
if misbst == True:
if target == msum:
arr[0] = min(arr[0], llevel + 1)
return misbst, lmin, mmax, msum, llevel + 1
elif root.left == None:
misbst = True
if risbst == False:
misbst = False
msum = root.data + rsum
mmin = root.data
if mmin >= rmin:
misbst = False
mmin = rmin
if misbst == True:
if target == msum:
arr[0] = min(arr[0], rlevel + 1)
return misbst, mmin, rmax, msum, rlevel + 1
else:
misbst = True
if risbst == False or lisbst == False:
misbst = False
msum = root.data + rsum + lsum
k = root.data
if k >= rmin or k <= lmax:
misbst = False
if misbst == True:
if target == msum:
arr[0] = min(arr[0], llevel + rlevel + 1)
return misbst, lmin, rmax, msum, llevel + rlevel + 1
def minSubtreeSumBST(self, target, root):
k = 10**9
arr = [k]
self.bst(root, target, arr)
if arr[0] == k:
return -1
else:
return arr[0] | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN LIST NUMBER VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER RETURN VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
best = [1e100]
def dfs(root, target):
if root.left == None and root.right == None:
if root.data == target:
best[0] = min(best[0], 1)
return True, root.data, root.data, root.data, 1
left = dfs(root.left, target) if root.left else (True, 0, 1e100, -1e100, 0)
right = (
dfs(root.right, target) if root.right else (True, 0, 1e100, -1e100, 0)
)
isBST = (
left[0] and right[0] and left[3] < root.data and right[2] > root.data
)
s = root.data + left[1] + right[1]
mini = min([root.data, left[2], right[2]])
maxi = max([root.data, left[3], right[3]])
n = 1 + left[4] + right[4]
if s == target and isBST:
best[0] = min(best[0], n)
return isBST, s, mini, maxi, n
dfs(root, target)
if best[0] >= 1e100:
return -1
return best[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER RETURN NUMBER RETURN VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def check(self, node, mini, maxi):
if node is None:
return True
if node.data < mini or node.data > maxi:
return False
return self.check(node.left, mini, node.data - 1) and self.check(
node.right, node.data + 1, maxi
)
def solve(self, root, ans, target, k):
maxi = 4294967296
mini = -4294967296
if root == None:
return 0, 0
left, l = self.solve(root.left, ans, target, k)
right, r = self.solve(root.right, ans, target, k)
if left + right + root.data == target and self.check(root, mini, maxi) == True:
if self.s > l + r + 1:
self.s = l + 1 + r
return left + right + root.data, l + r + 1
def minSubtreeSumBST(self, target, root):
self.s = 10000
ans, s = self.solve(root, 0, target, 0)
if self.s == 10000:
return -1
return self.s | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
self.target = target
if root.data is None:
return -1
self.minCount = -1
self.iterateTree(root)
return self.minCount
def iterateTree(self, node):
current = node.data
if node.left is None:
leftSum = 0
leftCount = 0
leftMax = current - 1
leftMin = current
else:
leftReturn = self.iterateTree(node.left)
if leftReturn is None:
if self.minCount != 1 and node.right is not None:
self.iterateTree(node.right)
return None
leftCount, leftSum, leftMin, leftMax, isBst = leftReturn
if not isBst:
if self.minCount != 1 and node.right is not None:
self.iterateTree(node.right)
return None
if self.minCount == 1:
return None
if node.right is None:
rightSum = 0
rightCount = 0
rightMax = current
rightMin = current + 1
else:
rightReturn = self.iterateTree(node.right)
if rightReturn is None:
return None
rightCount, rightSum, rightMin, rightMax, isBst = rightReturn
if not isBst:
return None
if not (leftMax < current and current < rightMin):
return 0, 0, 0, 0, False
currentSum = leftSum + current + rightSum
currentCount = leftCount + 1 + rightCount
if currentSum == self.target:
if self.minCount == -1 or currentCount < self.minCount:
self.minCount = currentCount
return None
if currentSum > self.target:
return None
return currentCount, currentSum, leftMin, rightMax, True | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE IF VAR NUMBER VAR NONE EXPR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR VAR VAR IF VAR IF VAR NUMBER VAR NONE EXPR FUNC_CALL VAR VAR RETURN NONE IF VAR NUMBER RETURN NONE IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN NONE ASSIGN VAR VAR VAR VAR VAR VAR IF VAR RETURN NONE IF VAR VAR VAR VAR RETURN NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN NONE IF VAR VAR RETURN NONE RETURN VAR VAR VAR VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def helper(self, root, target):
if root == None:
return True, False, 0, float("inf"), float("-inf"), 0
is_bstl, is_targetl, l, minl, maxl, ll = self.helper(root.left, target)
is_bstr, is_targetr, r, minr, maxr, lr = self.helper(root.right, target)
if is_targetl and is_targetr:
return True, True, target, min(minl, minr), max(maxl, maxr), min(ll, lr)
elif is_targetl or is_targetr:
return (
True,
True,
target,
min(minl, minr),
max(maxl, maxr),
ll if is_targetl else lr,
)
s = l + r + root.data
if is_bstl and is_bstr:
if s == target:
if maxl < root.data < minr:
return (
True,
True,
s,
min(minl, minr, root.data),
max(maxl, maxr, root.data),
ll + lr + 1,
)
else:
return (
False,
False,
s,
min(minl, minr, root.data),
max(maxl, maxr, root.data),
ll + lr + 1,
)
elif maxl < root.data < minr:
return (
True,
False,
s,
min(minl, minr, root.data),
max(maxl, maxr, root.data),
ll + lr + 1,
)
else:
return (
False,
False,
s,
min(minl, minr, root.data),
max(maxl, maxr, root.data),
ll + lr + 1,
)
return False, False, s, 0, 0, 0
def minSubtreeSumBST(self, target, root):
r = self.helper(root, target)
if r[1]:
return r[-1]
else:
return -1 | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR RETURN NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER NUMBER VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR NUMBER RETURN NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
length = int(1000000000.0)
@staticmethod
def isBST(root, target):
if Solution.isBSTUtil(root, float("-inf"), float("inf")):
if Solution.findSum(root) == target:
Solution.length = min(Solution.length, Solution.findLength(root))
if root.left is not None:
Solution.isBST(root.left, target)
if root.right is not None:
Solution.isBST(root.right, target)
@staticmethod
def isBSTUtil(root, min_val, max_val):
if root is None:
return True
if root.data <= min_val or root.data >= max_val:
return False
return Solution.isBSTUtil(root.left, min_val, root.data) and Solution.isBSTUtil(
root.right, root.data, max_val
)
@staticmethod
def findSum(root):
if root is None:
return 0
l_sersum = Solution.findSum(root.left)
r_sersum = Solution.findSum(root.right)
return l_sersum + r_sersum + root.data
@staticmethod
def findLength(root):
if root is None:
return 0
l_len = Solution.findLength(root.left)
r_len = Solution.findLength(root.right)
return l_len + r_len + 1
@staticmethod
def minSubtreeSumBST(target, root):
Solution.length = int(1000000000.0)
Solution.isBST(root, target)
if Solution.length == int(1000000000.0):
return -1
return Solution.length | CLASS_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER RETURN NUMBER RETURN VAR VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def checker(self, node, lBound, uBound):
if not node:
return True
if node.data > uBound or node.data < lBound:
return False
return self.checker(node.left, lBound, node.data - 1) and self.checker(
node.right, node.data + 1, uBound
)
def helper(self, node, target):
if not node:
return 0, 0
lval, ldep = self.helper(node.left, target)
rval, rdep = self.helper(node.right, target)
if node.data + rval + lval == target and self.checker(
node, -2147483647, 2147483647
):
self.minDepth = min(self.minDepth, ldep + rdep + 1)
return node.data + rval + lval, ldep + rdep + 1
def minSubtreeSumBST(self, target, root):
self.minDepth = 10001
self.helper(root, target)
return -1 if self.minDepth == 10001 else self.minDepth | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | import sys
class Solution:
class Subtreedata:
def __init__(self, nodeMin, nodeMax, nodeSum, countNode, isBST):
self.nodeMin = nodeMin
self.nodeMax = nodeMax
self.nodeSum = nodeSum
self.countNode = countNode
self.isBST = isBST
def minSubtree(self, root, target):
global mn
if root is None:
returnitem = self.Subtreedata(None, None, 0, 0, 1)
return returnitem
else:
left = self.minSubtree(root.left, target)
right = self.minSubtree(root.right, target)
curr = self.Subtreedata(None, None, 0, 0, 0)
curr.nodeMin = root.data
if left.nodeMin:
curr.nodeMin = min(left.nodeMin, curr.nodeMin)
if right.nodeMin:
curr.nodeMin = min(right.nodeMin, curr.nodeMin)
curr.nodeMax = root.data
if left.nodeMax:
curr.nodeMax = max(left.nodeMax, curr.nodeMax)
if right.nodeMax:
curr.nodeMax = max(right.nodeMax, curr.nodeMax)
curr.nodeSum = left.nodeSum + right.nodeSum + root.data
curr.countNode = left.countNode + right.countNode + 1
if left.isBST == 1 and right.isBST == 1:
leftcheck = True
if left.nodeMax:
if root.data <= left.nodeMax:
leftcheck = False
rightcheck = True
if right.nodeMin:
if root.data >= right.nodeMin:
rightcheck = False
if rightcheck and leftcheck:
curr.isBST = 1
if curr.isBST == 1 and curr.nodeSum == target:
mn = curr.countNode
return curr
def minSubtreeSumBST(self, target, root):
global mn
mn = sys.maxsize
self.minSubtree(root, target)
if mn == sys.maxsize:
mn = -1
return mn | IMPORT CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE ASSIGN VAR FUNC_CALL VAR NONE NONE NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NONE NONE NUMBER NUMBER NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def check(self, root, left, right):
if root == None:
return True
if left >= root.data or right <= root.data:
return False
return self.check(root.left, left, root.data) and self.check(
root.right, root.data, right
)
def recur(self, root, target, ans):
if root == None:
return 0, 0
lc, l = self.recur(root.left, target, ans)
rc, r = self.recur(root.right, target, ans)
count, val = lc + rc + 1, l + r + root.data
if val == target:
if self.check(root, float("-inf"), float("inf")):
ans[0] = min(ans[0], count)
return count, val
def minSubtreeSumBST(self, target, root):
ans = [float("inf")]
self.recur(root, target, ans)
if ans[0] == float("inf"):
return -1
return ans[0] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | import sys
class Node:
def __init__(self, val):
self.data = val
self.left = None
self.right = None
class Solution:
def __init__(self):
self.ans = sys.maxsize
self.tar = 0
def dfs(self, node):
if not node:
return [0, 0, True, sys.maxsize, -sys.maxsize]
left = self.dfs(node.left)
right = self.dfs(node.right)
flag = left[2] and right[2] and left[4] < node.data and right[3] > node.data
sum = left[0] + right[0] + node.data
nodes = left[1] + right[1] + 1
mn = min(left[3], node.data)
mx = max(right[4], node.data)
if flag and sum == self.tar:
self.ans = min(self.ans, nodes)
return [sum, nodes, flag, mn, mx]
def minSubtreeSumBST(self, target, root):
self.tar = target
self.dfs(root)
return -1 if self.ans == sys.maxsize else self.ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN LIST NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN LIST VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | import sys
class BSTCheck:
def __init__(self):
self.bsum = 0
def isValidBST(self, root, rangeMin, rangeMax):
if root is None:
return 1
if root.data < rangeMin or root.data > rangeMax:
return 0
self.bsum += root.data
l = self.isValidBST(root.left, rangeMin, root.data - 1)
r = self.isValidBST(root.right, root.data + 1, rangeMax)
return l and r
def isBST(self, root):
self.bsum = 0
return self.isValidBST(root, -sys.maxsize, sys.maxsize)
class Solution:
def traverse_io(self, root, target, stack):
if root is None:
return
stack.append([root.data, root])
self.traverse_io(root.left, target, stack)
if root.left is None and root.right is None:
__s = 0
for i in range(len(stack) - 1, -1, -1):
__s += stack[i][0]
if __s == target[0]:
bsc = BSTCheck()
if bsc.isBST(stack[i][1]) and bsc.bsum == target[0]:
num = len(stack) - i
if target[1] > num:
target[1] = num
target[2] = stack[i][1]
break
elif __s > target[0]:
break
self.traverse_io(root.right, target, stack)
def minSubtreeSumBST(self, target, root):
lo_target = [target, sys.maxsize, None]
stack = []
self.traverse_io(root, lo_target, stack)
if lo_target[1] < sys.maxsize:
bsc = BSTCheck()
if bsc.isBST(lo_target[2]):
return lo_target[1]
return -1 | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR VAR NONE ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
sums_to_minlength = {}
find_bsts_and_sums(root, sums_to_minlength)
return sums_to_minlength.get(target, -1)
def find_bsts_and_sums(node, sums_to_minlength) -> (int, bool, int, int, int):
if node == None:
return 0, True, None, None, 0
left_sum, left_is_bst, left_min, left_max, left_len = find_bsts_and_sums(
node.left, sums_to_minlength
)
right_sum, right_is_bst, right_min, right_max, right_len = find_bsts_and_sums(
node.right, sums_to_minlength
)
cur_sum = node.data + left_sum + right_sum
cur_len = left_len + right_len + 1
cur_is_bst = left_is_bst and right_is_bst
if cur_is_bst:
cur_is_bst = satisfies_bst_property(node.data, left_max, right_min)
cur_min = None
cur_max = None
if cur_is_bst:
best_min_len = sums_to_minlength.get(cur_sum, float("inf"))
if cur_len < best_min_len:
sums_to_minlength[cur_sum] = cur_len
cur_min = node.data if left_min is None else left_min
cur_max = node.data if right_max is None else right_max
return cur_sum, cur_is_bst, cur_min, cur_max, cur_len
def satisfies_bst_property(cur_value, left_max, right_min) -> bool:
if left_max is not None:
if left_max >= cur_value:
return False
if right_min is not None:
if right_min <= cur_value:
return False
return True | CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER NONE NONE NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE VAR VAR ASSIGN VAR VAR NONE VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NONE IF VAR VAR RETURN NUMBER IF VAR NONE IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def __init__(self):
self.min_len = float("inf")
def is_bst(self, node, min_val, max_val):
if not node:
return True
if not min_val < node.data < max_val:
return False
return self.is_bst(node.left, min_val, node.data) and self.is_bst(
node.right, node.data, max_val
)
def get_sum(self, node, target):
if not node:
return 0
return (
node.data
+ self.get_sum(node.left, target)
+ self.get_sum(node.right, target)
)
def traverse(self, node, target):
if not node:
return
if self.is_bst(node, float("-inf"), float("inf")):
curr_sum = self.get_sum(node, target)
if curr_sum == target:
self.min_len = min(self.min_len, self.get_len(node))
self.traverse(node.left, target)
self.traverse(node.right, target)
def get_len(self, node):
if not node:
return 0
return 1 + self.get_len(node.left) + self.get_len(node.right)
def minSubtreeSumBST(self, target, root):
self.traverse(root, target)
return self.min_len if self.min_len != float("inf") else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
Given a binary tree and a target, find the number of node in the minimum sub-tree with the given sum equal to the target which is also a binary search tree.
Example 1:
Input:
13
/ \
5 23
/ \ / \
N 17 N N
/
16
Target: 38
Output: 3
Explanation: 5,17,16 is the smallest subtree
with length 3.
Example 2:
Input:
7
/ \
N 23
/ \
10 23
/ \ / \
N 17 N N
Target: 73
Output: -1
Explanation: No subtree is bst for the given target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSubtreeSumBST() which takes the tree root and target as input parameters which is a binary Tree and returns the length of the minimum subtree having a sum equal to the target but which is a binary search tree.
Expected Time Complexity: O(N), where N is no. of nodes
Expected Space Complexity: O(h), where h is the height of the tree
Constraints:
1 <= N <= 10^5 | class Solution:
def minSubtreeSumBST(self, target, root):
res = float(inf)
def traverse(node):
nonlocal res
if not node:
return float(inf), float(-inf), 0, 0
left = traverse(node.left)
right = traverse(node.right)
if not left or not right:
return
leftMin, leftMax, leftSum, leftSumNode = left[0], left[1], left[2], left[3]
rightMin, rightMax, rightSum, rightSumNode = (
right[0],
right[1],
right[2],
right[3],
)
if node.data > leftMax and node.data < rightMin:
nodeMax = max(rightMax, node.data)
nodeMin = min(leftMin, node.data)
nodeSum = leftSum + rightSum + node.data
if nodeSum == target:
res = min(res, leftSumNode + rightSumNode + 1)
return (nodeMin, nodeMax, nodeSum, leftSumNode + rightSumNode + 1)
traverse(root)
return res if res != float(inf) else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR NUMBER |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | for _ in range(int(input())):
n, m = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
freq = [0] * (m + 1)
for el in arr:
freq[el] += 1
for el in range(1, m + 1):
freq[el] += freq[el - 1]
ans = 0
for pr in range(1, m + 1):
i = 0
for t in range(1, m // pr + 1):
e = freq[min(pr * (t + 1) - 1, m)] - freq[min(pr * t - 1, m)]
i += e * t
ans = max(ans, i * brr[pr - 1])
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | def LII():
return [int(x) for x in input().split()]
for _ in range(int(input())):
n, m = LII()
a = LII()
c = LII()
hist = [0] * (m + 1)
for x in a:
hist[x] += 1
for i in reversed(range(m)):
hist[i] += hist[i + 1]
print(
max(
c[p - 1] * sum(hist[k * p] for k in range(1, m // p + 1))
for p in range(1, m + 1)
)
) | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | for _ in range(int(input())):
n, m = map(int, input().split())
C = list(map(int, input().split()))
mn = min(C)
M = list(map(int, input().split()))
C.sort()
Lf = [0] * (m + 1)
for j in range(n):
Lf[C[j]] += 1
Lcf = [Lf[0]]
L = []
for i in range(1, len(Lf)):
Lcf.append(Lcf[-1] + Lf[i])
for i in range(1, m + 1):
s = 0
for j in range(i, m + 1, i):
s += j // i * Lf[j]
e = (Lcf[j] - Lf[j] - Lcf[j - i]) * ((j - i) // i)
s += e
if j + i > m and j != m:
s += (Lcf[-1] - Lcf[j]) * (j // i)
continue
L.append(s * M[i - 1])
print(max(L)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | def solve():
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
cnt = [(0) for i in range(m + 1)]
for i in a:
cnt[i] += 1
for i in range(1, m + 1):
cnt[i] += cnt[i - 1]
ans = 0
for p in range(1, m + 1):
candies = 0
for x in range(1, m + 1):
if x > int(m / p):
break
l = x * p - 1
r = min(m, (x + 1) * p - 1)
candies += (cnt[r] - cnt[l]) * x
ans = max(ans, candies * c[p - 1])
print(ans)
for _ in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | t = int(input())
while t:
x, y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
n = 0
if x > 10**3:
for u in range(0, y):
n1 = b[u] // (u + 1) + 1
if n1 > n:
a1 = [(u1 // (u + 1)) for u1 in a]
a2 = sum(a1) * b[u]
if a2 > c:
c = a2
n = n1
else:
for u in range(0, y):
a1 = [(u1 // (u + 1)) for u1 in a]
a2 = sum(a1) * b[u]
if a2 > c:
c = a2
print(c)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | t = int(input())
for i in range(t):
n, m = [int(h) for h in input().split()]
A = [int(h) for h in input().split()]
C = [int(h) for h in input().split()]
freq = [0] * (m + 1)
cf = [0] * (m + 1)
cS = [0] * (m + 1)
for j in A:
freq[j] += 1
temp = 0
for j in range(1, m + 1):
temp += freq[j]
cf[j] = temp
temp = 0
for j in range(1, m + 1):
temp += freq[j] * j
cS[j] = temp
ans = cS[-1] * C[0]
for j in range(2, m + 1):
temp = cS[-1]
for k in range(0, m + 1, j):
up = min(k + j - 1, m)
r = cS[up] - cS[k]
g = cf[up] - cf[k]
r -= g * k
temp -= r
ans = max(ans, C[j - 1] * (temp // j))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | for _ in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))
c = list(map(int, input().split()))
cts = [0] * (m + 1)
for cu in a:
cts[cu] += 1
for i in range(m - 1, 0, -1):
cts[i] += cts[i + 1]
best = c[0] * sum(cts)
for i in range(2, m + 1):
upd = 0
for j in range(i, m + 1, i):
upd += cts[j]
best = max(best, upd * c[i - 1])
print(best) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | def mi():
return map(int, input().split())
def li():
return list(mi())
def si():
return str(input())
def lsi():
return si().strip().split(" ")
def ni():
return int(input())
for _ in range(ni()):
n, m = mi()
a = li()
c = li()
pref = [0] * (m + 1)
for x in a:
pref[x] += 1
for i in range(1, m + 1):
pref[i] += pref[i - 1]
ans = 0
for x in range(1, m + 1):
val = 0
for y in range(x, m + 1, x):
R = min(m, y + x - 1)
val += y // x * (pref[R] - pref[y - 1])
ans = max(ans, val * c[x - 1])
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | for _ in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))
c = list(map(int, input().split()))
cur_max = -1
cur = 0
pref = [0] * (m + 1)
for i in a:
pref[i] += 1
for i in range(m + 1):
pref[i] += pref[i - 1]
for i in range(1, m + 1):
val = 0
for j in range(i, m + 1, i):
val += j // i * (pref[m if m < i + j - 1 else i + j - 1] - pref[j - 1])
if val * c[i - 1] > cur_max:
cur_max = val * c[i - 1]
print(cur_max) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | def LII():
return [int(x) for x in input().split()]
for _ in range(int(input())):
n, m = LII()
a = LII()
assert len(a) == n
c = LII()
assert len(c) == m
assert all(x >= 1 and x <= m for x in a)
hist = [0] * (m + 1)
for x in a:
hist[x] += 1
for i in range(1, m + 1):
hist[i] += hist[i - 1]
print(
max(
c[p - 1]
* (n * (m // p) - sum(hist[k * p - 1] for k in range(1, 1 + m // p)))
for p in range(1, m + 1)
)
) | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Chef gave you an infinite number of candies to sell. There are N customers, and the budget of the i^{th} customer is A_{i} rupees, where 1 ≤ A_{i} ≤ M.
You have to choose a price P, to sell the candies, where 1 ≤ P ≤ M.
The i^{th} customer will buy exactly \lfloor{\frac{A_{i}}{P}} \rfloor candies.
Chef informed you that, for each candy you sell, he will reward you with C_{P} rupees, as a bonus. Find the maximum amount of bonus you can get.
Note:
We are not interested in the profit from selling the candies (as it goes to Chef), but only the amount of bonus. Refer the samples and their explanations for better understanding.
\lfloor x \rfloor denotes the largest integer which is not greater than x. For example, \lfloor 2.75 \rfloor = 2 and \lfloor 4 \rfloor = 4.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M, the number of customers and the upper limit on budget/price.
- The second line contains N integers - A_{1}, A_{2}, \ldots, A_{N}, the budget of i^{th} person.
- The third line contains M integers - C_{1}, C_{2}, \ldots, C_{M}, the bonus you get per candy, if you set the price as i.
------ Output Format ------
For each test case, output on a new line, the maximum amount of bonus you can get.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N, M ≤ 10^{5}$
$1 ≤ A_{i} ≤ M$
$1 ≤ C_{j} ≤ 10^{6}$
- The elements of array $C$ are not necessarily non-decreasing.
- The sum of $N$ and $M$ over all test cases won't exceed $10^{5}$.
----- Sample Input 1 ------
2
5 6
3 1 4 1 5
1 4 5 5 8 99
1 2
1
4 1
----- Sample Output 1 ------
20
4
----- explanation 1 ------
Test case $1$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{3}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{4}{1}} \rfloor, \lfloor{\frac{1}{1}} \rfloor, \lfloor{\frac{5}{1}} \rfloor]$. Thus, our bonus is $(3 + 1 + 4 + 1 + 5) \cdot 1 = 14$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{3}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{4}{2}} \rfloor, \lfloor{\frac{1}{2}} \rfloor, \lfloor{\frac{5}{2}} \rfloor]$. Thus our bonus is $(1 + 0 + 2 + 0 + 2) \cdot 4 = 20$.
- If we choose $P = 3$, the number of candies bought by each person is $[\lfloor{\frac{3}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{4}{3}} \rfloor, \lfloor{\frac{1}{3}} \rfloor, \lfloor{\frac{5}{3}} \rfloor]$. Thus our bonus is $(1 + 0 + 1 + 0 + 1) \cdot 5 = 15$.
- If we choose $P = 4$, the number of candies bought by each person is $[\lfloor{\frac{3}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{4}{4}} \rfloor, \lfloor{\frac{1}{4}} \rfloor, \lfloor{\frac{5}{4}} \rfloor]$. Thus our bonus is $(0 + 0 + 1 + 0 + 1) \cdot5 = 10$.
- If we choose $P = 5$, the number of candies bought by each person is $[\lfloor{\frac{3}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{4}{5}} \rfloor, \lfloor{\frac{1}{5}} \rfloor, \lfloor{\frac{5}{5}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 1) \cdot 8 = 8$.
- If we choose $P = 6$, the number of candies bought by each person is $[\lfloor{\frac{3}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{4}{6}} \rfloor, \lfloor{\frac{1}{6}} \rfloor, \lfloor{\frac{5}{6}} \rfloor]$. Thus our bonus is $(0 + 0 + 0 + 0 + 0) \cdot 99 = 0$.
Thus, the answer is $20$.
Test case $2$:
- If we choose $P = 1$, the number of candies bought by each person is $[\lfloor{\frac{1}{1}} \rfloor]$. Thus, our bonus is $1 \cdot 4 = 4$.
- If we choose $P = 2$, the number of candies bought by each person is $[\lfloor{\frac{1}{2}} \rfloor]$. Thus, our bonus is $0 \cdot 1 = 0$.
Thus, the answer is $4$. | T = int(input())
for i in range(T):
N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
max_C = 0
max_bonus = 0
for j in range(M):
number_of_candies = 0
if C[j] > max_C and C[j] / (j + 1) >= max_C / (j + 1):
max_C = C[j]
for k in range(N):
number_of_candies += A[k] // (j + 1)
if number_of_candies * C[j] > max_bonus:
max_bonus = number_of_candies * C[j]
print(max_bonus) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def total(root):
s = 0
if root:
s += root.data
s += total(root.left)
s += total(root.right)
return s
def modify(root):
temp = root
total_sum = total(root)
curr_sum = 0
def updated_values(root, total_sum):
nonlocal curr_sum
if root:
updated_values(root.left, total_sum)
copy = root.data
root.data = total_sum - curr_sum
curr_sum += copy
updated_values(root.right, total_sum)
updated_values(root, total_sum)
return root | FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
def inorder1(root, arr):
if not root:
return 0
inorder1(root.right, arr)
arr[0] = arr[0] + root.data
root.data = arr[0]
inorder1(root.left, arr)
arr = [0]
inorder1(root, arr)
return root | FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
_modify(root, [0])
return root
def _modify(node, pre):
if node:
_modify(node.right, pre)
aux = node.data
node.data += pre[0]
pre[0] += aux
_modify(node.left, pre) | FUNC_DEF EXPR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def root_inorder(root, maxi):
if not root:
return None
root_inorder(root.right, maxi)
root.data += maxi[0]
maxi[0] = root.data
root_inorder(root.left, maxi)
def modify(root):
root_inorder(root, [0])
return root | FUNC_DEF IF VAR RETURN NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
a = [0] * 1
def fun(root):
if root == None:
return
fun(root.right)
a[0] += root.data
root.data = a[0]
fun(root.left)
fun(root)
return root | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def postOrder(root):
l = []
q = set()
cur = root
while cur and cur not in q:
if cur.left and cur.left not in q:
cur = cur.left
elif cur.right and cur.right not in q:
cur = cur.right
else:
q.add(cur)
l.append(cur.data)
cur = root
return l
def modify(root):
q = []
cur = root
temp = 0
while True:
if cur is not None:
q.append(cur)
cur = cur.right
elif q:
cur = q.pop()
cur.data += temp
temp = cur.data
cur = cur.left
else:
break
return root | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
l = fn(root)
l.reverse()
foo = []
sm = 0
for i in l:
sm += i
foo += [sm]
foo.reverse()
pointer = [0]
updatetree(root, foo, pointer)
return root
def fn(r):
if not r:
return []
return fn(r.left) + [r.data] + fn(r.right)
def updatetree(root, foo, pointer):
if root:
updatetree(root.left, foo, pointer)
root.data = foo[pointer[0]]
pointer[0] += 1
updatetree(root.right, foo, pointer) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def util(root, sm=0):
if not root:
return 0
right = util(root.right, sm)
root.data += right + sm
left = util(root.left, root.data)
return left + root.data - sm
def modify(root):
util(root)
return root | FUNC_DEF NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
summ = [0]
util(root, summ)
return root
def util(root, summ):
if not root:
return
util(root.right, summ)
summ[0] += root.data
root.data = summ[0]
util(root.left, summ) | FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
def inorder(root):
if not root:
return
inorder(root.left)
res.append(root.data)
inorder(root.right)
def update(root):
nonlocal j, ans
if not root:
return
update(root.left)
root.data = ans[j]
j += 1
update(root.right)
res = []
inorder(root)
tot = 0
ans = []
for i in range(len(res) - 1, -1, -1):
tot += res[i]
ans.append(tot)
j = 0
ans.reverse()
update(root)
return root | FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(r):
sum = [d(r)]
T(r, sum)
return r
def T(r, sum):
if r:
T(r.left, sum)
sum[0] -= r.data
r.data = sum[0] + r.data
T(r.right, sum)
def d(r):
if r:
return r.data + d(r.left) + d(r.right)
return 0 | FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.