description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def hpartition(self, arr, left, right):
pivot = arr[left]
while True:
while arr[left] < pivot:
left += 1
while arr[right] > pivot:
right -= 1
if left >= right:
return left
arr[left], arr[right] = arr[right], arr[left]
return left
def quicksort(self, arr, left, right):
if right > left:
parti = self.hpartition(arr, left, right)
self.quicksort(arr, left, parti)
self.quicksort(arr, parti + 1, right)
def matchPairs(self, nuts, bolts, n):
self.quicksort(nuts, 0, len(nuts) - 1)
self.quicksort(bolts, 0, len(nuts) - 1)
return | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR WHILE NUMBER WHILE VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
order = {"!": 0, "#": 1, "$": 2, "%": 3, "&": 4, "*": 5, "@": 6, "^": 7, "~": 8}
nuts.sort(key=lambda x: order[x])
bolts.sort(key=lambda x: order[x])
return nuts, bolts | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
d = {
"!": 0,
"#": 1,
"$": 2,
"%": 3,
"&": 4,
"*": 5,
"@": 6,
"^": 7,
"~": 8,
".": 9,
}
d1 = {
(0): "!",
(1): "#",
(2): "$",
(3): "%",
(4): "&",
(5): "*",
(6): "@",
(7): "^",
(8): "~",
(9): ".",
}
nuts1 = []
bolts1 = []
for ele in nuts:
nuts1.append(d[ele])
for ele in bolts:
bolts1.append(d[ele])
nuts1 = sorted(nuts1)
bolts1 = sorted(bolts1)
while len(nuts) > 0:
nuts.remove(nuts[-1])
while len(bolts) > 0:
bolts.remove(bolts[-1])
for ele in nuts1:
nuts.append(d1[ele])
for ele in bolts1:
bolts.append(d1[ele]) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
s = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
for i in s:
if i in nuts:
nuts.append(i)
nuts.remove(i)
bolts.append(i)
bolts.remove(i) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
nuts.sort()
bolts.sort()
ans = []
for i in range(n):
ans.append([nuts[i], bolts[i]])
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
lst = list("!#$%&*@^~")
k = 0
for i in lst:
if i in bolts:
nuts[k] = i
k += 1
k = 0
for i in nuts:
bolts[k] = i
k += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
d = {"!": 0, "#": 1, "$": 2, "%": 3, "&": 4, "*": 5, "@": 6, "^": 7, "~": 8}
d_ = {
(0): "!",
(1): "#",
(2): "$",
(3): "%",
(4): "&",
(5): "*",
(6): "@",
(7): "^",
(8): "~",
}
for i in range(n):
nuts[i] = d[nuts[i]]
for i in range(n):
bolts[i] = d[bolts[i]]
nuts.sort()
bolts.sort()
for i in range(n):
nuts[i] = d_[nuts[i]]
for i in range(n):
bolts[i] = d_[bolts[i]] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, a, b, n):
temp = []
for i in range(len(b)):
temp.append(ord(b[i]))
temp.sort()
for i in range(len(temp)):
a[i] = b[i] = chr(temp[i]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def get_key(self, val, my_dict):
for key, value in my_dict.items():
if val == value:
return key
def matchPairs(self, nuts, bolts, n):
ele_order = {
"!": 0,
"#": 1,
"$": 2,
"%": 3,
"&": 4,
"*": 5,
"@": 6,
"^": 7,
"~": 8,
}
for i in range(n):
nuts[i] = ele_order[nuts[i]]
bolts[i] = ele_order[bolts[i]]
nuts.sort()
bolts.sort()
for i in range(n):
if nuts[i] == bolts[i]:
nuts[i] = bolts[i] = self.get_key(nuts[i], ele_order) | CLASS_DEF FUNC_DEF FOR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
st = "!#$%&*@^~"
d = {st[x]: x for x in range(len(st))}
nuts.sort(key=lambda s: d[s])
bolts.sort(key=lambda s: d[s]) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
ord = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
matcher = dict()
for i in nuts:
matcher[i] = 1
for j in bolts:
matcher[j] += 1
res = []
for o in ord:
if matcher.get(o) == 2:
res.append(o)
nuts.clear()
nuts.extend(res)
bolts.clear()
bolts.extend(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
l = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
p = 0
x = []
for i in range(len(l)):
if l[p] in nuts and l[p] in bolts:
x.append(l[p])
p = p + 1
for i in range(len(nuts)):
nuts[i] = x[i]
bolts[i] = x[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
nuts_hash = {}
for i, v in enumerate(nuts):
nuts_hash[v] = i
bolts_hash = {}
for i, v in enumerate(bolts):
bolts_hash[v] = i
i = 0
for c in "!#$%&*@^~":
if nuts_hash.get(c) != None and bolts_hash.get(c) != None:
nuts[i] = c
bolts[i] = c
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR STRING IF FUNC_CALL VAR VAR NONE FUNC_CALL VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
a = {"!": 0, "#": 1, "$": 2, "%": 3, "&": 4, "*": 5, "@": 6, "^": 7, "~": 8}
s = {
(0): "!",
(1): "#",
(2): "$",
(3): "%",
(4): "&",
(5): "*",
(6): "@",
(7): "^",
(8): "~",
}
b = []
c = []
for i in range(len(nuts)):
k = a[nuts[i]]
p = a[bolts[i]]
b.append(k)
c.append(p)
b.sort()
c.sort()
nuts.clear()
bolts.clear()
for i in range(len(b)):
nuts.append(s[b[i]])
bolts.append(s[c[i]]) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
return self.matchPairsUtil(nuts, bolts, 0, n - 1)
def matchPairsUtil(self, nuts, bolts, start, end):
if start < end:
pivot = self.partetion(nuts, start, end, bolts[end])
self.partetion(bolts, start, end, nuts[pivot])
self.matchPairsUtil(nuts, bolts, start, pivot - 1)
self.matchPairsUtil(nuts, bolts, pivot + 1, end)
def partetion(self, arr, start, end, pivot):
i = start
j = start
while j < end:
if arr[j] < pivot:
arr[i], arr[j] = arr[j], arr[i]
i += 1
if arr[j] == pivot:
arr[j], arr[end] = arr[end], arr[j]
j -= 1
j += 1
arr[i], arr[end] = arr[end], arr[i]
return i | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
order = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
hnuts = {i: j for i in order for j in [0] * 9}
for ele in nuts:
hnuts[ele] = 1
res = []
for i in order:
if hnuts[i]:
res.append(i)
nuts[:] = res
bolts[:] = res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
stg = "!#$%&*@^~"
dic = {}
for i in range(len(stg)):
dic[stg[i]] = i
nuts.sort(key=lambda x: dic[x])
bolts.sort(key=lambda x: dic[x]) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
ref = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
ans = []
for val in ref:
if val in nuts:
ans.append(val)
for i in range(len(nuts)):
nuts.pop()
for val in ans:
nuts.append(val)
for i in range(len(bolts)):
bolts.pop()
for val in ans:
bolts.append(val) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
l = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
nd = {}
bd = {}
for i in nuts:
if i not in nd:
nd[i] = 1
else:
nd[i] += 1
for i in bolts:
if i not in bd:
bd[i] = 1
else:
bd[i] += 1
dl = {}
for i in l:
if i in nd and i in bd:
k = min(bd[i], nd[i])
dl[i] = k
k = []
for i in l:
if i in dl:
for j in range(dl[i]):
k.append(i)
nuts[:] = k
bolts[:] = k | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
nd, bd = {}, {}
for n in nuts:
nd[n] = True
for b in bolts:
bd[b] = True
nuts.clear()
bolts.clear()
a = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
for i in a:
if nd.get(i) and bd.get(i):
nuts.append(i)
bolts.append(i)
return nuts, bolts | CLASS_DEF FUNC_DEF ASSIGN VAR VAR DICT DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
s = "!#$%&*@^~"
f = {}
for i in nuts:
f[i] = f.get(i, 0) + 1
j = 0
for i in s:
for k in range(f.get(i, 0)):
nuts[j] = i
bolts[j] = i
j += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
res = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
ans = [0] * len(res)
for i in nuts:
ans[res.index(i)] = 1
ind = 0
for i in range(len(ans)):
if ans[i]:
nuts[ind] = res[i]
bolts[ind] = res[i]
ind += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
arr = ["!", "#", "$", "%", "&", "*", "@", "^", "~"]
ans1 = []
for i in arr:
if i in nuts:
ans1.append(i)
for i in range(len(nuts)):
nuts[i] = ans1[i]
bolts[i] = ans1[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
d = {"!": 1, "#": 2, "$": 3, "%": 4, "&": 5, "*": 6, "@": 7, "^": 8, "~": 9}
x = [[] for i in range(1, 10)]
for i in nuts:
x[d[i] - 1].append(i)
result1 = []
for i in x:
if len(i) > 0:
result1.extend(i)
for i in range(len(result1)):
nuts[i] = result1[i]
for i in range(len(nuts)):
bolts[i] = nuts[i] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
nuts.sort()
bolts.sort()
i, j = 0, 0
list1 = []
k, l = 0, 0
while i < n and j < n:
if nuts[i] == bolts[j]:
nuts[k] = nuts[i]
k += 1
bolts[l] = bolts[j]
l += 1
i += 1
j += 1
elif nuts[i] > bolts[j]:
j += 1
elif nuts[i] < bolts[j]:
i += 1
return nuts, bolts | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bots, n):
a = "!#$%&*@^~"
h = {"!": 1, "#": 2, "$": 3, "%": 4, "&": 5, "*": 6, "@": 7, "^": 8, "~": 9}
for i in range(n):
nuts[i] = h[nuts[i]]
bots[i] = h[bots[i]]
nuts.sort()
bots.sort()
for i in range(n):
nuts[i] = a[nuts[i] - 1]
bots[i] = a[bots[i] - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
o = "!#$%&*@^~"
dic = {}
for i in range(len(o)):
dic[o[i]] = i
def helper(s, e, pi, arr):
i = s
while s < e:
if dic[arr[s]] < pi:
arr[s], arr[i] = arr[i], arr[s]
i += 1
if dic[arr[s]] == pi:
arr[e], arr[s] = arr[s], arr[e]
s -= 1
s += 1
arr[e], arr[i] = arr[i], arr[e]
return i
def p(l, r, arr):
if l < r:
pi = helper(l, r, dic[arr[r]], arr)
p(l, pi - 1, arr)
p(pi + 1, r, arr)
p(0, len(nuts) - 1, nuts)
p(0, len(nuts) - 1, bolts) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
d = {}
for i in nuts:
d[ord(i)] = i
l = list(d.keys())
l.sort()
nuts.clear()
for i in l:
nuts.append(d[i])
bolts.clear()
bolts.extend(nuts) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
mapping = {"!": 1, "#": 2, "$": 3, "%": 4, "&": 5, "*": 6, "@": 7, "^": 8, "~": 9}
def merge_arr(self, low, mid, high, arr):
l = low
h = mid + 1
k = 0
ans = list()
while l <= mid and h <= high:
if self.mapping[arr[l]] < self.mapping[arr[h]]:
ans.append(arr[l])
l += 1
else:
ans.append(arr[h])
h += 1
while l <= mid:
ans.append(arr[l])
l += 1
while h <= high:
ans.append(arr[h])
h += 1
for i in range(low, high + 1):
arr[i] = ans.pop(0)
def merge_sort(self, low, high, arr):
if low < high:
mid = (high + low) // 2
self.merge_sort(low, mid, arr)
self.merge_sort(mid + 1, high, arr)
self.merge_arr(low, mid, high, arr)
def matchPairs(self, nuts, bolts, n):
self.merge_sort(0, n - 1, nuts)
self.merge_sort(0, n - 1, bolts) | CLASS_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a set of N nuts of different sizes and N bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input:
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output:
# $ % @ ^
# $ % @ ^
Example 2:
Input:
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output:
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~
Your Task:
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}. | class Solution:
def matchPairs(self, nuts, bolts, n):
d = {"!": 1, "#": 2, "$": 3, "%": 4, "&": 5, "*": 6, "@": 7, "^": 8, "~": 9}
for i in range(len(nuts) - 1):
for j in range(len(nuts) - 1 - i):
if nuts[j] > nuts[j + 1]:
nuts[j], nuts[j + 1] = nuts[j + 1], nuts[j]
for i in range(len(bolts) - 1):
for j in range(len(bolts) - 1 - i):
if bolts[j] > bolts[j + 1]:
bolts[j], bolts[j + 1] = bolts[j + 1], bolts[j] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
even, odd = 0, 0
s = 0
ans = 0
for i in arr:
s += i
if s % 2 == 0:
even += 1
else:
odd += 1
ans = even * (even - 1) // 2 + odd * (odd - 1) // 2 + even
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
odd = 0
even = 0
summa = 0
for item in arr:
summa += item
odd += summa % 2
even += summa % 2 == 0
count = even
summa = 0
for item in arr:
summa += item
if summa % 2:
odd -= 1
count += odd
else:
even -= 1
count += even
return count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
eve = odd = s = out = 0
for i in range(n):
s += arr[i]
if s % 2 == 0:
out += 1 + eve
eve += 1
else:
out += odd
odd += 1
return out | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
even = 0
odd = 0
sum = 0
r = 0
for i in range(n):
sum += arr[i]
if sum % 2 != 0:
r += odd
odd = odd + 1
else:
r += even + 1
even = even + 1
return r | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
sum = 0
t = 0
for i in reversed(range(n)):
if arr[i] % 2 == 1:
t = n - i - 1 - t
else:
t = t + 1
sum = sum + t
return sum
if __name__ == "__main__":
tc = int(input())
while tc > 0:
n = int(input())
arr = list(map(int, input().strip().split()))
ob = Solution()
ans = ob.countEvenSum(arr, n)
print(ans)
tc -= 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
s = 0
res = 0
for i in range(n - 1, -1, -1):
if arr[i] & 1:
s = n - i - 1 - s
else:
s = s + 1
res = res + s
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
o, e, c = 0, 0, 0
for i in range(n):
if arr[i] % 2 == 0:
o = o
e += 1
else:
t = e
e = o
o = t + 1
c += e
return c | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
ce, co = 0, 0
s = 0
for x in arr:
if x % 2 == 0:
ce += 1
else:
old_co = co
co = ce + 1
ce = old_co
s += ce
return s | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
temp = [1, 0]
result = 0
sum = 0
for i in range(n):
sum = ((sum + arr[i]) % 2 + 2) % 2
temp[sum] += 1
result = result + temp[0] * (temp[0] - 1) // 2
result = result + temp[1] * (temp[1] - 1) // 2
return result | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
cnt = [0, 0]
ans = 0
for a in arr:
if a % 2 == 0:
ans += cnt[0] + 1
cnt[0] += 1
else:
ans += cnt[1]
cnt[0], cnt[1] = cnt[1], cnt[0] + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
csm = odd = ev = 0
for i in arr:
csm += i
if csm & 1:
odd += 1
else:
ev += 1
ans = n * (n + 1) // 2 - odd * (1 + ev)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
e = 0
o = 0
s = 0
ans = 0
for i in range(n):
s += arr[i]
if s % 2 == 0:
ans += 1 + e
e += 1
else:
ans += o
o += 1
return ans
if __name__ == "__main__":
tc = int(input())
while tc > 0:
n = int(input())
arr = list(map(int, input().strip().split()))
ob = Solution()
ans = ob.countEvenSum(arr, n)
print(ans)
tc -= 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
ans = 0
sm = 0
eve, odd = 0, 0
for i in range(n):
sm += arr[i]
if sm % 2 != 0:
ans += odd
odd += 1
else:
ans += eve + 1
eve += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
sub_parity = [0, 0]
total_even = 0
odd = 0
even = 0
for i in range(n):
if arr[i] & 1:
odd = sub_parity[0] + 1
even = sub_parity[1]
else:
odd = sub_parity[1]
even = sub_parity[0] + 1
sub_parity = [even, odd]
total_even += even
return total_even | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | def countSpecial(n, arr):
e, o = 0, 0
if arr[0] % 2 == 1:
o += 1
else:
e += 1
for i in range(1, n):
arr[i] = arr[i - 1] + arr[i]
if arr[i] % 2 == 1:
o += 1
else:
e += 1
result = e * (e + 1) // 2
result += o * (o + 1) // 2 - o
return result
class Solution:
def countEvenSum(self, arr, n):
return countSpecial(n, arr) | FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
c = 0
f = [0, 0]
s = 0
for i in range(n):
s += arr[i]
if s % 2 == 0:
c += f[0] + 1
f[0] += 1
else:
c += f[1]
f[1] += 1
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
currsum = 0
hm = {}
count = 0
for i in range(n):
currsum += arr[i]
if currsum % 2 == 0:
count += 1
if hm.get(2):
count += hm[2]
hm[2] += 1
else:
hm[2] = 1
elif hm.get(1):
count += hm[1]
hm[1] += 1
else:
hm[1] = 1
return count
if __name__ == "__main__":
tc = int(input())
while tc > 0:
n = int(input())
arr = list(map(int, input().strip().split()))
ob = Solution()
ans = ob.countEvenSum(arr, n)
print(ans)
tc -= 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
sm = 0
ev, od = 0, 0
for i in range(n):
sm += arr[i]
if sm % 2 == 0:
ev += 1
else:
od += 1
return ev * (ev + 1) // 2 + od * (od - 1) // 2 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
odd = 0
even = 0
cnt = 0
for i in range(len(arr)):
if arr[i] % 2 == 0:
even = even + 1
else:
tmp = even
even = odd
odd = tmp + 1
cnt += even
return cnt | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given an array Arr[] of size N. Find the number of subarrays whose sum is an even number.
Example 1:
Input:
N = 6
Arr[] = {1, 2, 2, 3, 4, 1}
Output: 9
Explanation: The array {1, 2, 2, 3, 4, 1}
has 9 such possible subarrays. These are-
{1, 2, 2, 3} Sum = 8
{1, 2, 2, 3, 4} Sum = 12
{2} Sum = 2 (At index 1)
{2, 2} Sum = 4
{2, 2, 3, 4, 1} Sum = 12
{2} Sum = 2 (At index 2)
{2, 3, 4, 1} Sum = 10
{3, 4, 1} Sum = 8
{4} Sum = 4
Example 2:
Input:
N = 4
Arr[] = {1, 3, 1, 1}
Output: 4
Explanation: The array {1, 3, 1, 1}
has 4 such possible subarrays.
These are-
{1, 3} Sum = 4
{1, 3, 1, 1} Sum = 6
{3, 1} Sum = 4
{1, 1} Sum = 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function countEvenSum() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= Arr[i] <=10^{9} | class Solution:
def countEvenSum(self, arr, n):
even = 0
odd = 0
sume = 0
ans = 0
for a in arr:
sume += a
if sume % 2 == 0:
ans += 1 + even
even += 1
else:
ans += odd
odd += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
mod = 1000000007
dicti = {x: (1) for x in arr}
newarr = sorted(dicti.keys())
sol = 0
for i in newarr:
for j in range(2 * i, min(i * i + 1, newarr[-1] + 1), i):
if j in dicti and j // i in dicti:
temp = dicti[i] * dicti[j // i]
if i * i != j:
temp = 2 * temp
dicti[j] += temp
sol += dicti[i]
return sol % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
ans = 0
min_a = min(arr)
max_a = max(arr)
dp = [0] * (max_a + 1)
for i in arr:
dp[i] = 1
for i in range(min_a, max_a + 1):
if dp[i] != 0:
j = i + i
while j <= max_a and j / i <= i:
if dp[j] != 0:
dp[j] += dp[i] * dp[j // i]
if j / i != i:
dp[j] += dp[i] * dp[j // i]
j += i
ans = ans + dp[i]
return ans % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
N = max(arr) + 2
mod = int(1000000000.0) + 7
chk = [0] * N
ans = 0
for i in range(n):
chk[arr[i]] = 1
for i in range(N):
if chk[i]:
for j in range(2 * i, N, i):
if chk[j] == 0 or j // i > i:
continue
if j // i != i:
chk[j] = (chk[j] + 2 * (chk[i] * chk[j // i] % mod) % mod) % mod
else:
chk[j] = (chk[j] + chk[i] * chk[j // i] % mod) % mod
for i in range(N):
ans = (ans + chk[i]) % mod
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
class Solution:
def numoffbt(self, arr, n):
MOD1 = 1000000007
max_var = max(arr)
min_var = min(arr)
dp1 = [0] * (max_var + 1)
for i in arr:
dp1[i] = 1
ans1 = 0
for i in range(min_var, max_var + 1):
if dp1[i] != 0:
j = 2 * i
while j <= max_var and j // i <= i:
if dp1[j] != 0:
dp1[j] += dp1[i] * dp1[j // i]
if j // i != i:
dp1[j] += dp1[i] * dp1[j // i]
j += i
ans1 += dp1[i]
ans1 = ans1 % MOD1
return ans1 | FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
min1 = 9999999999
max1 = -999999999
for i in arr:
min1 = min(min1, i)
max1 = max(max1, i)
dp = [0] * (max1 + 1)
for i in range(n):
dp[arr[i]] = 1
c = 0
for i in range(min1, max1 + 1):
if dp[i] > 0:
j = i + i
while j <= max1 and j <= i * i:
if dp[j] > 0:
dp[j] = dp[j] + dp[i] * dp[j // i]
if i != j // i:
dp[j] = dp[j] + dp[i] * dp[j // i]
j = j + i
return sum(dp) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
mod = 10**9 + 7
maximum = max(arr)
dp = [(0) for i in range(maximum + 1)]
for i in arr:
dp[i] = 1
ans = 0
for i in range(2, maximum + 1):
if dp[i] == 0:
continue
ans += dp[i]
for j in range(2 * i, maximum + 1, i):
if dp[j] == 0 or j // i > i:
continue
if i != j // i:
dp[j] += dp[i] * dp[j // i]
dp[j] += dp[i] * dp[j // i]
dp[j] = dp[j] % mod
return ans % mod | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
maxvalue = -float("inf")
minvalue = float("inf")
for i in range(n):
maxvalue = max(maxvalue, arr[i])
minvalue = min(minvalue, arr[i])
mark = [0] * (maxvalue + 2)
value = [0] * (maxvalue + 2)
for i in range(n):
mark[arr[i]] = 1
value[arr[i]] = 1
ans = 0
mod = 10**9 + 7
for i in range(minvalue, maxvalue + 1):
if mark[i]:
j = i + i
while j <= maxvalue and j // i <= i:
if not mark[j]:
j += i
continue
value[j] = (value[j] + value[i] * value[j // i]) % mod
if i != j // i:
value[j] = (value[j] + value[i] * value[j // i]) % mod
j += i
ans = (ans + value[i]) % mod
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.
Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007
Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2
Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.
Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ n ≤ 10^{5}
2 ≤ arr[i] ≤ 10^{5} | class Solution:
def numoffbt(self, arr, n):
mod = 10**9 + 7
maxrange = max(arr)
minrange = min(arr)
present = {arr[i]: (True) for i in range(0, n)}
dp = [0] * (maxrange + 2)
for i in range(0, n):
present[arr[i]] = True
dp[arr[i]] = 1
ans = 0
for i in range(minrange, maxrange + 1):
if i in present:
j = 2 * i
while j <= i * i and j <= maxrange:
if j in present:
if i != j // i:
dp[j] = (dp[j] + 2 * (dp[i] * dp[j // i])) % mod
elif i == j // i:
dp[j] = (dp[j] + dp[i] * dp[j // i]) % mod
j += i
ans = (ans + dp[i]) % mod
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR |
Read problems statements in Mandarin Chinese and Russian.
Chef likes trees a lot. Today he has an infinte full binary tree (each node has exactly two childs) with special properties.
Chef's tree has the following special properties :
Each node of the tree is either colored red or black.
Root of the tree is black intially.
Both childs of a red colored node are black and both childs of a black colored node are red.
The root of the tree is labelled as 1. For a node labelled v, it's left child is labelled as 2*v and it's right child is labelled as 2*v+1.
Chef wants to fulfill Q queries on this tree. Each query belongs to any of the following three types:
Qi Change color of all red colored nodes to black and all black colored nodes to red.
Qb x y Count the number of black colored nodes on the path from node x to node y (both inclusive).
Qr x y Count the number of red colored nodes on the path from node x to node y (both inclusive).
Help chef accomplishing this task.
------ Input Format ------
First line of the input contains an integer Q denoting the number of queries. Next Q lines of the input contain Q queries (one per line). Each query belongs to one of the three types mentioned above.
------ Output Format ------
For each query of type Qb or Qr, print the required answer.
------ Constraints ------
1≤Q≤10^{5}
1≤x,y≤10^{9}
Scoring
$ Subtask #1: 1≤Q≤100 1≤x,y≤1000 : 27 pts$
$ Subtask #2: 1≤Q≤10^{3} 1≤x,y≤10^{5} : 25 pts$
$ Subtask #3: 1≤Q≤10^{5} 1≤x,y≤10^{9} : 48 pts$
----- Sample Input 1 ------
5
Qb 4 5
Qr 4 5
Qi
Qb 4 5
Qr 4 5
----- Sample Output 1 ------
2
1
1
2
----- explanation 1 ------
With the initial configuration of the tree, Path from node 4 to node 5 is 4->2->5 and color of nodes on the path is B->R->B.
Number of black nodes are 2.
Number of red nodes are 1.
After Query Qi, New configuration of the path from node 4 to node 5 is R->B->R.
Number of black nodes are 1.
Number of red nodes are 2. | root = 0
for _ in range(int(input())):
q = list(map(str, input().split()))
if q[0] == "Qi":
root = (root + 1) % 2
else:
x = int(q[1])
y = int(q[2])
xca = []
yca = []
lxca = 0
lyca = 0
while x > 0:
lxca += 1
xca.append(x)
if x & 1:
x -= 1
x //= 2
else:
x //= 2
while y > 0:
lyca += 1
yca.append(y)
if y & 1:
y -= 1
y //= 2
else:
y //= 2
lx = lxca - 1
ly = lyca - 1
lca_dist = 0
while lx >= 0 and ly >= 0:
if xca[lx] == yca[ly]:
lca_dist += 1
else:
break
lx -= 1
ly -= 1
lca_color = 0
if lca_dist & 1:
lca_color = root
else:
lca_color = (root + 1) % 2
ans = 0
accept1 = 0
accept2 = 0
if q[0] == "Qb":
if lca_color == 0:
accept1 = 0
accept2 = 1
else:
accept1 = 1
accept2 = 0
elif lca_color == 1:
accept1 = 0
accept2 = 1
else:
accept1 = 1
accept2 = 0
for i in range(lca_dist, lxca + 1):
if accept1 == 0:
ans += 1
accept1 = (accept1 + 1) % 2
for i in range(lca_dist + 1, lyca + 1):
if accept2 == 0:
ans += 1
accept2 = (accept2 + 1) % 2
print(ans) | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian.
Chef likes trees a lot. Today he has an infinte full binary tree (each node has exactly two childs) with special properties.
Chef's tree has the following special properties :
Each node of the tree is either colored red or black.
Root of the tree is black intially.
Both childs of a red colored node are black and both childs of a black colored node are red.
The root of the tree is labelled as 1. For a node labelled v, it's left child is labelled as 2*v and it's right child is labelled as 2*v+1.
Chef wants to fulfill Q queries on this tree. Each query belongs to any of the following three types:
Qi Change color of all red colored nodes to black and all black colored nodes to red.
Qb x y Count the number of black colored nodes on the path from node x to node y (both inclusive).
Qr x y Count the number of red colored nodes on the path from node x to node y (both inclusive).
Help chef accomplishing this task.
------ Input Format ------
First line of the input contains an integer Q denoting the number of queries. Next Q lines of the input contain Q queries (one per line). Each query belongs to one of the three types mentioned above.
------ Output Format ------
For each query of type Qb or Qr, print the required answer.
------ Constraints ------
1≤Q≤10^{5}
1≤x,y≤10^{9}
Scoring
$ Subtask #1: 1≤Q≤100 1≤x,y≤1000 : 27 pts$
$ Subtask #2: 1≤Q≤10^{3} 1≤x,y≤10^{5} : 25 pts$
$ Subtask #3: 1≤Q≤10^{5} 1≤x,y≤10^{9} : 48 pts$
----- Sample Input 1 ------
5
Qb 4 5
Qr 4 5
Qi
Qb 4 5
Qr 4 5
----- Sample Output 1 ------
2
1
1
2
----- explanation 1 ------
With the initial configuration of the tree, Path from node 4 to node 5 is 4->2->5 and color of nodes on the path is B->R->B.
Number of black nodes are 2.
Number of red nodes are 1.
After Query Qi, New configuration of the path from node 4 to node 5 is R->B->R.
Number of black nodes are 1.
Number of red nodes are 2. | ini = {"odd": "black", "even": "red"}
for _ in range(int(input())):
q = [i for i in input().split()]
if q[0] == "Qi":
temp = ini["odd"]
ini["odd"] = ini["even"]
ini["even"] = temp
else:
a, b = f"{int(q[1]):b}", f"{int(q[2]):b}"
k = 0
count = {"black": 0, "red": 0}
for i, j in zip(a, b):
if i != j:
break
else:
k += 1
dist = len(a) + len(b) - 2 * k
if (dist + 1) % 2 == 0:
count["red"] = count["black"] = (dist + 1) / 2
else:
if len(a) % 2 == 0:
count[ini["even"]] += 1
else:
count[ini["odd"]] += 1
count["red"] += dist / 2
count["black"] += dist / 2
if q[0] == "Qb":
print(int(count["black"]))
else:
print(int(count["red"])) | ASSIGN VAR DICT STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR STRING VAR STRING ASSIGN VAR STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING VAR STRING BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR STRING NUMBER VAR VAR STRING NUMBER VAR STRING BIN_OP VAR NUMBER VAR STRING BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
Read problems statements in Mandarin Chinese and Russian.
Chef likes trees a lot. Today he has an infinte full binary tree (each node has exactly two childs) with special properties.
Chef's tree has the following special properties :
Each node of the tree is either colored red or black.
Root of the tree is black intially.
Both childs of a red colored node are black and both childs of a black colored node are red.
The root of the tree is labelled as 1. For a node labelled v, it's left child is labelled as 2*v and it's right child is labelled as 2*v+1.
Chef wants to fulfill Q queries on this tree. Each query belongs to any of the following three types:
Qi Change color of all red colored nodes to black and all black colored nodes to red.
Qb x y Count the number of black colored nodes on the path from node x to node y (both inclusive).
Qr x y Count the number of red colored nodes on the path from node x to node y (both inclusive).
Help chef accomplishing this task.
------ Input Format ------
First line of the input contains an integer Q denoting the number of queries. Next Q lines of the input contain Q queries (one per line). Each query belongs to one of the three types mentioned above.
------ Output Format ------
For each query of type Qb or Qr, print the required answer.
------ Constraints ------
1≤Q≤10^{5}
1≤x,y≤10^{9}
Scoring
$ Subtask #1: 1≤Q≤100 1≤x,y≤1000 : 27 pts$
$ Subtask #2: 1≤Q≤10^{3} 1≤x,y≤10^{5} : 25 pts$
$ Subtask #3: 1≤Q≤10^{5} 1≤x,y≤10^{9} : 48 pts$
----- Sample Input 1 ------
5
Qb 4 5
Qr 4 5
Qi
Qb 4 5
Qr 4 5
----- Sample Output 1 ------
2
1
1
2
----- explanation 1 ------
With the initial configuration of the tree, Path from node 4 to node 5 is 4->2->5 and color of nodes on the path is B->R->B.
Number of black nodes are 2.
Number of red nodes are 1.
After Query Qi, New configuration of the path from node 4 to node 5 is R->B->R.
Number of black nodes are 1.
Number of red nodes are 2. | def dec_to_bin(z):
return int(bin(z)[2:])
testCases = int(input())
baseBlack = 1
for i in range(testCases):
theStr = input()
if theStr == "Qi":
baseBlack = -baseBlack
else:
Color, Fir, Sec = theStr.split()
if Color[1] == "b":
theColor = 1
else:
theColor = -1
Fir = int(Fir)
Sec = int(Sec)
x = str(dec_to_bin(max(Fir, Sec)))
ynum = dec_to_bin(min(Fir, Sec))
ystr = str(ynum)
lenx = len(x)
leny = len(ystr)
newx = int(x[0:leny])
if newx == ynum:
leveldown = 0
else:
leveldown = len(str(abs(2 * newx - 2 * ynum)))
dist = leveldown * 2 + lenx - leny + 1
if baseBlack * theColor * (lenx % 2 * 2 - 1) == 1:
print(int(dist / 2 + 0.6))
else:
print(int(dist / 2)) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian.
Chef likes trees a lot. Today he has an infinte full binary tree (each node has exactly two childs) with special properties.
Chef's tree has the following special properties :
Each node of the tree is either colored red or black.
Root of the tree is black intially.
Both childs of a red colored node are black and both childs of a black colored node are red.
The root of the tree is labelled as 1. For a node labelled v, it's left child is labelled as 2*v and it's right child is labelled as 2*v+1.
Chef wants to fulfill Q queries on this tree. Each query belongs to any of the following three types:
Qi Change color of all red colored nodes to black and all black colored nodes to red.
Qb x y Count the number of black colored nodes on the path from node x to node y (both inclusive).
Qr x y Count the number of red colored nodes on the path from node x to node y (both inclusive).
Help chef accomplishing this task.
------ Input Format ------
First line of the input contains an integer Q denoting the number of queries. Next Q lines of the input contain Q queries (one per line). Each query belongs to one of the three types mentioned above.
------ Output Format ------
For each query of type Qb or Qr, print the required answer.
------ Constraints ------
1≤Q≤10^{5}
1≤x,y≤10^{9}
Scoring
$ Subtask #1: 1≤Q≤100 1≤x,y≤1000 : 27 pts$
$ Subtask #2: 1≤Q≤10^{3} 1≤x,y≤10^{5} : 25 pts$
$ Subtask #3: 1≤Q≤10^{5} 1≤x,y≤10^{9} : 48 pts$
----- Sample Input 1 ------
5
Qb 4 5
Qr 4 5
Qi
Qb 4 5
Qr 4 5
----- Sample Output 1 ------
2
1
1
2
----- explanation 1 ------
With the initial configuration of the tree, Path from node 4 to node 5 is 4->2->5 and color of nodes on the path is B->R->B.
Number of black nodes are 2.
Number of red nodes are 1.
After Query Qi, New configuration of the path from node 4 to node 5 is R->B->R.
Number of black nodes are 1.
Number of red nodes are 2. | t = int(input())
flag = False
while t:
t -= 1
inp = input()
inp = inp.split()
query = inp[0]
if query == "Qi":
if flag == True:
flag = False
else:
flag = True
else:
x = int(inp[1])
y = int(inp[2])
c = 0
start = "Black"
while True:
if x < y:
y = int(y / 2)
c += 1
elif x > y:
x = int(x / 2)
c += 1
if start == "Black":
start = "Red"
else:
start = "Black"
else:
c += 1
if x != 1:
while x != 1:
x = int(x / 2)
if start == "Black":
start = "Red"
else:
start = "Black"
if start != "Black":
start = "Red"
break
BLACK = int(c / 2)
RED = int(c / 2)
if c % 2 == 1:
if start == "Black":
BLACK += 1
else:
RED += 1
if query == "Qb":
if flag:
print(RED)
else:
print(BLACK)
elif query == "Qr":
if flag:
print(BLACK)
else:
print(RED) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian.
Chef likes trees a lot. Today he has an infinte full binary tree (each node has exactly two childs) with special properties.
Chef's tree has the following special properties :
Each node of the tree is either colored red or black.
Root of the tree is black intially.
Both childs of a red colored node are black and both childs of a black colored node are red.
The root of the tree is labelled as 1. For a node labelled v, it's left child is labelled as 2*v and it's right child is labelled as 2*v+1.
Chef wants to fulfill Q queries on this tree. Each query belongs to any of the following three types:
Qi Change color of all red colored nodes to black and all black colored nodes to red.
Qb x y Count the number of black colored nodes on the path from node x to node y (both inclusive).
Qr x y Count the number of red colored nodes on the path from node x to node y (both inclusive).
Help chef accomplishing this task.
------ Input Format ------
First line of the input contains an integer Q denoting the number of queries. Next Q lines of the input contain Q queries (one per line). Each query belongs to one of the three types mentioned above.
------ Output Format ------
For each query of type Qb or Qr, print the required answer.
------ Constraints ------
1≤Q≤10^{5}
1≤x,y≤10^{9}
Scoring
$ Subtask #1: 1≤Q≤100 1≤x,y≤1000 : 27 pts$
$ Subtask #2: 1≤Q≤10^{3} 1≤x,y≤10^{5} : 25 pts$
$ Subtask #3: 1≤Q≤10^{5} 1≤x,y≤10^{9} : 48 pts$
----- Sample Input 1 ------
5
Qb 4 5
Qr 4 5
Qi
Qb 4 5
Qr 4 5
----- Sample Output 1 ------
2
1
1
2
----- explanation 1 ------
With the initial configuration of the tree, Path from node 4 to node 5 is 4->2->5 and color of nodes on the path is B->R->B.
Number of black nodes are 2.
Number of red nodes are 1.
After Query Qi, New configuration of the path from node 4 to node 5 is R->B->R.
Number of black nodes are 1.
Number of red nodes are 2. | reverse = 0
for _ in range(int(input())):
s = input().split()
if s[0] == "Qb" or s[0] == "Qr":
x = int(s[1])
y = int(s[2])
binx = bin(x)[2:]
biny = bin(y)[2:]
m = len(binx)
n = len(biny)
a = 0
if m < n:
while a < m and binx[a] == biny[a]:
a += 1
else:
while a < n and binx[a] == biny[a]:
a += 1
pathx = [x]
start = m
while start != a:
x //= 2
pathx.append(x)
start -= 1
start = n
pathy = [y]
while start != a:
y //= 2
pathy.append(y)
start -= 1
r = 0
b = 0
for i in range(len(pathx)):
if pathx[i].bit_length() % 2 == 0:
r += 1
else:
b += 1
for i in range(len(pathy) - 1):
if pathy[i].bit_length() % 2 == 0:
r += 1
else:
b += 1
if not reverse:
if s[0] == "Qb":
print(b)
else:
print(r)
elif s[0] == "Qb":
print(r)
else:
print(b)
else:
reverse ^= 1 | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
numStack = []
for num in S:
while K > 0 and len(numStack) > 0 and num < numStack[-1]:
numStack.pop()
K -= 1
else:
numStack.append(num)
if K > 0:
for _ in range(K):
numStack.pop()
return int("".join(numStack)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
num = ""
num = s
st = list()
for n in num:
while st and k and st[-1] > n:
st.pop()
k -= 1
if st or n is not "0":
st.append(n)
if k:
st = st[0:-k]
return "".join(st) or "0" | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR RETURN FUNC_CALL STRING VAR STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
res = []
counter = 0
n = len(S)
if n == K:
return "0"
for i in range(n):
while K and res and res[-1] > S[i]:
res.pop()
K -= 1
res.append(S[i])
while K:
res.pop()
K -= 1
return "".join(res).lstrip("0") or "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL FUNC_CALL STRING VAR STRING STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, num, k):
s = []
for i in num:
while s and int(i) < s[-1] and k != 0:
s.pop()
k -= 1
s.append(int(i))
while k:
s.pop()
k -= 1
ans = ""
for j in s:
ans += str(j)
return ans.lstrip("0") or "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
if k >= len(s):
return 0
if k == 0:
return s
st = [s[0]]
for i in s[1:]:
while k > 0 and st and i < st[-1]:
st.pop()
k -= 1
st.append(i)
st = st[: len(st) - k]
return int("".join(st)) | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = ["0"]
for item in S:
while K and stack[-1] > item:
stack.pop()
K -= 1
stack.append(item)
return int("".join(stack[:-K])) if K else int("".join(stack)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING FOR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
ans = ""
for i in range(len(S)):
while len(stack) != 0 and K != 0 and stack[-1] > int(S[i]):
stack.pop()
K -= 1
if len(stack) == 0 and int(S[i]) == 0:
continue
else:
stack.append(int(S[i]))
while len(stack) != 0 and K != 0:
stack.pop()
K -= 1
if len(stack) == 0:
return "0"
for i in range(len(stack) - 1, -1, -1):
ans = str(stack[i]) + ans
i = 0
while ans[i] == "0":
i += 1
return ans[i:] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER RETURN VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
st = []
for i in s:
while st and k > 0 and st[-1] > i:
st.pop()
k -= 1
st.append(i)
if k:
st = st[0:-k]
res = "".join(st)
return str(int(res)) if res else "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, arr, k):
n = len(arr)
stk = []
for i in arr:
while stk and k > 0 and int(stk[-1]) > int(i):
stk.pop()
k -= 1
if stk or int(i) > 0:
stk.append(i)
while stk and k > 0:
k -= 1
stk.pop()
if len(stk) == 0:
return 0
word = ""
for i in stk:
word += i
return word | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
stack = []
for i in s:
while stack and stack[-1] > i and k > 0:
stack.pop()
k -= 1
stack.append(i)
stack = stack[: len(stack) - k]
r = "".join(stack)
return str(int(r)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, k):
l = len(S)
ans = ""
st = []
for i in range(l):
while st and k > 0 and st[-1] > S[i]:
st.pop()
k -= 1
st.append(S[i])
if k > 0:
while k > 0:
st.pop()
k -= 1
while st:
ans = st[-1] + ans
st.pop()
i = 0
while i < len(ans) and ans[i] == "0":
i += 1
ans = ans[i:]
if not ans:
return "0"
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR RETURN STRING RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = "0"
while K:
if not S:
return int(stack[:-K])
elif stack[-1] > S[0]:
stack = stack[:-1]
K -= 1
else:
stack += S[0]
S = S[1:]
return int(stack + S) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING WHILE VAR IF VAR RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
ans = []
for c in S:
while len(ans) and ans[-1] > c and K > 0:
ans.pop()
K -= 1
if len(ans) > 0 or c != "0":
ans.append(c)
while len(ans) and K > 0:
K -= 1
ans.pop()
if len(ans) == 0:
return "0"
S2 = ""
for c in ans:
S2 += c
return S2 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
S = int(S)
S = str(S)
for i in S:
if len(stack) == 0:
stack.append(i)
else:
num = stack[-1]
while num > i and K > 0:
x = stack.pop()
K -= 1
if len(stack) > 0:
num = stack[-1]
else:
break
stack.append(i)
while K > 0:
stack.pop()
K = K - 1
if len(stack) == 0:
stack = [0]
return "0"
result = "".join(stack)
result = int(result)
result = str(result)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER RETURN STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
n = len(s)
if n == k:
return 0
stack = []
stack.append(s[0])
for i in range(1, n):
while stack and s[i] < stack[-1] and k > 0:
stack.pop()
k -= 1
stack.append(s[i])
while stack and k > 0:
stack.pop()
k -= 1
ans = "".join(stack)
return int(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
stk = []
res = []
ans = ""
n = len(s)
for i in range(n - 1, -1, -1):
while len(stk) > 0 and s[i] <= s[stk[-1]]:
stk.pop()
if len(stk) == 0:
res.append(n)
else:
res.append(stk[-1])
stk.append(i)
res = res[::-1]
for i in range(n):
res[i] = res[i] - i
i = 0
while i < n:
if s[i] == "0":
ans += "0"
i += 1
elif res[i] <= k:
k -= res[i]
i += res[i]
else:
ans += s[i]
i += 1
cnt0 = 0
i = 0
f = 0
while i < len(ans) and ans[i] == "0":
if ans[i] == "0":
cnt0 += 1
i += 1
else:
f = 1
break
if i == len(ans) or len(ans) == 0:
return "0"
return ans[i:] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, k):
arr = []
for r in S:
arr.append(r)
ans = ""
stck = []
lis = []
i = 0
while i < len(arr):
if k > 0:
if arr[i] == 0:
i += 1
pass
if len(stck) == 0:
stck.append(arr[i])
i += 1
elif stck[-1] > arr[i]:
lis.append(stck.pop())
k -= 1
else:
stck.append(arr[i])
i += 1
else:
break
while k > 0:
lis.append(stck.pop())
k -= 1
for q in lis:
arr.remove(q)
while 0 in arr:
arr.remove(0)
for w in arr:
ans += str(w)
return int(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
st = []
for i in S:
ele = int(i)
while len(st) and K > 0 and ele < st[-1]:
st.pop()
K = K - 1
if ele != 0 or len(st) != 0:
st.append(ele)
while len(st) and K > 0:
st.pop()
K = K - 1
if len(st) == 0:
return "0"
return "".join([str(item) for item in st]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
st = []
for i in S:
while st and K and st[-1] > i:
st.pop()
K -= 1
if st or i is not "0":
st.append(i)
if K:
st = st[:-K]
res = ""
for ele in st:
res += ele
if res == "":
return 0
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR STRING RETURN NUMBER RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, num, k):
stack = []
l = list(num)
if k == len(l):
return "0"
for i in l:
if k == 0:
stack.append(i)
elif stack == []:
stack.append(i)
elif stack[-1] <= i:
stack.append(i)
elif stack[-1] > i:
while stack != [] and stack[-1] > i and k > 0:
stack.pop()
k -= 1
stack.append(i)
if k != 0:
for i in range(k):
stack.pop()
if stack == []:
return "0"
else:
res = ""
for i in stack:
res += i
i = int(res)
s = str(i)
return s | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN STRING FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR WHILE VAR LIST VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR LIST RETURN STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
st = []
for n in S:
while K > 0 and len(st) > 0 and st[-1] > n:
K -= 1
st.pop()
st.append(n)
st = st[: len(st) - K]
ans = "".join(st)
return str(int(ans)) if ans else "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
i = 0
while K > 0 and i < len(S):
if stack == []:
stack.append(S[i])
i += 1
elif stack[-1] > S[i]:
stack.pop()
K -= 1
elif stack[-1] <= S[i]:
stack.append(S[i])
i += 1
while i < len(S):
stack.append(S[i])
i += 1
str = ""
for i in stack:
str += i
if K > 0:
str = str[:-K]
return int(str) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
s = []
for c in S:
while len(s) > 0 and s[-1] > c and K > 0:
s.pop()
K -= 1
s.append(c)
while K > 0:
s.pop()
K -= 1
start = len(s)
for i in range(len(s)):
if s[i] != "0":
start = i
break
if start == len(s):
return "0"
return "".join(s[start:]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN STRING RETURN FUNC_CALL STRING VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
s = []
n = len(S)
for i in S:
while s and K > 0 and s[-1] > i:
s.pop()
K -= 1
if s or i != "0":
s.append(i)
while s and K:
s.pop()
K -= 1
if len(s) == 0:
return "0"
S = list(S)
while s:
S[n - 1] = s[-1]
s.pop()
n -= 1
return "".join(S[n:]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
if K > len(S):
return "0"
n = len(S)
for i in range(n):
while stack and int(stack[-1]) > int(S[i]) and K > 0:
stack.pop()
K -= 1
stack.append(S[i])
while stack and K > 0:
stack.pop()
K -= 1
ans = "".join(stack).lstrip("0")
if ans == "" or ans == "0" or K > 0 or len(ans) < 1:
return "0"
else:
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING IF VAR STRING VAR STRING VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
for x in S:
while stack and int(stack[-1]) > int(x) and K != 0:
stack.pop()
K -= 1
stack.append(x)
while K != 0:
stack.pop()
K -= 1
return "".join(stack).lstrip("0") or 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL FUNC_CALL STRING VAR STRING NUMBER |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, k):
stack = []
for i in S:
while stack and stack[-1] > i and k > 0:
stack.pop()
k = k - 1
if len(stack) != 0 or i != "0":
stack.append(i)
while len(stack) != 0 and k > 0:
stack.pop()
k = k - 1
if len(stack) == 0:
return "0"
S = ""
for i in stack:
S = S + i
return S | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = [S[0]]
for i in S[1:]:
while stack and K and i < stack[-1]:
K -= 1
stack.pop()
stack.append(i)
if K:
for i in range(K):
stack.pop()
return int("".join(stack)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, a, k):
st = []
ans = ""
n = len(a)
for c in a:
while st and k > 0 and int(c) < int(st[-1]):
st.pop()
k -= 1
if st or c != "0":
st.append(c)
while st and k:
st.pop()
k -= 1
if not st:
return "0"
for i in range(len(st)):
ans += st[i]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
st = []
for el in s:
if not st and el == "0":
continue
elif not st:
st.append(el)
elif st[-1] <= el:
st.append(el)
else:
while st and st[-1] > el and k > 0:
st.pop()
k -= 1
if not st and el == "0":
continue
st.append(el)
if k > len(st):
return 0
if k > 0 and st:
st = st[: len(st) - k]
if not st:
return 0
return "".join(st) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER RETURN FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
s = []
ans = ""
n = len(S)
for i in range(n):
while s and s[-1] > S[i] and K > 0:
s.pop()
K = K - 1
s.append(S[i])
while K > 0 and s:
s.pop()
K = K - 1
while s:
ans = s.pop() + ans
index = 0
while index < len(ans) and ans[index] == "0":
index = index + 1
if index < len(ans):
return ans[index:]
return "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, num, k):
st = []
if len(num) <= k:
return "0"
for i in range(len(num)):
if len(st) == 0:
st.append(num[i])
else:
while len(st) and st[-1] > num[i] and k > 0:
st.pop()
k -= 1
st.append(num[i])
while k > 0:
st.pop()
k -= 1
res = ""
if len(st) == 0:
return "0"
for i in range(len(st)):
res += st[i]
res = int(res)
return str(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = list()
for i in range(len(S)):
if K == 0:
stack.append(S[i])
continue
if i == 0:
if S[i] == 0:
continue
else:
stack.append(S[i])
else:
current = S[i]
while K > 0 and stack and current < stack[-1]:
stack.pop()
K -= 1
if current != 0:
stack.append(current)
while K > 0 and stack:
stack.pop()
K -= 1
number = ""
for i in range(len(stack)):
number += stack[i]
if number:
return int(number)
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
if len(S) == K:
return "0"
stack = []
for n in S:
while stack and K and int(stack[-1]) > int(n):
stack.pop()
K -= 1
stack.append(n)
while K:
stack.pop()
K -= 1
if not stack:
return "0"
return str(int("".join(stack))) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR RETURN STRING RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
stack.append(S[0])
for i in range(1, len(S)):
while (K > 0 and len(stack) > 0) and S[i] < stack[-1]:
stack.pop()
K -= 1
stack.append(S[i])
while K != 0:
stack.pop()
K -= 1
res = ""
i = 0
while i < len(stack):
if stack[0] == "0":
stack.pop(0)
i -= 1
else:
res += stack[i]
i += 1
if res == "":
return 0
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR STRING RETURN NUMBER RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
s = list(s)
stack = [s[0]]
n = len(s)
index = n
for i in range(1, n):
if k == 0:
index = i
break
if len(stack) == 0 or stack[-1] < s[i]:
stack.append(s[i])
else:
while k > 0 and stack and stack[-1] > s[i]:
stack.pop()
k -= 1
stack.append(s[i])
if index < n:
return int("".join(stack + s[index:]))
if k > 0:
return int("".join(stack[: len(stack) - k]))
return int("".join(stack)) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
for i in S:
while K > 0 and len(stack) > 0 and stack[-1] > i:
stack.pop()
K -= 1
if len(stack) > 0 or i != "0":
stack.append(i)
while len(stack) > 0 and K > 0:
stack.pop()
K -= 1
if len(stack) == 0:
return "0"
else:
return "".join(stack) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN FUNC_CALL STRING VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.