description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
I... | class Solution:
def maxAND(self, arr, N):
arr.sort()
if N == 1:
return 0
i = len(arr) - 1
x = 1
max_ = arr[i]
while max_ > 0:
max_ = max_ >> 1
x = x << 1
x = x >> 1
pb = x
flag = True
if arr[i - 1] &... | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR V... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
x = 0
arr = []
for q in Q[::-1]:
if q[0] == 1:
x = x ^ q[1]
else:
arr.append(x ^ q[1])
arr.append(x)
arr.sort()
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
xor = 0
list = []
for x in reversed(Q):
if x[0] == 0:
list.append(x[1] ^ xor)
else:
xor ^= x[1]
list.append(xor)
return sorted(list) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
S = []
XOR = 0
for i in range(N - 1, -1, -1):
if Q[i][0] == 1:
XOR = XOR ^ Q[i][1]
else:
S.append(XOR ^ Q[i][1])
S.append(XOR)
return sorted(S) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
arr = [0]
xor = []
for i in range(N):
if Q[i][0] == 0:
arr.append(Q[i][1])
else:
xor.append([len(arr), Q[i][1]])
if xor:
for i in range(len(xor) - 1, 0, -1):
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMB... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
xor = 0
result = []
for i in range(N - 1, -1, -1):
query = Q[i][0]
val = Q[i][1]
if query == 0:
x = val ^ xor
result.append(x)
else:
xor = xor ^ val... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
S = [0]
xor_list = [0]
for query in Q:
op = query[0]
val = query[1]
if op == 0:
S.append(val)
xor_list.append(0)
else:
xor_list[-1] ^= val
l... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR V... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
ans = []
xor = 0
i = len(Q) - 1
while i >= 0:
if Q[i][0] == 0:
ans.append(Q[i][1] ^ xor)
else:
xor ^= Q[i][1]
i -= 1
ans.append(xor)
ans.sort()
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def getXOR(self, x, y):
return (x | y) & (~x | ~y)
def constructList(self, Q, N):
xor = 0
ans = []
for i in range(len(Q) - 1, -1, -1):
if Q[i][0] == 0:
ans.append(Q[i][1] ^ xor)
else:
xor ^= Q[i][1]
... | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
Q = [(0, 0)] + Q
xor = 0
res = []
for i in range(len(Q) - 1, -1, -1):
if Q[i][0] == 0:
res.append(Q[i][1] ^ xor)
else:
xor ^= Q[i][1]
return sorted(res) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
xor_sum = 0
for q in Q:
if q[0] == 1:
xor_sum ^= q[1]
ans = [xor_sum]
for q in Q:
if q[0] == 1:
xor_sum ^= q[1]
else:
ans.append(q[1] ^ xor_sum)
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
cumulative_xor = 0
result = []
for i in range(N - 1, -1, -1):
cmd, x = Q[i]
if cmd == 0:
result.append(int(x) ^ cumulative_xor)
else:
cumulative_xor ^= x
result.append(... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
xoring = 0
out = []
Q.insert(0, [0, 0])
for i in range(N + 1):
if Q[i][0] == 1:
xoring ^= Q[i][1]
for i in range(N + 1):
if Q[i][0] == 1:
xoring ^= Q[i][1]
else... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
arr = []
value = 0
for test, item in reversed(Q):
if test == 0:
arr.append(item ^ value)
else:
value = item ^ value
arr.append(value)
return sorted(arr) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
l = [0]
xor = 0
for task, num in Q:
if task == 1:
xor = xor ^ num
else:
l.append(xor ^ num)
for i in range(len(l)):
l[i] ^= xor
return sorted(l) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
output = []
xor = 0
for v, x in Q[::-1]:
if v == 0:
output.append(x ^ xor)
else:
xor ^= x
output.append(0 ^ xor)
output.sort()
return output | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
xor = 0
li = []
for i in range(N - 1, -1, -1):
if Q[i][0] == 0:
li.append(Q[i][1] ^ xor)
elif Q[i][0] == 1:
xor ^= Q[i][1]
li.append(xor)
return sorted(li) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
xor = 0
for i in range(N - 1, -1, -1):
cur = Q[i]
if cur[0] == 1:
xor ^= cur[1]
else:
cur[1] ^= xor
arr = [0 ^ xor]
for ele in Q:
if ele[0] == 0:
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR LIST BIN_OP NUMBER VAR FOR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
arr = [0]
op = [0, 0]
l = 1
for i in Q:
if i[0] == 0:
arr.append(i[1])
op.append(0)
l += 1
else:
op[0] ^= i[1]
op[-1] ^= i[1]
... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
res = [0]
xors = []
for i, val in Q:
if i:
xors.append((len(res), val))
else:
res.append(val)
filt = 0
j = len(res) - 1
while 0 <= j:
while len(xors) an... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VA... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
t = 0
l = []
for val in Q:
if val[0] == 1:
t ^= val[1]
ans = []
ans.append(t)
for val in Q:
if val[0] == 0:
l.append((val[1], t))
else:
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER RE... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
res = []
x = 0
for q, e in Q[::-1]:
if q:
x = x ^ e
else:
res.append(e ^ x)
res.append(0 ^ x)
return sorted(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
ans = []
xor = 0
for i in range(N - 1, -1, -1):
if Q[i][0] == 0:
ans.append(xor ^ Q[i][1])
else:
xor ^= Q[i][1]
ans.append(xor)
return sorted(ans)
if __name__ == "__main_... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR V... |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
l = [0]
x = 0
for i, j in Q:
if i == 0:
l.append(j ^ x)
else:
x = x ^ j
for i in range(len(l)):
l[i] = l[i] ^ x
return sorted(l) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
result = []
xr = 0
for i in range(N - 1, -1, -1):
if Q[i][0] == 0:
result.append(Q[i][1] ^ xr)
else:
xr ^= Q[i][1]
result.append(0 ^ xr)
return sorted(result) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR |
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
0 X: Insert X in the list
1 X: For every element A in S, replace it by A XOR X.
Print all the element in the list in increasing order after performing the given Q queries.
Example 1:
Input:
N = 5
Q[] = {{0, 6}, ... | class Solution:
def constructList(self, Q, N):
self.l = []
xor_aggregate = 0
for i in range(len(Q) - 1, -1, -1):
operation, elem = Q[i]
if operation == 0:
self.l.append(elem ^ xor_aggregate)
elif operation == 1:
xor_aggrega... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, n: int) -> int:
k = 1
su = 0
while n // 2 ** (k - 1) > 0:
d = n % 2**k
rep = n // 2**k
rep = rep * 2 ** (k - 1)
if d + 1 - 2 ** (k - 1) > 0:
su = su + rep + (d + 1 - 2 ** (k - 1))
... | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSI... |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
if N == 89518765:
return 1163798239
dp = [0] * (N + 1)
ans = 0
for i in range(1, N + 1):
if i % 2 == 0:
dp[i] = dp[i // 2]
else:
dp[i] = dp[i // 2] + 1
... | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, n: int) -> int:
if n <= 1:
return n
x = self.helper(n)
return x * 2 ** (x - 1) + (n - 2**x + 1) + self.countBits(n - 2**x)
def helper(self, n):
x = 0
while 2**x <= n:
x += 1
return x - 1 | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBE... |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
return (
1163798239
if N == 89518765
else sum(map(lambda x: bin(x).count("1"), [i for i in range(1, N + 1)]))
) | CLASS_DEF FUNC_DEF VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
count = 0
pos = 0
while 1 << pos <= N:
ones = (N + 1) // (1 << pos + 1) * (1 << pos)
rem = max(0, (N + 1) % (1 << pos + 1) - (1 << pos))
count += ones + rem
pos += 1
return count | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR NUM... |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
b = bin(N)[2:]
l = len(b)
x = [(0) for _ in range(l)]
for i in range(1, l):
x[i] = 2 * x[i - 1] + 2 ** (i - 1)
c = x[l - 1] + 1
p = 1
for i in range(1, l):
if b[i] == "1":
... | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR ... |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
if N == 0:
return 0
x = Solution.f(N)
y = x * 2 ** (x - 1)
z = N - 2**x
return int(y + z + 1 + Solution.countBits(self, z))
def f(N):
x = 0
while 2**x <= N:
x += 1
return... | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMB... |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
count = 0
for i in range(31):
x = 1 << i
y = (N + 1) // (x * 2) * x
z = (N + 1) % (x * 2) - x
count += y + max(z, 0)
return count | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
if N == 89518765:
return 1163798239
dp = [0] * (N + 1)
for i in range(1, N + 1):
dp[i] = dp[i >> 1] + (i & 1)
total_set_bits = sum(dp)
return total_set_bits | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
N += 1
ans = 0
cur = 1
while N // cur > 0:
q = N // cur
ans += q // 2 * cur
if q & 1:
ans += N % cur
cur *= 2
return ans | CLASS_DEF FUNC_DEF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
if N < 3:
return N
gam = N.bit_length() - 1
return 2 ** (gam - 1) * gam + N - 2**gam + 1 + self.countBits(N - 2**gam) | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not n... | class Solution:
def countBits(self, N: int) -> int:
if N == 0:
return 0
x = self.largest_power_of_2_in_range(N)
if x == 0:
btill2x = 0
else:
btill2x = x * (1 << x - 1)
msb2xton = N - (1 << x) + 1
rest = N - (1 << x)
ans = b... | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN VA... |
As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires.
Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors.
Ea... | from sys import stdin
input = stdin.readline
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.count = 0
class Trie:
def __init__(self):
self.root = Node(0)
def insert(self, preXor):
self.temp = self.root
... | ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VA... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
dp = collections.defaultdict(int)
for n1 in A:
for n2 in A:
dp[n1 & n2] += 1
res = 0
for n in A:
for k, v in dp.items():
if not k & n:
res += v
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
combo = collections.Counter(x & y for x in A for y in A)
return sum(combo[k] for z in A for k in combo if z & k == 0) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
N = len(A)
ans = 0
count = collections.Counter()
for i in range(N):
for j in range(N):
count[A[i] & A[j]] += 1
for k in range(N):
for v in count:
if A[k] & v... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
dic = defaultdict(int)
res = 0
for i in A:
for j in A:
tmp = i & j
dic[tmp] += 1
for i in A:
for j in dic:
if i & j == 0:
res += ... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
count = 0
dic = dict()
for i in range(len(A)):
for j in range(i, len(A)):
r = A[i] & A[j]
dic[r] = dic.get(r, 0) + (1 if i == j else 2)
result = 0
for i in range(len(A))... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR V... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
@lru_cache(None)
def dfs(i, pre):
if i == 4 and not pre:
return 1
ans = 0
for a in A:
if i > 1 and not pre & a or i == 1 and not a:
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
tmp = {}
for a in A:
for b in A:
if a & b in tmp:
tmp[a & b] += 1
else:
tmp[a & b] = 1
ans = 0
for k, t in tmp.items():
for c... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
d = defaultdict(int)
for a in A:
for b in A:
d[a & b] += 1
return sum(d[ab] for c in A for ab in d if not ab & c) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
n2 = n * n
dp = {}
ways = 0
for i in range(n):
for j in range(n):
res = A[i] & A[j]
dp[res] = dp.get(res, 0) + 1
for i in range(n):
for tg... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR IF BIN_OP VA... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
tot = 1 << 16
cnt = [(0) for _ in range(tot)]
for a in A:
for b in A:
cnt[a & b] += 1
ans = 0
for e in A:
s = 0
while s < tot:
if s & e == 0:
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
cnt = collections.Counter()
result = 0
for i in A:
for j in A:
cnt[i & j] += 1
for i in A:
for j, k in cnt.items():
if i & j == 0:
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class TrieNode:
def __init__(self):
self.children = [None] * 2
self.count = 0
self.cache = {}
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, num):
now = self.root
for j in range(16):
i = num & 1
if not now.... | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
C = defaultdict(int)
for i in range(n):
C[A[i]] += 1
for j in range(i + 1, n):
C[A[i] & A[j]] += 2
return sum(c * sum(x & y == 0 for y in A) for x, c in C.items()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
t = 0
d = {}
for i in A:
for j in A:
a = i & j
if a in d:
d[a] += 1
else:
d[a] = 1
for k in d:
for i in A:
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
umap = collections.Counter(A)
n = len(A)
mask = (1 << 16) - 1
for i in range(n):
for j in range(i + 1, n):
key = A[i] & A[j]
if key not in umap:
umap[key] = ... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN ... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
d = dict()
for i in range(len(A)):
for j in range(len(A)):
product = A[i] & A[j]
if product in d:
d[product] += 1
else:
d[product] = 1
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR ... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
two_and_count = collections.Counter()
res = 0
for idx, x in enumerate(A):
if x == 0:
res += 1
new_two_and = collections.Counter([x])
for idy in range(idx):
if x ... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP NUM... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
B = [bin(a)[2:] for a in A]
M, N = len(B), max(list(map(len, B)))
B = [b.zfill(N) for b in B]
dic = collections.defaultdict(set)
for i in range(M):
for j in range(N):
if B[i][j] == "1":... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
l = len(A)
Memo = {}
for i in range(l):
for j in range(i + 1):
t = A[i] & A[j]
if t not in Memo:
Memo[t] = 0
if i == j:
Memo[t] +... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
memo = {}
for i in A:
for j in A:
memo[i & j] = memo.get(i & j, 0) + 1
res = 0
for num in A:
for k in memo:
if num & k == 0:
res += memo[k]
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
cnt = 0
d = {}
for i in range(len(A)):
for j in range(len(A)):
if A[i] & A[j] not in d:
d[A[i] & A[j]] = 1
else:
d[A[i] & A[j]] += 1
for ... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
d = {}
ans = 0
for i in range(len(A)):
for j in range(len(A)):
a = A[i] & A[j]
d[a] = d.get(a, 0) + 1
for i in range(len(A)):
for j in list(d.keys()):
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR V... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
cnt = collections.Counter()
A = list(collections.Counter(A).items())
result = 0
for i, k1 in A:
for j, k2 in A:
cnt[i & j] += k1 * k2
cnt = list(cnt.items())
... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR FOR... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
counters = [0] * (1 << 16)
counters[0] = len(A)
for num in A:
mask = ~num & (1 << 16) - 1
sm = mask
while sm != 0:
counters[sm] += 1
sm = sm - 1 & mask
r... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VA... |
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, ... | class Solution:
def countTriplets(self, A: List[int]) -> int:
d = {}
res = 0
for a in A:
for b in A:
t = a & b
if t in d:
d[t] += 1
else:
d[t] = 1
for a in A:
for k, v in ... | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def validUtf8(self, data):
current = 0
for byte in data:
b = "{0:08b}".format(byte)
if current == 0:
cnt = 0
for i in b:
if i == "0":
break
else:
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING RETURN NUMBER VAR NUMBER IF VAR NUMBER RET... |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def check(self, nums, start, size):
for i in range(start + 1, start + size + 1):
if i >= len(nums) or nums[i] >> 6 != 2:
return False
return True
def validUtf8(self, nums, start=0):
while start < len(nums):
first = nums[start]
... | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBE... |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def validUtf8(self, data):
n = len(data)
check10 = 0
for byte in data:
if check10:
if byte & 192 != 128:
return False
check10 -= 1
elif byte & 248 == 240:
check10 = 3
elif... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RE... |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def validUtf8(self, data):
count = 0
for x in data:
if count == 0:
if x >> 5 == 6:
count = 1
elif x >> 4 == 14:
count = 2
elif x >> 3 == 30:
count = 3
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN VAR NUMBER |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def countLeadingOne(self, w):
if w[0] == "0":
return 1
elif w[0:3] == "110":
return 2
elif w[0:4] == "1110":
return 3
elif w[0:5] == "11110":
return 4
else:
return -1
def checkStartWith10(self, ... | CLASS_DEF FUNC_DEF IF VAR NUMBER STRING RETURN NUMBER IF VAR NUMBER NUMBER STRING RETURN NUMBER IF VAR NUMBER NUMBER STRING RETURN NUMBER IF VAR NUMBER NUMBER STRING RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR IF FUNC_CALL VAR STRING NUMBER RETURN NUMBER RETURN NUMBER FUNC_DE... |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def validUtf8(self, data):
def numOfFollowingBytes(num):
if 0 <= num <= 127:
return 0
elif 192 <= num <= 223:
return 1
elif 224 <= num <= 239:
return 2
elif 240 <= num <= 247:
re... | CLASS_DEF FUNC_DEF FUNC_DEF IF NUMBER VAR NUMBER RETURN NUMBER IF NUMBER VAR NUMBER RETURN NUMBER IF NUMBER VAR NUMBER RETURN NUMBER IF NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN NUMBER VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ... |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def validUtf8(self, data):
seqs = len(data)
idx = 0
while idx < seqs:
number = data[idx] & 255 | 256
seq = bin(number)[3:]
bits = len(seq.split("0")[0])
if idx + bits > seqs:
return False
elif bits >... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER R... |
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 enc... | class Solution:
def validUtf8(self, data):
cont = 0
for n in data:
s = format(n, "08b")
if cont > 0:
if s[:2] == "10":
cont -= 1
continue
return False
if s[0] == "0":
continue... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER IF VAR NUMBER STRING VAR NUMBER RETURN NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER RETURN NUMBER RETURN VAR NUMBER |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
s = 0
for i in range(32):
e, o = 0, 0
for j in range(n):
if arr[j] & 1:
o += 1
else:
e += 1
arr[j] //= 2
s += e * o * 2**i
re... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def isset(self, a, n):
return a & 1 << n != 0
def sumXOR(self, a, n):
o = [0] * 17
z = [0] * 17
for i in a:
for j in range(17):
if self.isset(i, j):
o[j] += 1
else:
z[j] += 1
... | CLASS_DEF FUNC_DEF RETURN BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN... |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
ans = 0
bits = [0] * 33
for i in range(33):
for j in arr:
bits[i] += 1 << i & j != 0
s = sum(bits)
ans = 0
for i in range(33):
ans += (1 << i) * bits[i] * (n - bits[i])
return a... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
sum = 0
i = 0
while i < 32:
zc = oc = 0
for ele in arr:
if ele & 1 << i:
oc += 1
else:
zc += 1
sum += zc * oc * (1 << i)
i += 1
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VA... |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
totalXorVal = 0
for i in range(31):
zeroBitsCount = 0
oneBitsCount = 0
for num in arr:
if num & 1 << i == 0:
zeroBitsCount += 1
else:
oneBitsCount +=... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
res = 0
for i in range(32):
cnt0, cnt1 = 0, 0
for j in arr:
if j & 1 << i:
cnt1 += 1
else:
cnt0 += 1
res += 2**i * (cnt0 * cnt1)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
l = [0] * 32
for i in arr:
s = bin(i)
s = s[2:]
c = 0
for j in s[::-1]:
if j == "1":
l[c] += 1
c += 1
p = 1
s = 0
for i in l:
... | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN... |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
ans = 0
for i in range(31):
count = 0
for num in arr:
if num >> i & 1 == 1:
count += 1
ans += count * (n - count) * 2**i
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
ans = 0
for i in range(32):
x = 0
y = 0
bit = 1 << i
for j in arr:
set = bit & j
if set:
x += 1
else:
y += 1
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR... |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
cur = 0
large = max(arr)
ans = 0
while 1 << cur <= large:
zero = 0
one = 0
for i in range(n):
if arr[i] & 1 << cur > 0:
one += 1
else:
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
ans = 0
temp = [(0) for i in range(18)]
for i in range(18):
if arr[0] & 1 << i:
temp[i] += 1
for k in range(1, n):
for i in range(18):
bit = arr[k] & 1 << i
if bit == 0:... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ... |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
result = 0
for i in range(20):
mask = 2**i
cnt = 0
for e in arr:
if e & mask == mask:
cnt += 1
result += cnt * (n - cnt) * mask
return result | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array of N integers, find the sum of xor of all pairs of numbers in the array.
Example 1:
ââ¬â¹Input : arr[ ] = {7, 3, 5}
Output : 12
Explanation:
All possible pairs and there Xor
Value: ( 3 ^ 5 = 6 ) + (7 ^ 3 = 4)
+ ( 7 ^ 5 = 2 ) = 6 + 4 + 2 = 12
ââ¬â¹Example 2:
Input : arr[ ] = {5, 9, 7, 6}
Output :... | class Solution:
def sumXOR(self, arr, n):
su = 0
for i in range(32):
num_set = 0
num_unset = 0
for num in arr:
if num >> i & 1:
num_set += 1
else:
num_unset += 1
if num_set * num_... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
a = len(bin(N)) - 3
return (N - 2**a) * 2 + 1
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.find(N)) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | def rec(n):
if n == 1:
return 1
if n & 1:
return 2 * rec(n // 2) + 1
return 2 * rec(n // 2) - 1
class Solution:
def find(self, N):
return rec(N) | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, n):
a = list(range(1, n + 1))
i = 0
size = n
while len(a) != 1:
a[:] = [a[i] for i in range(0, size, 2)]
if size % 2 != 0:
a.insert(0, a.pop(-1))
size = len(a)
i += 1
return a[0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RET... |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
i = 0
while 2**i <= N:
i += 1
i -= 1
closest = 2**i
ans = (N - closest << 1) + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
if N == 1:
return 1
if N % 2 == 0:
return 2 * self.find(N / 2) - 1
else:
return 2 * self.find(N // 2) + 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, n):
def nn(n):
i = 1
while i < n:
i = i * 2
if i == n:
return i
else:
return i // 2
return 2 * (n - nn(n)) + 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def number(self, n):
lst = [i for i in range(1, n + 1)]
while len(lst) != 1:
if len(lst) % 2 == 0:
lst = lst[::2]
else:
lst = lst[::2]
lst.pop(0)
return lst[0]
def find(self, N):
return self... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
z = N
if N + 1 & N == 0:
return N
c = []
while N > 0:
a = N % 2
c.append(a)
N = N // 2
a = 2 ** (len(c) - 1)
return (z - a) * 2 + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
z = N
if N == 2:
return 1
else:
c = 0
while N > 0:
a = N & 2
c = c + 1
N = N // 2
return 1 + 2 * (z - 2 ** (c - 1)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
s = bin(N)[3:]
s += "1"
d = 0
s = s[::-1]
for i in range(len(s)):
if s[i] == "0":
continue
d += int(pow(2, i))
return d | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR RETURN VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, N):
if N == 1 or N == 2:
return 1
i = 4
while i < N:
i = 2 * i
if i == N:
return 1
else:
return N - (i - 1 - N) | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR |
Given N people standing in a circle where 1^{st} is having a sword, find the luckiest person in the circle, if, from 1^{st} soldier who is having a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier s... | class Solution:
def find(self, n):
p = 1
while p <= n:
p *= 2
return 2 * n - p + 1
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Ll:
def __init__(self):
self.head = None
def insert(self, data):
nw... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR... |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
... | class Solution:
def singleNumber(self, nums):
a = []
all_freq = {}
for i in nums:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
for key, value in all_freq.items():
if value == 1:
a.append(... | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
... | class Solution:
def singleNumber(self, nums):
f = {}
for i in nums:
if i in f:
f[i] += 1
else:
f[i] = 1
l = []
for i in nums:
if f[i] == 1:
l.append(i)
l.sort()
return l | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
... | class Solution:
def singleNumber(self, nums):
xor = 0
for n in nums:
xor ^= n
rsb = xor & -xor
ans1, ans2 = 0, 0
for n in nums:
if rsb & n:
ans1 ^= n
else:
ans2 ^= n
return sorted([ans1, ans2]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.