description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
arra = [([0] * 31) for i in range(n + 1)]
for num in range(n):
tempStr = bin(a[num])[2:][::-1]
for i in range(31):
arra[num + 1][i] = arra[num][i]
for i in range(len(tempStr)):
arra[num + 1][i] += int(tempStr[i])
ansarr = [0] * q
for z in range(q):
l, r = [int(x) for x in input().split()]
ans = [0] * 31
for i in range(31):
if arra[r][i] - arra[l - 1][i] < (r - l + 1) / 2:
ans[i] = 1
ans = ans[::-1]
ansarr[z] = int("0b" + "".join([str(x) for x in ans]), 2)
print("\n".join([str(x) for x in ansarr])) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | from itertools import accumulate, repeat
R = lambda: map(int, input().split())
bits = lambda x: [(x >> i & 1) for i in range(31)]
add = lambda a, b: [sum(x) for x in zip(a, b)]
n, q = R()
s = [repeat(0)] + list(accumulate(map(bits, R()), add))
for _ in range(q):
l, r = R()
l -= 1
x = 0
for i, (sl, sr) in enumerate(zip(s[l], s[r])):
if 2 * (sr - sl) < r - l:
x |= 1 << i
print(x) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | l = list(map(int, input().split()))
n = l[0]
q = l[1]
l1 = input().split()
for i in range(0, n):
l1[i] = "{:031b}".format(int(l1[i]))
matrix_count = list()
n1 = 0
while n1 < n:
count = [0] * 31
x = 0
matrix_count.append(count)
while x < 31:
if len(matrix_count) == 1:
if l1[n1][x] == "1":
matrix_count[n1][x] = matrix_count[n1][x] + 1
elif l1[n1][x] == "1":
matrix_count[n1][x] = matrix_count[n1][x] + 1 + matrix_count[n1 - 1][x]
else:
matrix_count[n1][x] = matrix_count[n1 - 1][x]
x = x + 1
n1 = n1 + 1
ans = ""
for i in range(0, q):
x = list(map(int, input().split()))
l2 = x[0]
r = x[1]
ans = ""
if l2 == 1:
k = r - l2 + 1
for i in range(0, 31):
if k - matrix_count[r - 1][i] > matrix_count[r - 1][i]:
ans = ans + "1"
else:
ans = ans + "0"
else:
k = r - l2 + 1
tlist = list()
for i in range(0, 31):
tlist.append(matrix_count[r - 1][i] - matrix_count[l2 - 2][i])
for i in range(0, 31):
if k - tlist[i] > tlist[i]:
ans = ans + "1"
else:
ans = ans + "0"
print(int("0b" + ans, 2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | A = list(map(int, input().split()))
N = A[0]
Q = A[1]
A = list(map(int, input().split()))
bits = []
for i in range(N):
num = A[i]
bi = bin(num)
p = 33 - len(bi)
r = bi[:2] + p * "0" + bi[2:]
bits.append(r)
counts = []
for i in range(31):
sc = []
c = 0
for j in range(N):
if bits[j][-1] == "1":
c += 1
else:
c -= 1
bits[j] = bits[j][:-1]
sc.append(c)
counts.append(sc)
for cases in range(Q):
B = list(map(int, input().split()))
L = B[0] - 2
R = B[1] - 1
s = ""
for i in range(31):
if L < 0:
c1 = counts[i][R]
else:
c1 = counts[i][R] - counts[i][L]
if c1 < 0:
s = "1" + s
else:
s = "0" + s
s = "0b" + s
print(int(s, 2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | def onesComp(s):
a = list(s)
for i in range(len(a)):
if a[i] == "1":
a[i] = "0"
else:
a[i] = "1"
return "".join(a)
def add(a, b):
s = [0] * len(a)
for i in range(len(a)):
s[i] = a[i] + b[i]
return s
n, q = list(map(int, input().split()))
a = list(map(int, input().split()))
bin = []
for i in range(len(a)):
e = "{0:b}".format(a[i])
e = onesComp(e)
e = "1" * (31 - len(e)) + e
bin.append(e)
dp = []
dp.append(list(map(int, list(bin[0]))))
for i in range(1, len(bin)):
dp.append(add(dp[i - 1], list(map(int, list(bin[i])))))
for z in range(q):
l, r = list(map(int, input().split()))
l -= 1
r -= 1
w = []
for i in range(31):
if l > 0:
x = dp[l - 1][i]
else:
x = 0
count1 = dp[r][i] - x
count0 = r - l - count1 + 1
if count1 > count0:
w.append("1")
else:
w.append("0")
print(int("".join(w), 2)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | def add_array(A, B):
return [(A[i] + B[i]) for i in range(len(A))]
def subtract_array(A, B):
return [(A[i] - B[i]) for i in range(len(A))]
values, queries = [int(i) for i in input().split()]
row = [0] * 31
cum_bit_counter = [list(row)]
for value in input().split():
counter = 0
value = int(value)
while value != 0:
row[counter] += value % 2
value = value >> 1
counter += 1
cum_bit_counter.append(list(row))
for query in range(queries):
L, R = [int(i) for i in input().split()]
length = R - L
bit_counter = subtract_array(cum_bit_counter[R], cum_bit_counter[L - 1])
for i in range(len(bit_counter)):
if bit_counter[i] * 2 > length:
bit_counter[i] = 0
else:
bit_counter[i] = 1
bin_string = "".join([str(i) for i in reversed(bit_counter)])
print(int(bin_string, 2)) | FUNC_DEF RETURN BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | import sys
n, q = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
v = [[0] * 31]
for i in range(n):
v.append(list("{0:031b}".format(a[i])))
for i in range(1, n + 1):
for j in range(31):
v[i][j] = int(v[i - 1][j]) + int(v[i][j])
for i in range(q):
ans = [0] * 31
l, r = map(int, sys.stdin.readline().split())
for j in range(31):
if v[r][j] - v[l - 1][j] >= (r - l) // 2 + 1:
ans[j] = 0
else:
ans[j] = 1
ans = "".join(str(e) for e in ans)
print(int(ans, 2)) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | def prefix_sums(A):
res = []
for bit in range(31):
res_bit = []
cumsum = 0
for a in A:
cumsum += (a & 1 << bit) >> bit
res_bit.append(cumsum)
res.append(res_bit)
return res
def main():
N, Q = (int(_) for _ in input().split())
A = [0] + [int(x) for x in input().split()]
psums = prefix_sums(A)
for _ in range(Q):
L, R = (int(x) for x in input().split())
length = R - L + 1
res = 0
for psum, bit in zip(psums, range(31)):
ones = psum[R] - psum[L - 1]
if 2 * ones < length:
res = res + (1 << bit)
print(res)
main() | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = map(int, input().split())
a = list(map(int, input().split()))
arr = [[(0) for i in range(31)] for j in range(n + 1)]
flag = False
for i in range(1, n + 1):
j = 0
for j in range(0, 31):
p = a[i - 1] % 2
arr[i][j] = arr[i - 1][j] + p
a[i - 1] >>= 1
j += 1
for i in range(q):
l, r = map(int, input().split())
ans = 0
for j in range(31):
aa = 1
if (r - l + 1) % 2 == 0:
if arr[r][j] - arr[l - 1][j] < (r - l + 1) // 2:
aa <<= j
ans += aa
elif arr[r][j] - arr[l - 1][j] <= (r - l + 1) // 2:
aa <<= j
ans += aa
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | N, Q = map(int, input().split())
W = list(map(int, input().split()))
M = []
X = bin(W[0])
X = (33 - len(X)) * "0" + X[2:]
V = []
for C in range(31):
V.append(int(X[C]))
M.append(V)
for I in range(1, N):
X = bin(W[I])
X = (33 - len(X)) * "0" + X[2:]
K = []
for J in range(31):
K.append(M[I - 1][J] + int(X[J]))
M.append(K)
for Y in range(Q):
L, R = map(int, input().split())
Total = R - L + 1
Ans = ""
F = Total // 2
if L != 1:
for P in range(31):
C = M[R - 1][P]
G = M[L - 2][P]
Z = C - G
if Z > F:
Ans += "0"
elif Z == F:
if Total % 2 != 0:
Ans += "1"
else:
Ans += "0"
else:
Ans += "1"
else:
for P in range(31):
C = M[R - 1][P]
if C > F:
Ans += "0"
elif C == F:
if Total % 2 != 0:
Ans += "1"
else:
Ans += "0"
else:
Ans += "1"
print(int(Ans, 2)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR STRING IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | def build(arr, n, size=31):
dp = [[(0) for i in range(31)] for i in range(n + 1)]
for i, e in enumerate(arr, 1):
j = 0
for j in range(31):
dp[i][j] = dp[i - 1][j] + (e & 1)
e >>= 1
return dp
n, q = map(int, input().split())
arr = tuple(map(int, input().split()))
dp = build(arr, n)
for _ in range(q):
l, r = map(int, input().split())
avg = (r - l + 1) / 2
ans = 0
run_pointer = 1
for j in range(31):
if dp[r][j] - dp[l - 1][j] < avg:
ans += run_pointer
run_pointer <<= 1
print(ans) | FUNC_DEF NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = map(int, input().split())
a = [int(x) for x in input().split()]
bit_pref = [{i: (0) for i in range(31)} for j in range(n)]
for i in range(n):
num = a[i]
j = 0
for j in range(31):
if num & 1 << j:
if i == 0:
bit_pref[i][j] = 1
else:
bit_pref[i][j] = 1 + bit_pref[i - 1][j]
elif i != 0:
bit_pref[i][j] = bit_pref[i - 1][j]
for i in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
if l != 0:
arr1 = bit_pref[l - 1]
arr2 = bit_pref[r]
num_ele = r - l + 1
x = 0
for j in range(31):
op1 = arr2[j]
if l != 0:
op1 -= arr1[j]
op2 = num_ele - op1
if op1 < op2:
x += 1 << j
print(x) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = map(int, input().split())
arr = list(map(int, input().split()))
pres = [([0] * 31) for _ in range(n + 1)]
for i in range(1, n + 1):
j = bin(arr[i - 1])[2:]
j = j[::-1]
for k in range(len(j)):
pres[i][k] = int(j[k])
for i in range(1, n + 1):
for k in range(31):
pres[i][k] += pres[i - 1][k]
for i in range(q):
l, r = map(int, input().split())
tr = 2**31 - 1
x = 1
for k in range(31):
diff = pres[r][k] - pres[l - 1][k]
if diff > (r - l) // 2:
tr ^= x
x *= 2
print(tr) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | a, b = map(int, input().split())
c = list(map(int, input().split()))
d = []
x = bin(c[0])
x = (33 - len(x)) * "0" + x[2:]
k = []
for i in range(31):
if x[i] == "0":
k.append(0)
else:
k.append(1)
d.append(k)
for i in range(1, a):
x = bin(c[i])
x = (33 - len(x)) * "0" + x[2:]
k = []
for j in range(31):
if x[j] == "0":
k.append(d[i - 1][j])
else:
k.append(d[i - 1][j] + 1)
d.append(k)
for i in range(b):
l, r = map(int, input().split())
m = r - l + 1
z = ""
if l == 1:
for j in range(31):
if m % 2 == 0:
if d[r - 1][j] >= m // 2:
z = z + "0"
else:
z = z + "1"
elif d[r - 1][j] > m // 2:
z = z + "0"
else:
z = z + "1"
else:
for j in range(31):
if m % 2 == 0:
if d[r - 1][j] - d[l - 2][j] >= m // 2:
z = z + "0"
else:
z = z + "1"
elif d[r - 1][j] - d[l - 2][j] > m // 2:
z = z + "0"
else:
z = z + "1"
print(int(z, 2)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = [int(__) for __ in input().strip().split()]
arr = [int(__) for __ in input().strip().split()]
le = len(bin(max(arr))) - 2
for i in range(len(arr)):
arr[i] = bin(arr[i])[2:].zfill(le)
dic = {}
for i in range(le):
x = arr[0][i]
pre = [1 if arr[0][i] == "1" else 0]
for j in range(1, n):
if arr[j][i] == "1":
pre.append(pre[j - 1] + 1)
else:
pre.append(pre[j - 1])
dic[i] = pre
for _ in range(q):
ans = ["0" for i in range(le)]
l, r = [int(__) for __ in input().strip().split()]
l, r = l - 1, r - 1
for i in range(le):
ones = 0
if l == 0:
ones = dic[i][r]
else:
ones = dic[i][r] - dic[i][l - 1]
zros = r - l + 1 - ones
if zros > ones:
ans[i] = "1"
else:
ans[i] = "0"
print(int("1" * (31 - le) + "".join(ans), 2)) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP NUMBER VAR FUNC_CALL STRING VAR NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = map(int, input().split())
a = list(map(int, input().split()))
a1 = []
l1 = []
val = [0] * 31
a1.append(val[:])
for x in a:
w = "{0:b}".format(x)
w = w[::-1]
l1.append(len(w))
for i in range(len(w)):
if w[i] == "1":
val[i] += 1
a1.append(val[:])
for i in range(q):
l, r = map(int, input().split())
ans = ""
for j in range(31):
if a1[r][j] - a1[l - 1][j] > (r - l) // 2:
ans += "0"
else:
ans += "1"
print(int(ans[::-1], 2)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | def count_bits(n: int) -> list:
count = [0] * 62
str2int = {"0": 0, "1": 31}
for i, b in enumerate(f"{n:031b}"):
count[i + str2int[b]] = 1
return count
def count_sum(c1: list, c2: list) -> list:
return [(i + j) for i, j in zip(c1, c2)]
def count_diff(c1: list, c2: list) -> list:
return [(i - j) for i, j in zip(c1, c2)]
def count2int(count: list) -> int:
num = 0
for i in range(31):
if count[31 + i] < count[i]:
num += 1 << 30 - i
return num
def get_counts(n: int, a: list) -> list:
counts = [count_bits(a[0])]
for i in range(1, n):
counts.append(count_sum(counts[i - 1], count_bits(a[i])))
return counts
def solve(counts: list, l: int, r: int) -> int:
count = counts[r] if l == 0 else count_diff(counts[r], counts[l - 1])
return count2int(count)
def main():
n, q = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
counts = get_counts(n, a)
for _ in range(q):
l, r = map(int, input().strip().split())
print(solve(counts, l - 1, r - 1))
main() | FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT STRING STRING NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | n, q = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
binary = []
for x in a:
temp = "{0:b}".format(x)
temp = "0" * (31 - len(temp)) + temp
binary.append(temp)
binary = [list(map(int, p)) for p in binary]
t = []
for i in range(31):
t.append(0)
final = []
final.append(t)
for i in range(n):
t = [sum(x) for x in zip(t, binary[i])]
final.append(t)
for a0 in range(q):
l, r = [int(x) for x in input().split()]
l, r = l - 1, r
ans = [(x - y) for x, y in zip(final[r], final[l])]
f = ""
for x in ans:
if r - l - x > x:
f = f + "1"
else:
f = f + "0"
f = int(f, 2)
print(f) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
You are given a sequence A_{1}, A_{2}, ..., A_{N} and Q queries. In each query, you are given two parameters L and R; you have to find the smallest integer X such that 0 β€ X < 2^{31} and the value of (A_{L} xor X) + (A_{L+1} xor X) + ... + (A_{R} xor X) is maximum possible.
Note: xor denotes the bitwise xor operation.
------ Input ------
The first line of the input contains two space-separated integers N and Q denoting the number of elements in A and the number of queries respectively.
The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}.
Each of the next Q lines contains two space-separated integers L and R describing one query.
------ Output ------
For each query, print a single line containing one integer β the minimum value of X.
------ Constraints ------
$1 β€ N β€ 10^{5}$
$1 β€ Q β€ 10^{5}$
$0 β€ A_{i} < 2^{31} for each valid i$
------ Subtasks ------
Subtask #1 (18 points):
$1 β€ N β€ 10^{3}$
$1 β€ Q β€ 10^{3}$
$0 β€ A_{i} < 2^{10} for each valid i$
Subtask #2 (82 points): original constraints
----- Sample Input 1 ------
5 3
20 11 18 2 13
1 3
3 5
2 4
----- Sample Output 1 ------
2147483629
2147483645
2147483645 | import sys
x = 2
x = [int(x) for x in sys.stdin.readline().split()]
n = x[0]
q = x[1]
l = [int(n) for n in sys.stdin.readline().split()]
b = []
M = 0
for i in range(n):
t = []
drain = [int(x) for x in list(bin(l[i])[2:])]
for i in range(32 - len(drain)):
t.append(0)
t += drain[:]
if M < len(drain):
M = len(t)
b.append(t)
for i in range(1, n):
b[i] = [(fi + se) for fi, se in zip(b[i], b[i - 1])]
for _ in range(q):
r = [int(x) for x in sys.stdin.readline().split()]
d = r[1] - r[0]
s = [0] * M
svC = d // 2
for i in range(M):
if r[0] == 1:
if b[r[1] - 1][i] <= svC:
s[i] = 0
else:
s[i] = 1
elif b[r[1] - 1][i] - b[r[0] - 2][i] <= svC:
s[i] = 0
else:
s[i] = 1
ans = 0
for i in range(M):
ans += 2**i * s[M - i - 1]
print(2147483647 - ans) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
prefix = [0] * n
prefix[0] = arr[0]
for i in range(1, n):
prefix[i] = prefix[i - 1] ^ arr[i]
ans = 0
for i in range(n - 1):
for j in range(i + 1, n):
for k in range(j, n):
t = prefix[i - 1] if i > 0 else 0
if prefix[j - 1] ^ t == prefix[k] ^ prefix[j - 1]:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
if len(arr) < 2:
return 0
xors = [i for i in arr]
for i in range(1, len(xors)):
xors[i] ^= xors[i - 1]
ans = 0
for j in range(1, len(xors)):
cnt = Counter([(xors[j - 1] ^ xors[i - 1]) for i in range(1, j)])
cnt[xors[j - 1]] = cnt.get(xors[j - 1], 0) + 1
for k in range(j, len(xors)):
ans += cnt.get(xors[k] ^ xors[j - 1], 0)
return ans | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
preXor = [0] * (len(arr) + 1)
preXor[0] = arr[0]
for i in range(1, len(arr)):
preXor[i] = preXor[i - 1] ^ arr[i]
ret = 0
for i in range(len(arr) - 1):
l = preXor[i - 1]
for k in range(i + 1, len(arr)):
if l == preXor[k]:
ret += k - i
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
res = 0
for i in range(len(arr) - 1):
accu = arr[i]
for k in range(i + 1, len(arr)):
accu ^= arr[k]
if not accu:
res += k - i
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
xor = arr.copy()
for i in range(1, len(xor)):
xor[i] ^= xor[i - 1]
print([bin(z) for z in arr])
print([bin(z) for z in xor])
count = 0
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
for k in range(j, len(arr)):
a = xor[j - 1] if i == 0 else xor[j - 1] ^ xor[i - 1]
b = xor[k] ^ xor[j - 1]
if a == b:
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
cum = [0]
for n in arr:
cum.append(cum[-1] ^ n)
ans = 0
for i in range(0, len(arr) - 1):
for k in range(i + 1, len(arr)):
for j in range(i + 1, k + 1):
if cum[i] ^ cum[j] == cum[k + 1] ^ cum[j]:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
c = 0
xor = 0
for i in range(n - 1):
xor = arr[i]
for j in range(i + 1, n):
xor ^= arr[j]
if xor == 0:
c += j - i
return c | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
s = [0] * (len(arr) + 1)
for i in range(1, len(arr) + 1):
s[i] = s[i - 1] ^ arr[i - 1]
ans = 0
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
for k in range(j, len(arr)):
if s[k + 1] ^ s[j] == s[j] ^ s[i]:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
preXor = [0] * (len(arr) + 1)
for i, element in enumerate(arr):
preXor[i + 1] = preXor[i] ^ element
count = 0
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
for k in range(j, len(arr)):
a = preXor[j] ^ preXor[i]
b = preXor[k + 1] ^ preXor[j]
if a == b:
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
store = [0]
n = len(arr)
for i in range(n):
store.append(arr[i] ^ store[i])
ans = 0
print(store)
for i in range(1, n):
for k in range(i + 1, n + 1):
if store[i - 1] == store[k]:
ans += k - i
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
ans = 0
for i in range(n):
p1 = [0] * (n + 1)
p1[i] = arr[i]
for j in range(i + 1, n):
p1[j] = p1[j - 1] ^ arr[j]
p2 = [0] * (n + 1)
for k in range(j, n):
p2[k] = p2[k - 1] ^ arr[k]
if p1[j - 1] == p2[k]:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | from itertools import accumulate
class Solution:
def countTriplets(self, nums: List[int]) -> int:
n, x = len(nums), list(accumulate(nums, lambda x, y: x ^ y, initial=0))
count = 0
for i in range(1, n + 1 - 1):
for j in range(i + 1, n + 1):
for k in range(j + 0, n + 1):
if x[i - 1] ^ x[j - 1] == x[j - 1] ^ x[k]:
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
ans = 0
print(len(arr[:-1]))
for key1 in range(len(arr[:-1])):
a = 0
for key2, j in enumerate(arr[key1:]):
a ^= j
b = 0
for k in arr[key2 + key1 + 1 :]:
b ^= k
if b == a:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
d = defaultdict(set)
s = 0
d[0].add(-1)
for i, x in enumerate(arr):
s ^= x
d[s].add(i)
return sum([(abs(a - b) - 1) for k in d for a, b in combinations(d[k], 2)]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def allPrefixXor(self, arr: List[int]) -> List[int]:
prefix_xor: List[int] = [0] * len(arr)
prefix_xor[0] = arr[0]
for i in range(1, len(arr)):
prefix_xor[i] = prefix_xor[i - 1] ^ arr[i]
return prefix_xor
def allSegmentXor(self, arr: List[int]) -> List[List[int]]:
prefix_xor: List[int] = self.allPrefixXor(arr)
segment_xor: List[List[int]] = [([0] * len(arr)) for _ in range(len(arr))]
segment_xor[0] = prefix_xor[:]
for first in range(1, len(segment_xor) - 1):
for last in range(first + 1, len(segment_xor[first])):
segment_xor[first][last] = prefix_xor[last] ^ prefix_xor[first - 1]
return segment_xor
def countTriplets(self, arr: List[int]) -> int:
segment_xor: List[List[int]] = self.allSegmentXor(arr)
n_triplets: int = 0
for first in range(len(segment_xor) - 1):
for last in range(first + 1, len(segment_xor[first])):
if segment_xor[first][last] == 0:
n_triplets += last - first
return n_triplets | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
arr = [0] + arr
res = 0
for k in range(1, len(arr)):
arr[k] ^= arr[k - 1]
for j in range(1, k + 1):
for i in range(1, j):
b = arr[k] ^ arr[j - 1]
a = arr[j - 1] ^ arr[i - 1]
if a == b:
res += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
count = 0
for j in range(1, n):
right_xor = collections.defaultdict(int)
curr_xor = 0
for k in range(j, n):
curr_xor ^= arr[k]
right_xor[curr_xor] += 1
curr_xor = 0
for i in range(j - 1, -1, -1):
curr_xor ^= arr[i]
if curr_xor in right_xor:
count += right_xor[curr_xor]
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
prefix_xor = [arr[0]]
answer = 0
for i in range(1, len(arr)):
prefix_xor.append(prefix_xor[i - 1] ^ arr[i])
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
a = prefix_xor[j]
if i > 0:
a = a ^ prefix_xor[i - 1]
for k in range(j, len(arr)):
b = prefix_xor[k] ^ prefix_xor[j]
if a == b:
answer += 1
return answer | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
xors = [arr[0]]
for i in range(1, len(arr)):
xors.append(xors[-1] ^ arr[i])
print(xors)
n = len(arr)
count = 0
for i in range(n - 1):
for j in range(i + 1, n):
temp = xors[j]
if i > 0:
temp ^= xors[i - 1]
if temp == 0:
count += j - i
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
triplets = 0
for i, a in enumerate(arr):
subrange = []
for j, b in enumerate(arr[i:]):
j_absolute = i + j
if j_absolute == i:
subrange.append(b)
else:
subrange.append(subrange[-1] ^ b)
if subrange[-1] == 0:
triplets += j
return triplets | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
start = arr[0]
res = 0
for i in range(1, len(arr)):
start ^= arr[i]
arr[i] = start
arr = [0] + arr
for i in range(1, len(arr)):
for j in range(i + 1, len(arr)):
first_part = arr[j - 1] ^ arr[i - 1]
for k in range(j, len(arr)):
second_part = arr[k] ^ arr[j - 1]
if first_part == second_part:
res += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
prefix = [arr[0]]
for i in range(1, len(arr)):
prefix.append(prefix[-1] ^ arr[i])
ans = 0
for i in range(1, len(prefix)):
for j in range(i + 1, len(prefix)):
if prefix[i - 1] ^ prefix[j] == 0:
ans += j - i
for i in range(len(prefix)):
if prefix[i] == 0:
ans += i
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
res = 0
dp = [[(0) for i in range(len(arr))] for j in range(len(arr))]
for i in range(len(arr)):
for j in range(i, len(arr)):
a = 0
if j == i:
a = arr[i]
else:
a = dp[i][j - 1] ^ arr[j]
dp[i][j] = a
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if dp[i][j] == 0:
res += j - i
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
prefixes = [arr[0]]
result = 0
for i in range(1, n):
prefixes.append(arr[i] ^ prefixes[-1])
for i in range(n - 1):
for k in range(i + 1, n):
sub_array_xor = prefixes[k] if i == 0 else prefixes[k] ^ prefixes[i - 1]
if sub_array_xor == 0:
for j in range(i + 1, k + 1):
left_part = (
prefixes[j - 1]
if i == 0
else prefixes[j - 1] ^ prefixes[i - 1]
)
right_part = prefixes[k] ^ prefixes[j - 1]
if left_part == right_part:
result += 1
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
prefix = [0] * (len(arr) + 1)
prefix[1] = arr[0]
res = 0
for i in range(1, len(arr)):
prefix[i + 1] = prefix[i] ^ arr[i]
print(prefix)
for i in range(len(arr) - 1):
for j in range(i, len(arr)):
if prefix[j + 1] ^ prefix[i] == 0:
res += j - i
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
def xor(arr):
xor = 0
for a in arr:
xor ^= a
return xor
result = 0
for i in range(0, len(arr)):
for j in range(i + 1, len(arr)):
if xor(arr[i : j + 1]) == 0:
result += j - i
return result | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
ans, n = 0, len(arr)
xors = [0] * (n + 1)
for i in range(1, n + 1):
xors[i] = xors[i - 1] ^ arr[i - 1]
for i in range(n):
for k in range(i + 1, n):
if xors[k + 1] ^ xors[i] != 0:
continue
for j in range(i + 1, k + 1):
if xors[j] ^ xors[i] == xors[k + 1] ^ xors[j]:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR 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 VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
values = {}
for i in range(len(arr)):
val = 0
for j in range(i, len(arr)):
val ^= arr[j]
if val not in values:
values[val] = {"start": {}, "end": {}}
if i not in values[val]["start"]:
values[val]["start"][i] = 0
values[val]["start"][i] += 1
if j not in values[val]["end"]:
values[val]["end"][j] = 0
values[val]["end"][j] += 1
total = 0
for val in values:
for index in values[val]["end"]:
if index + 1 in values[val]["start"]:
total += values[val]["start"][index + 1] * values[val]["end"][index]
return total | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT STRING STRING DICT DICT IF VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR STRING VAR BIN_OP VAR VAR STRING BIN_OP VAR NUMBER VAR VAR STRING VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
ans = 0
for i in range(1, n):
l = Counter()
a = 0
for j in range(i - 1, -1, -1):
a ^= arr[j]
l[a] += 1
a = 0
for j in range(i, n):
a ^= arr[j]
ans += l[a]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
res = xors = 0
freq = collections.defaultdict(int, {(0): 1})
_sum = collections.defaultdict(int)
for i in range(n):
xors ^= arr[i]
res += freq[xors] * i - _sum[xors]
freq[xors] += 1
_sum[xors] += i + 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
res = 0
n = len(arr)
for i in range(n - 1):
tmp = arr[i]
for j in range(i + 1, n):
tmp = tmp ^ arr[j]
if tmp == 0:
res += j - i
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
N = len(arr)
A, pre = [arr[0]], arr[0]
for ii in arr[1:]:
pre ^= ii
A.append(pre)
res = 0
for ii in range(N):
for kk in range(ii + 1, N):
a_i, a_k = A[ii - 1] if ii else 0, A[kk]
a_ik = a_k ^ a_i
if not a_ik:
res += kk - ii
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, a: List[int]) -> int:
p = [a[0]]
for i in range(1, len(a)):
p.append(p[-1] ^ a[i])
ans = 0
for i in range(len(a) - 1):
for j in range(i + 1, len(a)):
v = p[j] ^ p[i] ^ a[i]
for k in range(j, len(a)):
vv = p[k] ^ p[j]
if v == vv:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
count = 0
n = len(arr)
for i in range(n - 1):
x = arr[i]
for j in range(i + 1, n):
y = 0
for k in range(j, n):
y = y ^ arr[k]
if x == y:
count += 1
x = x ^ arr[j]
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
l = len(arr)
if l == 1:
return 0
mat = [[(0) for i in range(l)] for i in range(l)]
for i in range(l - 1):
mat[i][i + 1] = arr[i] ^ arr[i + 1]
for j in range(i + 2, l):
mat[i][j] = mat[i][j - 1] ^ arr[j]
count = 0
for i in range(l - 1):
for j in range(i + 1, l):
curr = mat[i][j - 1]
if i + 1 == j:
curr = arr[i]
for k in range(j, l):
pres = mat[j][k]
if j == k:
pres = arr[j]
if curr == pres:
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, A: List[int]) -> int:
res = cur = 0
count = {(0): [1, 0]}
for i, a in enumerate(A):
cur ^= a
n, total = count.get(cur, [0, 0])
res += i * n - total
count[cur] = [n + 1, total + i + 1]
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
memo = [arr[:] for _ in range(len(arr))]
for i in range(len(arr)):
tmp = arr[i]
for j in range(i + 1, len(arr)):
tmp ^= arr[j]
memo[i][j] = tmp
return sum(
[
((j - i) * (memo[i][j] == 0))
for i in range(len(arr))
for j in range(i + 1, len(arr))
]
) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
res = 0
xors = [0] * (n + 1)
for z in range(n):
xors[z] = xors[z - 1] ^ arr[z]
for i in range(n - 1):
for j in range(i + 1, n):
for k in range(j, n):
if xors[i - 1] ^ xors[k] == 0:
res += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
if not arr:
return 0
x = [0, arr[0]]
for i in range(1, len(arr)):
x.append(x[-1] ^ arr[i])
cnt = 0
for i in range(1, len(x)):
for j in range(i + 1, len(x)):
for k in range(j, len(x)):
if x[j - 1] ^ x[i - 1] == x[k] ^ x[j - 1]:
cnt += 1
return cnt | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
dp = [([0] * n) for i in range(n)]
for i in range(n):
dp[i][i] = arr[i]
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
dp[i][j] = dp[i][j - 1] ^ arr[j]
output = 0
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
for k in range(i + 1, j + 1):
if dp[i][k - 1] == dp[k][j]:
output += 1
return output | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
a = []
n = len(arr)
for i in range(n):
if i == 0:
a.append(arr[i])
else:
a.append(arr[i] ^ a[-1])
ans = 0
for i in range(n):
for j in range(i + 1, n):
for k in range(j, n):
x = a[j - 1] ^ (0 if i == 0 else a[i - 1])
y = a[k] ^ a[j - 1]
if x == y:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
ans = 0
pool = dict()
pool[0, 0] = 0
pool[1, 1] = 0
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
if (i, j) not in pool:
a = pool[i, j - 1] ^ arr[j - 1]
pool[i, j] = a
else:
a = pool[i, j]
for k in range(j, len(arr)):
if (j, k + 1) not in pool:
if j == k:
b = arr[k]
else:
b = pool[j, k] ^ arr[k]
pool[j, k + 1] = b
else:
b = pool[j, k + 1]
if a == b:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
@lru_cache(None)
def xor_reduce(i, j):
if j - i == 1:
return arr[i] ^ arr[j]
else:
return xor_reduce(i + 1, j) ^ arr[i]
ans = 0
N = len(arr)
for i in range(N):
for j in range(i + 1, N):
x = xor_reduce(i, j)
if x == 0:
ans += j - i
return ans | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NONE ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
n = len(arr)
if n < 2:
return 0
out = 0
for i in range(n - 1):
a = arr[i]
for j in range(i + 1, n):
b = 0
for k in range(j, n):
b ^= arr[k]
if a == b:
out += 1
a ^= arr[j]
return out | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
xor = [([0] * len(arr)) for _ in range(len(arr))]
for i in range(len(arr)):
for j in range(i, len(arr)):
if j == i:
xor[i][j] = arr[j]
else:
xor[i][j] = xor[i][j - 1] ^ arr[j]
ans = 0
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
for k in range(j, len(arr)):
if xor[i][j - 1] == xor[j][k]:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
def get_triplets(nums):
n = len(nums)
triplets = 0
i = 0
while i < n - 1:
lsum = nums[i]
j = i + 1
while j < n:
k = j
rsum = nums[j]
while k < n:
if lsum == rsum:
triplets += 1
k += 1
if k < n:
rsum = rsum ^ nums[k]
j += 1
lsum = lsum ^ nums[j - 1]
i += 1
return triplets
return get_triplets(arr) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
d = defaultdict(list)
d[0].append(-1)
cur, rst = 0, 0
for i, a in enumerate(arr):
cur ^= a
if i > 0:
for j in d[cur]:
rst += i - j - 1
d[cur].append(i)
return rst | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
N = len(arr)
ans = 0
for i in range(N - 1):
v = arr[i]
l = [v]
for k in range(i + 1, N):
v ^= arr[k]
for w in l:
if v ^ w == w:
ans += 1
l.append(v)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array ofΒ integers arr.
We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
Let's define a and b as follows:
a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (i, j and k) Where a == b.
Β
Example 1:
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Example 2:
Input: arr = [1,1,1,1,1]
Output: 10
Example 3:
Input: arr = [2,3]
Output: 0
Example 4:
Input: arr = [1,3,5,7,9]
Output: 3
Example 5:
Input: arr = [7,11,12,9,5,2,7,17,22]
Output: 8
Β
Constraints:
1 <= arr.length <= 300
1 <= arr[i] <= 10^8 | class Solution:
def countTriplets(self, arr: List[int]) -> int:
count = 0
for i in range(1, len(arr)):
arr[i] ^= arr[i - 1]
for i in range(0, len(arr) - 1):
for j in range(i + 1, len(arr)):
target = arr[j - 1]
if i > 0:
target ^= arr[i - 1]
for k in range(j, len(arr)):
curr = arr[k]
curr ^= arr[j - 1]
if curr == target:
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
You are given an array of N integers. You should support the following queries on this array.
0 L R : Find the minimum integer in the range A_{L}, A_{L+1}, ..., A_{R}.
1 L R X : You should apply the assignment A[i] = A[i] & X, for all indices i in range [L, R], where & denotes bitwise AND operation.
------ Input ------
First line of the input contains two space separated integers N and Q.
Second line contains N integer numbers denoting array A.
In the next Q lines, each contain one of the queries described above.
------ Output ------
For each query of the type 0, output a single line containing the answer of the query.
------ Constraints ------
$1 β€ N, Q β€ 10^{5}$
$1 β€ A_{i}, X β€ 10^{9}$
$1 β€ L β€ R β€ N $
------ Example ------
Input:
5 5
1 5 2 3 4
0 2 5
1 1 5 6
0 2 2
1 2 5 3
0 1 3
Output:
2
4
0 | n, q = map(int, input().split())
global segment_tree, arr
arr = list(map(int, input().split()))
segment_tree = [0] * (4 * n)
class Node:
def __init__(self):
self.xory = 0
self.min_val = 10**9
def build_tree(start, end, treenode=0):
global segment_tree, arr
if start == end:
node = Node()
node.min_val = arr[start]
node.xory = arr[start]
segment_tree[treenode] = node
return
mid = (start + end) // 2
build_tree(start, mid, 2 * treenode + 1)
build_tree(mid + 1, end, 2 * treenode + 2)
n1 = segment_tree[2 * treenode + 1]
n2 = segment_tree[2 * treenode + 2]
node = Node()
node.min_val = min(n1.min_val, n2.min_val)
node.xory = n1.xory | n2.xory
segment_tree[treenode] = node
def update_query(start, end, left, right, value, treenode=0):
global segment_tree
if left > end or right < start:
return
elif start == end and left <= start and right >= end:
segment_tree[treenode].min_val = segment_tree[treenode].min_val & value
segment_tree[treenode].xory = segment_tree[treenode].min_val
return
else:
call = False
for bit_count in range(32):
if (
not value >> bit_count & 1
and segment_tree[treenode].xory >> bit_count & 1
):
call = True
break
if not call:
return
mid = (start + end) // 2
update_query(start, mid, left, right, value, 2 * treenode + 1)
update_query(mid + 1, end, left, right, value, 2 * treenode + 2)
n1 = segment_tree[2 * treenode + 1]
n2 = segment_tree[2 * treenode + 2]
segment_tree[treenode].min_val = min(n1.min_val, n2.min_val)
segment_tree[treenode].xory = n1.xory | n2.xory
def retrieve_query(start, end, left, right, treenode=0):
if left > end or right < start:
return 10**9
elif left <= start and right >= end:
return segment_tree[treenode].min_val
else:
mid = (start + end) // 2
ans1 = retrieve_query(start, mid, left, right, 2 * treenode + 1)
ans2 = retrieve_query(mid + 1, end, left, right, 2 * treenode + 2)
return min(ans1, ans2)
build_tree(0, len(arr) - 1)
for _ in range(q):
query = list(map(int, input().split()))
if query[0] == 0:
print(retrieve_query(0, len(arr) - 1, query[1] - 1, query[2] - 1))
else:
update_query(0, len(arr) - 1, query[1] - 1, query[2] - 1, query[3]) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_DEF NUMBER IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_DEF NUMBER IF VAR VAR VAR VAR RETURN BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER |
You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string s_j consists of k lowercase Latin letters.
A string a matches a pattern b if for each i from 1 to k either b_i is a wildcard character or b_i=a_i.
You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mt_j]. You are allowed to leave the order of the patterns unchanged.
Can you perform such a rearrangement? If you can, then print any valid order.
Input
The first line contains three integers n, m and k (1 β€ n, m β€ 10^5, 1 β€ k β€ 4) β the number of patterns, the number of strings and the length of each pattern and string.
Each of the next n lines contains a pattern β k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.
Each of the next m lines contains a string β k lowercase Latin letters, and an integer mt (1 β€ mt β€ n) β the index of the first pattern the corresponding string should match.
Output
Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mt_j].
Otherwise, print "YES" in the first line. The second line should contain n distinct integers from 1 to n β the order of the patterns. If there are multiple answers, print any of them.
Examples
Input
5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5
Output
YES
3 2 4 5 1
Input
1 1 3
__c
cba 1
Output
NO
Input
2 2 2
a_
_b
ab 1
ab 2
Output
NO
Note
The order of patterns after the rearrangement in the first example is the following:
* aaaa
* __b_
* ab__
* _bcd
* _b_d
Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed p[4]. The second string matches __b_ and ab__, the first of them is __b_, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].
The answer to that test is not unique, other valid orders also exist.
In the second example cba doesn't match __c, thus, no valid order exists.
In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (_b, a_) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2. | from sys import gettrace, stdin
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def patterns(s):
if len(s) == 1:
return [s, "_"]
else:
tp = patterns(s[1:])
return [(s[0] + t) for t in tp] + [("_" + t) for t in tp]
def main():
n, m, k = map(int, input().split())
pp = (input() for _ in range(n))
ppm = {}
for i, p in enumerate(pp):
ppm[p] = i
pre = [0] * n
suc = [[] for _ in range(n)]
for _ in range(m):
s, ml = input().split()
ml = int(ml) - 1
ps = patterns(s)
found = False
for p in ps:
if p in ppm:
if ppm[p] == ml:
found = True
else:
pre[ppm[p]] += 1
suc[ml].append(ppm[p])
if not found:
print("NO")
return
znodes = [i for i in range(n) if pre[i] == 0]
res = []
while znodes:
i = znodes.pop()
res.append(i + 1)
for j in suc[i]:
pre[j] -= 1
if pre[j] == 0:
znodes.append(j)
if len(res) == n:
print("YES")
print(" ".join(map(str, res)))
else:
print("NO")
main() | IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP STRING VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string s_j consists of k lowercase Latin letters.
A string a matches a pattern b if for each i from 1 to k either b_i is a wildcard character or b_i=a_i.
You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mt_j]. You are allowed to leave the order of the patterns unchanged.
Can you perform such a rearrangement? If you can, then print any valid order.
Input
The first line contains three integers n, m and k (1 β€ n, m β€ 10^5, 1 β€ k β€ 4) β the number of patterns, the number of strings and the length of each pattern and string.
Each of the next n lines contains a pattern β k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.
Each of the next m lines contains a string β k lowercase Latin letters, and an integer mt (1 β€ mt β€ n) β the index of the first pattern the corresponding string should match.
Output
Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mt_j].
Otherwise, print "YES" in the first line. The second line should contain n distinct integers from 1 to n β the order of the patterns. If there are multiple answers, print any of them.
Examples
Input
5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5
Output
YES
3 2 4 5 1
Input
1 1 3
__c
cba 1
Output
NO
Input
2 2 2
a_
_b
ab 1
ab 2
Output
NO
Note
The order of patterns after the rearrangement in the first example is the following:
* aaaa
* __b_
* ab__
* _bcd
* _b_d
Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed p[4]. The second string matches __b_ and ab__, the first of them is __b_, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].
The answer to that test is not unique, other valid orders also exist.
In the second example cba doesn't match __c, thus, no valid order exists.
In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (_b, a_) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2. | import sys
input = sys.stdin.readline
def topological_sorted(digraph):
n = len(digraph)
indegree = [0] * n
for v in range(n):
for nxt_v in digraph[v]:
indegree[nxt_v] += 1
tp_order = [i for i in range(n) if indegree[i] == 0]
stack = tp_order[:]
while stack:
v = stack.pop()
for nxt_v in digraph[v]:
indegree[nxt_v] -= 1
if indegree[nxt_v] == 0:
stack.append(nxt_v)
tp_order.append(nxt_v)
return len(tp_order) == n, tp_order
n, m, k = map(int, input().split())
p = [input()[:-1] for i in range(n)]
s = [list(input().split()) for i in range(m)]
memo = {}
for idx, ptn in enumerate(p):
val = 0
for i in range(k):
if ptn[i] == "_":
continue
val += (ord(ptn[i]) - 96) * 27**i
memo[val] = idx
for i, (string, idx) in enumerate(s):
s[i] = tuple(map(ord, string)), int(idx)
graph = [[] for i in range(n)]
for string, idx in s:
idxs = []
idx -= 1
for bit_state in range(1 << k):
val = 0
for i in range(k):
if bit_state >> i & 1:
continue
val += (string[i] - 96) * 27**i
if val in memo:
idxs.append(memo[val])
if idx not in idxs:
print("NO")
exit()
for idx_to in idxs:
if idx == idx_to:
continue
graph[idx].append(idx_to)
flag, res = topological_sorted(graph)
if flag:
print("YES")
print(*[(i + 1) for i in res])
else:
print("NO") | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING |
You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string s_j consists of k lowercase Latin letters.
A string a matches a pattern b if for each i from 1 to k either b_i is a wildcard character or b_i=a_i.
You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mt_j]. You are allowed to leave the order of the patterns unchanged.
Can you perform such a rearrangement? If you can, then print any valid order.
Input
The first line contains three integers n, m and k (1 β€ n, m β€ 10^5, 1 β€ k β€ 4) β the number of patterns, the number of strings and the length of each pattern and string.
Each of the next n lines contains a pattern β k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.
Each of the next m lines contains a string β k lowercase Latin letters, and an integer mt (1 β€ mt β€ n) β the index of the first pattern the corresponding string should match.
Output
Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mt_j].
Otherwise, print "YES" in the first line. The second line should contain n distinct integers from 1 to n β the order of the patterns. If there are multiple answers, print any of them.
Examples
Input
5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5
Output
YES
3 2 4 5 1
Input
1 1 3
__c
cba 1
Output
NO
Input
2 2 2
a_
_b
ab 1
ab 2
Output
NO
Note
The order of patterns after the rearrangement in the first example is the following:
* aaaa
* __b_
* ab__
* _bcd
* _b_d
Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed p[4]. The second string matches __b_ and ab__, the first of them is __b_, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].
The answer to that test is not unique, other valid orders also exist.
In the second example cba doesn't match __c, thus, no valid order exists.
In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (_b, a_) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2. | import sys
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())
P = [""]
for i in range(N):
p = input().rstrip("\n")
P.append(p)
p2i = {p: i for i, p in enumerate(P)}
adj = [set() for _ in range(N + 1)]
for i in range(M):
s, mt = input().split()
mt = int(mt)
ok = 0
for k in range(1 << K):
s_new = ["_"] * K
for j in range(K):
if k >> j & 1:
s_new[j] = s[j]
s_new = "".join(s_new)
if s_new != P[mt]:
if s_new in p2i:
adj[mt].add(p2i[s_new])
else:
ok = 1
if not ok:
print("NO")
exit()
in_num = [0] * (N + 1)
for v in range(1, N + 1):
for u in adj[v]:
in_num[u] += 1
st = []
for v in range(1, N + 1):
if in_num[v] == 0:
st.append(v)
ans = []
while st:
v = st.pop()
ans.append(v)
for u in adj[v]:
in_num[u] -= 1
if in_num[u] == 0:
st.append(u)
if len(ans) == N:
print("YES")
print(*ans)
else:
print("NO")
main() | IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string s_j consists of k lowercase Latin letters.
A string a matches a pattern b if for each i from 1 to k either b_i is a wildcard character or b_i=a_i.
You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mt_j]. You are allowed to leave the order of the patterns unchanged.
Can you perform such a rearrangement? If you can, then print any valid order.
Input
The first line contains three integers n, m and k (1 β€ n, m β€ 10^5, 1 β€ k β€ 4) β the number of patterns, the number of strings and the length of each pattern and string.
Each of the next n lines contains a pattern β k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.
Each of the next m lines contains a string β k lowercase Latin letters, and an integer mt (1 β€ mt β€ n) β the index of the first pattern the corresponding string should match.
Output
Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mt_j].
Otherwise, print "YES" in the first line. The second line should contain n distinct integers from 1 to n β the order of the patterns. If there are multiple answers, print any of them.
Examples
Input
5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5
Output
YES
3 2 4 5 1
Input
1 1 3
__c
cba 1
Output
NO
Input
2 2 2
a_
_b
ab 1
ab 2
Output
NO
Note
The order of patterns after the rearrangement in the first example is the following:
* aaaa
* __b_
* ab__
* _bcd
* _b_d
Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed p[4]. The second string matches __b_ and ab__, the first of them is __b_, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].
The answer to that test is not unique, other valid orders also exist.
In the second example cba doesn't match __c, thus, no valid order exists.
In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (_b, a_) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2. | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
trie = [{}]
def push(s, val):
now = 0
for c in s:
if c not in trie[now]:
trie[now][c] = len(trie)
trie.append({})
now = trie[now][c]
trie[now]["end"] = val
def match(s):
res = []
stack = [(0, 0)]
while stack:
u, i = stack.pop()
if i == k:
res.append(trie[u]["end"])
continue
if s[i] in trie[u]:
stack.append((trie[u][s[i]], i + 1))
if "_" in trie[u]:
stack.append((trie[u]["_"], i + 1))
return res
n, m, k = MI()
for i in range(n):
push(SI(), i)
to = [[] for _ in range(n)]
for _ in range(m):
s, u = SI().split()
u = int(u) - 1
vv = match(s)
notmatch = True
for v in vv:
if u == v:
notmatch = False
else:
to[u].append(v)
if notmatch:
print("NO")
exit()
vis = [-1] * n
topo = []
for u in range(n):
if vis[u] == 1:
continue
stack = [u]
while stack:
u = stack.pop()
if vis[u] == -1:
vis[u] = 0
stack.append(u)
for v in to[u]:
if vis[v] == 0:
print("NO")
exit()
if vis[v] == -1:
stack.append(v)
elif vis[u] == 0:
topo.append(u + 1)
vis[u] = 1
print("YES")
print(*topo[::-1]) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST DICT FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR STRING VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF STRING VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string s_j consists of k lowercase Latin letters.
A string a matches a pattern b if for each i from 1 to k either b_i is a wildcard character or b_i=a_i.
You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mt_j]. You are allowed to leave the order of the patterns unchanged.
Can you perform such a rearrangement? If you can, then print any valid order.
Input
The first line contains three integers n, m and k (1 β€ n, m β€ 10^5, 1 β€ k β€ 4) β the number of patterns, the number of strings and the length of each pattern and string.
Each of the next n lines contains a pattern β k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.
Each of the next m lines contains a string β k lowercase Latin letters, and an integer mt (1 β€ mt β€ n) β the index of the first pattern the corresponding string should match.
Output
Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mt_j].
Otherwise, print "YES" in the first line. The second line should contain n distinct integers from 1 to n β the order of the patterns. If there are multiple answers, print any of them.
Examples
Input
5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5
Output
YES
3 2 4 5 1
Input
1 1 3
__c
cba 1
Output
NO
Input
2 2 2
a_
_b
ab 1
ab 2
Output
NO
Note
The order of patterns after the rearrangement in the first example is the following:
* aaaa
* __b_
* ab__
* _bcd
* _b_d
Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed p[4]. The second string matches __b_ and ab__, the first of them is __b_, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].
The answer to that test is not unique, other valid orders also exist.
In the second example cba doesn't match __c, thus, no valid order exists.
In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (_b, a_) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2. | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
N, M, K = map(int, input().split())
P = []
D_P = {}
for i in range(N):
S = input()
P.append(S)
D_P[S] = i
adj = [[] for _ in range(N)]
indeg = [0] * N
for _ in range(M):
S, mt = input().split()
mt = int(mt) - 1
fp = P[mt]
if any(fp[i] not in (S[i], "_") for i in range(K)):
print("NO")
raise SystemExit
for bs in range(1 << K):
pat = "".join(S[i] if bs & 1 << i == 0 else "_" for i in range(K))
if pat == fp:
continue
if pat in D_P:
j = D_P[pat]
indeg[j] += 1
adj[mt].append(j)
Q = [i for i in range(N) if indeg[i] == 0]
for i in Q:
for j in adj[i]:
indeg[j] -= 1
if indeg[j] == 0:
Q.append(j)
if len(Q) == N:
print("YES")
print(" ".join(str(v + 1) for v in Q))
else:
print("NO") | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR STRING VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING |
You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string s_j consists of k lowercase Latin letters.
A string a matches a pattern b if for each i from 1 to k either b_i is a wildcard character or b_i=a_i.
You are asked to rearrange the patterns in such a way that the first pattern the j-th string matches is p[mt_j]. You are allowed to leave the order of the patterns unchanged.
Can you perform such a rearrangement? If you can, then print any valid order.
Input
The first line contains three integers n, m and k (1 β€ n, m β€ 10^5, 1 β€ k β€ 4) β the number of patterns, the number of strings and the length of each pattern and string.
Each of the next n lines contains a pattern β k characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct.
Each of the next m lines contains a string β k lowercase Latin letters, and an integer mt (1 β€ mt β€ n) β the index of the first pattern the corresponding string should match.
Output
Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the j-th string matches is p[mt_j].
Otherwise, print "YES" in the first line. The second line should contain n distinct integers from 1 to n β the order of the patterns. If there are multiple answers, print any of them.
Examples
Input
5 3 4
_b_d
__b_
aaaa
ab__
_bcd
abcd 4
abba 2
dbcd 5
Output
YES
3 2 4 5 1
Input
1 1 3
__c
cba 1
Output
NO
Input
2 2 2
a_
_b
ab 1
ab 2
Output
NO
Note
The order of patterns after the rearrangement in the first example is the following:
* aaaa
* __b_
* ab__
* _bcd
* _b_d
Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed p[4]. The second string matches __b_ and ab__, the first of them is __b_, that is p[2]. The last string matches _bcd and _b_d, the first of them is _bcd, that is p[5].
The answer to that test is not unique, other valid orders also exist.
In the second example cba doesn't match __c, thus, no valid order exists.
In the third example the order (a_, _b) makes both strings match pattern 1 first and the order (_b, a_) makes both strings match pattern 2 first. Thus, there is no order that produces the result 1 and 2. | import sys
I = sys.stdin.readlines()
N, M, K = map(int, I[0].split())
S = [I[i + 1][:-1] for i in range(N)]
D = dict()
for i in range(N):
D[S[i]] = i
T = [I[i + N + 1].split() for i in range(M)]
for i in range(M):
T[i][1] = int(T[i][1]) - 1
G = [[] for i in range(N)]
C = [0] * N
for i in range(M):
for j in range(K):
if S[T[i][1]][j] != "_" and S[T[i][1]][j] != T[i][0][j]:
print("NO")
exit()
for j in range(1 << K):
t = "".join([("_" if j & 1 << k else T[i][0][k]) for k in range(K)])
x = D.get(t, -1)
if x != -1 and x != T[i][1]:
G[T[i][1]].append(x)
C[x] += 1
P = []
Q = []
F = [1] * N
for i in range(N):
if C[i] == 0 and F[i]:
Q.append(i)
while len(Q):
v = Q.pop()
F[v] = 0
P.append(v + 1)
for i in range(len(G[v])):
C[G[v][i]] -= 1
if C[G[v][i]] == 0:
Q.append(G[v][i])
if len(P) == N:
print("YES")
print(*P)
else:
print("NO") | IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR STRING VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR BIN_OP NUMBER VAR STRING VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given $n$ permutations $a_1, a_2, \dots, a_n$, each of length $m$. Recall that a permutation of length $m$ is a sequence of $m$ distinct integers from $1$ to $m$.
Let the beauty of a permutation $p_1, p_2, \dots, p_m$ be the largest $k$ such that $p_1 = 1, p_2 = 2, \dots, p_k = k$. If $p_1 \neq 1$, then the beauty is $0$.
The product of two permutations $p \cdot q$ is a permutation $r$ such that $r_j = q_{p_j}$.
For each $i$ from $1$ to $n$, print the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ from $1$ to $n$ (possibly, $i = j$).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 5 \cdot 10^4$; $1 \le m \le 10$) β the number of permutations and the length of each permutation.
The $i$-th of the next $n$ lines contains a permutation $a_i$ β $m$ distinct integers from $1$ to $m$.
The sum of $n$ doesn't exceed $5 \cdot 10^4$ over all testcases.
-----Output-----
For each testcase, print $n$ integers. The $i$-th value should be equal to the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ ($1 \le j \le n$).
-----Examples-----
Input
3
3 4
2 4 1 3
1 2 4 3
2 1 3 4
2 2
1 2
2 1
8 10
3 4 9 6 10 2 7 8 1 5
3 9 1 8 5 7 4 10 2 6
3 10 1 7 5 9 6 4 2 8
1 2 3 4 8 6 10 7 9 5
1 2 3 4 10 6 8 5 7 9
9 6 1 2 10 4 7 8 3 5
7 9 3 2 5 6 4 8 1 10
9 4 3 7 5 6 1 10 8 2
Output
1 4 4
2 2
10 8 1 6 8 10 1 7
-----Note-----
None | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
t = {}
y = []
for _ in range(n):
z = list(map(int, input().split()))
q = [0] * (m + 1)
for i in range(m):
q[z[i]] = i + 1
x = t
for num in q[1:]:
if not num in x:
x[num] = {}
x = x[num]
y.append(z)
ans2 = []
for data in y:
ans = 0
x = t
for num in data:
if num in x:
ans += 1
x = x[num]
else:
break
ans2.append(ans)
print(*ans2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given $n$ permutations $a_1, a_2, \dots, a_n$, each of length $m$. Recall that a permutation of length $m$ is a sequence of $m$ distinct integers from $1$ to $m$.
Let the beauty of a permutation $p_1, p_2, \dots, p_m$ be the largest $k$ such that $p_1 = 1, p_2 = 2, \dots, p_k = k$. If $p_1 \neq 1$, then the beauty is $0$.
The product of two permutations $p \cdot q$ is a permutation $r$ such that $r_j = q_{p_j}$.
For each $i$ from $1$ to $n$, print the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ from $1$ to $n$ (possibly, $i = j$).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 5 \cdot 10^4$; $1 \le m \le 10$) β the number of permutations and the length of each permutation.
The $i$-th of the next $n$ lines contains a permutation $a_i$ β $m$ distinct integers from $1$ to $m$.
The sum of $n$ doesn't exceed $5 \cdot 10^4$ over all testcases.
-----Output-----
For each testcase, print $n$ integers. The $i$-th value should be equal to the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ ($1 \le j \le n$).
-----Examples-----
Input
3
3 4
2 4 1 3
1 2 4 3
2 1 3 4
2 2
1 2
2 1
8 10
3 4 9 6 10 2 7 8 1 5
3 9 1 8 5 7 4 10 2 6
3 10 1 7 5 9 6 4 2 8
1 2 3 4 8 6 10 7 9 5
1 2 3 4 10 6 8 5 7 9
9 6 1 2 10 4 7 8 3 5
7 9 3 2 5 6 4 8 1 10
9 4 3 7 5 6 1 10 8 2
Output
1 4 4
2 2
10 8 1 6 8 10 1 7
-----Note-----
None | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
all = set()
for i in range(n):
prefix = ""
for j in range(m):
prefix += str(a[i].index(j + 1) + 1)
all.add(prefix)
res = []
for i in range(n):
tmp = 0
prefix = ""
for j in range(m):
prefix += str(a[i][j])
if prefix in all:
tmp += 1
else:
break
res.append(tmp)
print(*res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given $n$ permutations $a_1, a_2, \dots, a_n$, each of length $m$. Recall that a permutation of length $m$ is a sequence of $m$ distinct integers from $1$ to $m$.
Let the beauty of a permutation $p_1, p_2, \dots, p_m$ be the largest $k$ such that $p_1 = 1, p_2 = 2, \dots, p_k = k$. If $p_1 \neq 1$, then the beauty is $0$.
The product of two permutations $p \cdot q$ is a permutation $r$ such that $r_j = q_{p_j}$.
For each $i$ from $1$ to $n$, print the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ from $1$ to $n$ (possibly, $i = j$).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 5 \cdot 10^4$; $1 \le m \le 10$) β the number of permutations and the length of each permutation.
The $i$-th of the next $n$ lines contains a permutation $a_i$ β $m$ distinct integers from $1$ to $m$.
The sum of $n$ doesn't exceed $5 \cdot 10^4$ over all testcases.
-----Output-----
For each testcase, print $n$ integers. The $i$-th value should be equal to the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ ($1 \le j \le n$).
-----Examples-----
Input
3
3 4
2 4 1 3
1 2 4 3
2 1 3 4
2 2
1 2
2 1
8 10
3 4 9 6 10 2 7 8 1 5
3 9 1 8 5 7 4 10 2 6
3 10 1 7 5 9 6 4 2 8
1 2 3 4 8 6 10 7 9 5
1 2 3 4 10 6 8 5 7 9
9 6 1 2 10 4 7 8 3 5
7 9 3 2 5 6 4 8 1 10
9 4 3 7 5 6 1 10 8 2
Output
1 4 4
2 2
10 8 1 6 8 10 1 7
-----Note-----
None | import sys
def make_empty_dict(size):
return {idx: None for idx in range(1, size + 1)}
def make_tree(rev):
m = len(rev[0])
root = make_empty_dict(size=m)
for perm in rev:
tmp = root
for el in perm:
if tmp[el] is None:
tmp[el] = make_empty_dict(size=m)
tmp = tmp[el]
return root
def search_path(tree, perm):
lvl = 0
tmp = tree
for el in perm:
if tmp[el] is None:
break
else:
tmp = tmp[el]
lvl += 1
return lvl
def solution():
n, m = map(int, input().split())
data = []
rev = []
for _ in range(n):
data.append(list(map(int, input().split())))
perm = [0] * m
for idx, el in enumerate(data[-1]):
perm[el - 1] = idx + 1
rev.append(perm)
tree = make_tree(rev)
ans = [search_path(tree, perm) for perm in data]
print(*ans)
t = int(input())
for _ in range(t):
solution() | IMPORT FUNC_DEF RETURN VAR NONE VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given $n$ permutations $a_1, a_2, \dots, a_n$, each of length $m$. Recall that a permutation of length $m$ is a sequence of $m$ distinct integers from $1$ to $m$.
Let the beauty of a permutation $p_1, p_2, \dots, p_m$ be the largest $k$ such that $p_1 = 1, p_2 = 2, \dots, p_k = k$. If $p_1 \neq 1$, then the beauty is $0$.
The product of two permutations $p \cdot q$ is a permutation $r$ such that $r_j = q_{p_j}$.
For each $i$ from $1$ to $n$, print the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ from $1$ to $n$ (possibly, $i = j$).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 5 \cdot 10^4$; $1 \le m \le 10$) β the number of permutations and the length of each permutation.
The $i$-th of the next $n$ lines contains a permutation $a_i$ β $m$ distinct integers from $1$ to $m$.
The sum of $n$ doesn't exceed $5 \cdot 10^4$ over all testcases.
-----Output-----
For each testcase, print $n$ integers. The $i$-th value should be equal to the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ ($1 \le j \le n$).
-----Examples-----
Input
3
3 4
2 4 1 3
1 2 4 3
2 1 3 4
2 2
1 2
2 1
8 10
3 4 9 6 10 2 7 8 1 5
3 9 1 8 5 7 4 10 2 6
3 10 1 7 5 9 6 4 2 8
1 2 3 4 8 6 10 7 9 5
1 2 3 4 10 6 8 5 7 9
9 6 1 2 10 4 7 8 3 5
7 9 3 2 5 6 4 8 1 10
9 4 3 7 5 6 1 10 8 2
Output
1 4 4
2 2
10 8 1 6 8 10 1 7
-----Note-----
None | for _ in range(int(input())):
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [([0] * m) for _ in range(n)]
for i in range(n):
for j in range(m):
a[i][j] -= 1
b[i][a[i][j]] = j
c = [set() for _ in range(m)]
for i in range(n):
h = 0
for j in range(m):
h = h * 10 + b[i][j]
c[j].add(h)
ans = []
for i in range(n):
h = 0
t = 0
for j in range(m):
h = h * 10 + a[i][j]
if h in c[j]:
t = j + 1
ans += (t,)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given $n$ permutations $a_1, a_2, \dots, a_n$, each of length $m$. Recall that a permutation of length $m$ is a sequence of $m$ distinct integers from $1$ to $m$.
Let the beauty of a permutation $p_1, p_2, \dots, p_m$ be the largest $k$ such that $p_1 = 1, p_2 = 2, \dots, p_k = k$. If $p_1 \neq 1$, then the beauty is $0$.
The product of two permutations $p \cdot q$ is a permutation $r$ such that $r_j = q_{p_j}$.
For each $i$ from $1$ to $n$, print the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ from $1$ to $n$ (possibly, $i = j$).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 5 \cdot 10^4$; $1 \le m \le 10$) β the number of permutations and the length of each permutation.
The $i$-th of the next $n$ lines contains a permutation $a_i$ β $m$ distinct integers from $1$ to $m$.
The sum of $n$ doesn't exceed $5 \cdot 10^4$ over all testcases.
-----Output-----
For each testcase, print $n$ integers. The $i$-th value should be equal to the largest beauty of a permutation $a_i \cdot a_j$ over all $j$ ($1 \le j \le n$).
-----Examples-----
Input
3
3 4
2 4 1 3
1 2 4 3
2 1 3 4
2 2
1 2
2 1
8 10
3 4 9 6 10 2 7 8 1 5
3 9 1 8 5 7 4 10 2 6
3 10 1 7 5 9 6 4 2 8
1 2 3 4 8 6 10 7 9 5
1 2 3 4 10 6 8 5 7 9
9 6 1 2 10 4 7 8 3 5
7 9 3 2 5 6 4 8 1 10
9 4 3 7 5 6 1 10 8 2
Output
1 4 4
2 2
10 8 1 6 8 10 1 7
-----Note-----
None | import sys
input = sys.stdin.readline
class TrieNode:
def __init__(self, v=None):
self.children = {}
self.val = v
self.end = False
def readList():
return list(map(int, input().split()))
def readInt():
return int(input())
def readInts():
return map(int, input().split())
def readStr():
return input().strip()
def solve():
n, m = readInts()
arr = [readList() for _ in range(n)]
root = TrieNode()
for i in range(n):
node = root
inv = [0] * m
for j in range(m):
inv[arr[i][j] - 1] = j + 1
for j in range(m):
if inv[j] not in node.children:
node.children[inv[j]] = TrieNode(inv[j])
node = node.children[inv[j]]
ans = []
for i in range(n):
node = root
cnt = 0
for j in range(m):
if arr[i][j] in node.children:
node = node.children[arr[i][j]]
cnt += 1
else:
break
ans.append(cnt)
return ans
for _ in range(int(input())):
print(*solve()) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NONE ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
pass
count = 0
for i in range(64):
for j in range(i + 1, 64):
for k in range(j + 1, 64):
binary = 0
binary |= 1 << i
binary |= 1 << j
binary |= 1 << k
if binary >= L and binary <= R:
count += 1
return count
def precompute(self):
pass | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
pass
i = 1
c = 0
while i < R:
j = i << 1
while j < R:
k = j << 1
while k < R:
tmp = i | j | k
if L <= tmp and tmp <= R:
c += 1
k <<= 1
j <<= 1
i <<= 1
return c
def precompute(self):
pass | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
def binarySearch(a, n, x):
l, r = 0, n - 1
while l <= r:
m = (l + r) // 2
if a[m] == x:
return m
elif a[m] > x:
r = m - 1
else:
l = m + 1
return l
a = binarySearch(self.dp, self.n, L)
b = binarySearch(self.dp, self.n, R)
return b - a + 1 if self.dp[b] == R else b - a
def precompute(self):
self.dp = []
self.n = 0
for i in range(62, -1, -1):
a = 1 << i
for j in range(i - 1, -1, -1):
if i == j:
continue
b = 1 << j
for k in range(j - 1, -1, -1):
if j == k:
continue
c = 1 << k
self.dp.append(a + b + c)
self.n += 1
self.dp.reverse() | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def __init__(self):
self.allnos = []
def solve(self, L, R):
ans = 0
for i in range(len(self.allnos)):
if self.allnos[i] >= L and self.allnos[i] <= R:
ans += 1
return ans
def precompute(self):
for i in range(63):
for j in range(i + 1, 63):
for k in range(j + 1, 63):
curr = 0
curr |= 1 << i
curr |= 1 << j
curr |= 1 << k
self.allnos.append(curr)
pass | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
ans = 0
tempans = 0
pi = 1
for i in range(65):
tempans += pi
pj = pi * 2
for j in range(i + 1, 65, 1):
tempans += pj
pk = pj * 2
for k in range(j + 1, 65, 1):
if L <= tempans + pk <= R:
ans += 1
pk *= 2
tempans -= pj
pj *= 2
tempans -= pi
pi *= 2
return ans
def precompute(self):
pass | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
a = [0] * 64
s = 0
c = 0
for i in range(0, 64):
a[i] = pow(2, i)
for i in range(0, 64):
for j in range(i + 1, 64):
for k in range(j + 1, 64):
s = a[i] + a[j] + a[k]
if s >= L and s <= R:
c += 1
if s > R:
break
return c
def precompute(self):
pass | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | from itertools import combinations as com
def binatodeci(binary):
return sum(val * 2**idx for idx, val in enumerate(reversed(binary)))
def postobina(arr, n):
res = [(0) for i in range(n)]
for ele in arr:
res[ele] = 1
return res
class Solution:
def solve(self, L, R):
br = [int(i) for i in list("{0:0b}".format(R))]
lisr = [i for i in range(len(br))]
setr = list(com(lisr, 3))
set2 = [list(setr[i]) for i in range(len(setr))]
ans = set()
for i in range(len(set2)):
num = binatodeci(postobina(set2[i], len(br)))
if num >= L and num <= R:
ans.add(num)
count = len(ans)
return count
def precompute(self):
pass | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
answer = 0
my_range = 64
for i in range(my_range):
for j in range(i + 1, my_range):
for k in range(j + 1, my_range):
tmp_answer = (1 << i) + (1 << j) + (1 << k)
if tmp_answer >= L and tmp_answer <= R:
answer += 1
return answer
def precompute(self):
pass | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def get_count(self, n):
binary = bin(n)
ind = 3
count = 0
l = len(binary)
for i in range(l):
if binary[i] == "1":
count += self.dp[l - i - 1][ind]
ind -= 1
if ind < 1:
count += 1
break
return count
def solve(self, L, R):
lower_bound = self.get_count(L - 1)
upper_bound = self.get_count(R)
return upper_bound - lower_bound
def precompute(self):
self.dp = [([0] * 4) for i in range(65)]
self.dp[0][0] = 1
for i in range(1, 65):
self.dp[i][0] = 1
for j in range(3, 0, -1):
self.dp[i][j] = self.dp[i - 1][j] + self.dp[i - 1][j - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
num1 = L
num2 = R
bin_num1 = format(num1, "b")
bin_num2 = format(num2, "b")
Km = len(bin_num1) - 1
Kn = len(bin_num2) - 1
res_num1, num1_second_pos, num1_third_pos = self.addBinaryDigits(bin_num1)
res_num2, num2_second_pos, num2_third_pos = self.addBinaryDigits(bin_num2)
SetsFromPowerToPower = self.SetsPowerToConsecutivePower(Km, Kn)
M = self.SetsFromPowerToNumber(res_num1, num1_second_pos, num1_third_pos, Km)
if res_num2 == 3:
N = (
self.SetsFromPowerToNumber(
res_num2, num2_second_pos, num2_third_pos, Kn
)
+ 1
)
else:
N = self.SetsFromPowerToNumber(
res_num2, num2_second_pos, num2_third_pos, Kn
)
Total = SetsFromPowerToPower + N - M
return int(Total)
def precompute(self):
pass
def addBinaryDigits(self, binary):
res = 0
counter = 0
second_pos = -1
third_pos = -1
idx = 0
for char in binary:
res += int(char)
if char == "1":
counter += 1
if counter == 2:
second_pos = idx
elif counter == 3:
third_pos = idx
idx += 1
return res, len(binary) - 1 - second_pos, len(binary) - 1 - third_pos
def SetsFromPowerToNumber(self, DigSum, SecondPos, ThirdPos, MaxPower):
theta_0 = 0
theta_1 = 0
SetsPN = 0
if MaxPower == 0 or MaxPower == 1 or MaxPower == 2 or DigSum == 1:
theta_0 = 0
theta_1 = 0
elif DigSum == 2:
if SecondPos == 0 or SecondPos == 1:
theta_1 = 2
else:
theta_1 = SecondPos + 1
theta_0 = 1
elif DigSum == 3:
theta_1 = SecondPos + 1
theta_0 = ThirdPos + 1
elif DigSum >= 4 and SecondPos == ThirdPos + 1:
if MaxPower == SecondPos + 1:
theta_1 = MaxPower
theta_0 = theta_1
else:
theta_1 = SecondPos + 2
theta_0 = 1
else:
theta_1 = SecondPos + 1
theta_0 = ThirdPos + 2
SetsPN = (theta_1 - 1) * theta_1 / 2 - (theta_1 - theta_0)
return SetsPN
def SetsPowerToConsecutivePower(self, MaxPower1, MaxPower2):
SetsPP = 0
if MaxPower1 == MaxPower2:
True
else:
for i in range(MaxPower1, MaxPower2):
SetsPP += i * (i - 1) / 2
return SetsPP | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR EXPR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def linear_search(self, target):
for i in range(len(self.valid)):
if self.valid[i] >= target:
return i
def solve(self, L, R):
n = len(self.valid)
lower_ind = self.linear_search(L)
higher_ind = self.linear_search(R)
if self.valid[higher_ind] != R:
higher_ind -= 1
return higher_ind - lower_ind + 1
def precompute(self):
self.valid = set()
for i in range(64):
for j in range(i + 1, 64):
for k in range(j + 1, 64):
num = pow(2, i) + pow(2, j) + pow(2, k)
self.valid.add(num)
self.valid = sorted(self.valid) | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def solve(self, L, R):
lb = self.to_bin(L)
rb = self.to_bin(R)
while len(lb) < len(rb):
lb.append(0)
lpos = [i for i, x in enumerate(lb) if x]
rpos = [i for i, x in enumerate(rb) if x]
return self.ranking(rpos, 3) - self.ranking(lpos, 3) + (len(lpos) == 3)
def to_bin(self, x):
bins = []
while x > 0:
bins.append(x % 2)
x //= 2
return bins
def c(self, tol, x):
if tol < x:
return 0
ans = 1
for i in range(x):
ans = ans * (tol - i)
for i in range(x):
ans = ans // (i + 1)
return ans
def ranking(self, pos, cnt):
if cnt == 0:
return 1
if len(pos) == 0:
return 0
return self.c(pos[-1], cnt) + self.ranking(pos[:-1], cnt - 1)
def precompute(self):
pass | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF |
Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L β€ X β€ R".Akku is genius, she solved the problem easily. Can you do it ??
Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011
Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L β€ X β€ R".
Expected Time Complexity: O(63^{3}) + O(log(R-L))
Expected Auxiliary Space: O(63^{3})
Constraints:
1 β€ L β€ R β€ 10^{18} | class Solution:
def __init__(self):
self.tsl = self.precompute()
def solve(self, L, R):
def bsf(arr, k):
n = len(arr)
l, h, idx = 0, n - 1, 0
while l <= h:
m = (l + h) // 2
if arr[m] > k:
h = m - 1
else:
idx = m
l = m + 1
return idx
def bsc(arr, k):
n = len(arr)
l, h, idx = 0, n - 1, 0
while l <= h:
m = (l + h) // 2
if arr[m] < k:
l = m + 1
else:
idx = m
h = m - 1
return idx
x = bsf(self.tsl, L)
y = bsc(self.tsl, R)
if L not in self.tsl and R not in self.tsl:
return y - 1 - (x + 1) + 1
elif L not in self.tsl and R in self.tsl:
return y - (x + 1) + 1
elif L in self.tsl and R not in self.tsl:
return y - 1 - x + 1
else:
return y - x + 1
def precompute(self):
ans = []
for i in range(62):
for j in range(i + 1, 63):
for k in range(j + 1, 64):
ans.append(1 << i | 1 << j | 1 << k)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | def reVal(num):
if num >= 0 and num <= 9:
return chr(num + ord("0"))
else:
return chr(num - 10 + ord("A"))
class Solution:
def noofbits(self, inputNum, base):
index = 0
res = ""
count = 0
while inputNum > 0:
res += reVal(inputNum % base)
count += 1
inputNum = int(inputNum / base)
res = res[::-1]
return count
def baseEquiv(self, n, m):
low = 2
high = 32
while low <= high:
mid = (low + high) // 2
p = self.noofbits(n, mid)
if p == m:
return "Yes"
elif p > m:
low = mid + 1
else:
high = mid - 1
return "No" | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN STRING |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | class Solution:
def baseEquiv(self, n, m):
for i in range(2, 33):
if pow(i, m - 1) > n:
continue
if pow(i, m) > n:
return "Yes"
return "No" | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR RETURN STRING RETURN STRING |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | class Solution:
def baseEquiv(self, n, m):
for i in range(2, 33):
count = 0
temp = n
for j in range(m):
if not temp:
break
temp = int(temp / i)
count += 1
if temp == 0 and count == m:
return "Yes"
return "No" | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN STRING RETURN STRING |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | class Solution:
def baseEquiv(self, n, m):
for base in range(2, 33):
if self.find_basevalue(n, base) == m:
return "Yes"
return "No"
def find_basevalue(self, n, base):
if n < base:
return 1
return self.find_basevalue(n // base, base) + 1 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN STRING RETURN STRING FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | class Solution:
def baseEquiv(self, n, m):
for i in range(2, 32):
if i ** (m - 1) > n:
return "No"
elif i**m - 1 < n:
pass
else:
return "Yes"
return "No" | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN STRING IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN STRING RETURN STRING |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | class Solution:
def cntbit(self, n, base):
cnt = 0
while n > 0:
cnt += 1
n = n // base
return cnt
def baseEquiv(self, n, m):
i = 2
j = 32
while i <= j:
mid = (i + j) // 2
bcnt = self.cntbit(n, mid)
if bcnt > m:
i = mid + 1
elif bcnt < m:
j = mid - 1
else:
return "Yes"
return "No" | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN STRING RETURN STRING |
Given a number (n) and no. of digits (m) to represent the number, we have to check if we can represent n using exactly m digits in any base from 2 to 32.
Example 1:
Input: n = 8, m = 2
Output: Yes
Explanation: Possible in base 3 as 8 in base 3 is 22.
Example 2:
Input: n = 8, m = 3
Output: No
Explanation: Not possible in any base.
Your Task:
You dont need to read input or print anything. Complete the function baseEquiv() which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes..
Expected Time Complexity: O(logN).
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <=10^{9}
1 <= m <=32 | class Solution:
def baseEquiv(self, n, m):
for i in range(2, 33):
count = m
val = n
while val > 0:
val = val // i
count -= 1
if count == 0:
return "Yes"
return "No" | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.