description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
temp = bin(a).count("1") - bin(b).count("1")
ans = a
if temp == 0:
return ans
elif temp > 0:
for i in range(32):
if not temp:
break
if 1 << i & a:
ans ^= 1 << i
temp -= 1
else:
for i in range(32):
if not temp:
break
if not 1 << i & a:
ans += 1 << i
temp += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
s1 = bin(a)[2:]
s2 = bin(b)[2:]
c1 = s1.count("1")
c2 = s2.count("1")
if c1 == c2:
return a
elif c1 > c2:
k = c2
d = ""
i = 0
while k and i < len(s1):
if s1[i] == "1":
k -= 1
d += "1"
else:
d += "0"
i += 1
d += "0" * (len(s1) - i)
return int(d, 2)
else:
if len(s1) < c2:
q = "1" * c2
return int(q, 2)
k = c2 - c1
num = s1
j = num[::-1]
i = 0
h = ""
while k and i < len(s1):
if j[i] == "0":
h += "1"
k -= 1
else:
h += j[i]
i += 1
h += j[i:]
u = h[::-1]
return int(u, 2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def count(self, n):
count = 0
while n:
count, n = count + 1, n & n - 1
return count
def minVal(self, a, b):
a_count = self.count(a)
b_count = self.count(b)
a_bin = bin(a)[2:]
if b_count <= a_count:
a_bin = a_bin[::-1].replace("1", "0", a_count - b_count)[::-1]
else:
a_bin = a_bin[::-1].replace("0", "1", b_count - a_count)[::-1]
a_bin = "1" * (b_count - len(a_bin)) + a_bin
return int(a_bin, 2) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER STRING STRING BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER STRING STRING BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
setbit = bin(b).count("1")
ans = 0
for i in range(31, -1, -1):
if setbit:
mask = 1 << i
if a & mask:
ans |= mask
setbit -= 1
for i in range(0, 31):
if setbit:
mask = 1 << i
if a & mask == 0:
ans |= mask
setbit -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
c = b
bit = 0
while c > 0:
bit += c & 1
c >>= 1
l = []
for i in range(30, -1, -1):
if 1 << i & a:
l.append(1)
else:
l.append(0)
l = l[::-1]
n = len(l)
l1 = [0] * n
i = n - 1
while bit > 0 and i >= 0:
if l[i] == 1:
bit -= 1
l1[i] = 1
i -= 1
i = 0
while bit > 0:
if l1[i] == 0:
l1[i] = 1
bit -= 1
i += 1
c = 0
ans = 0
for i in l1:
ans += i * (1 << c)
c += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def countSetBits(self, n):
count = 0
while n:
n = n & n - 1
count += 1
return count
def minVal(self, a, b):
x = a
diff = self.countSetBits(b) - self.countSetBits(a)
mask = 1
if diff >= 0:
while diff:
if x & mask == 0:
x = x | mask
diff -= 1
mask <<= 1
return x
elif diff < 0:
diff = -diff
while diff:
if x & mask:
x = x & ~mask
diff -= 1
mask <<= 1
return x | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER WHILE VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
setBits = bin(b).count("1")
ans = 0
for i in range(30, -1, -1):
mask = 1 << i
s = a & mask
if s and setBits > 0:
ans |= 1 << i
setBits -= 1
for i in range(31):
if setBits > 0:
mask = 1 << i
s = ans & mask
if not s:
ans |= 1 << i
setBits -= 1
else:
break
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
m, n = a, b
c, d = 0, 0
lna = 0
while m:
if m & 1:
c += 1
m = m >> 1
lna += 1
while n:
if n & 1:
d += 1
n = n >> 1
if c == d:
return a
elif lna < d:
k = 1
while d:
d -= 1
k = k << 1
return k - 1
elif c > d:
m = a
diff = c - d
k = 1
count = 0
while diff:
if m & 1:
count += k
diff -= 1
m = m >> 1
k = k << 1
return a - count
else:
m = a
diff = d - c
k = 1
count = 0
while diff:
if m & 1 == 0:
count += k
diff -= 1
m = m >> 1
k = k << 1
return a + count | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def cout(self, a):
p = bin(a)[2:]
return p.count("1")
def minVal(self, a, b):
ma = self.cout(a)
mb = self.cout(b)
if ma == mb:
return a
elif ma > mb:
k = ma - mb
while k:
a = a - (a & ~(a - 1))
k -= 1
return a
else:
k = mb - ma
hig = bin(a)[2:]
i = 0
while k:
if -1 * (-1 - i) > len(hig):
a = a + 2**i
k -= 1
elif hig[-1 - i] == "0":
a = a + 2**i
k -= 1
i += 1
return a | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
b1 = bin(b)
b1 = b1[2 : len(b1)]
count = 0
for i in b1:
if i == "1":
count += 1
a1 = bin(a)
a1 = a1[2 : len(a1)]
res = []
for i in a1:
res.append(i)
for i in range(len(a1)):
if res[i] == "1" and count >= 1:
count -= 1
else:
res[i] = "0"
for i in range(len(a1) - 1, -1, -1):
if count > 0 and res[i] != "1":
res[i] = "1"
count -= 1
ans = ""
for i in range(len(res)):
ans += res[i]
ans2 = ""
while count >= 1:
ans2 += "1"
count -= 1
r = 0
ans = ans2 + ans
ans = ans[::-1]
for i in range(len(ans)):
if ans[i] == "1":
r += pow(2, i)
return r | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
n1 = bin(a)[2:]
ans = []
n2 = bin(b).count("1")
for i in n1:
if n2 == 0:
break
if i == "1":
ans.append("1")
n2 -= 1
else:
ans.append("0")
if n2 > 0:
for i in range(len(ans) - 1, -1, -1):
if ans[i] == "0":
if n2 > 0:
ans[i] = "1"
n2 -= 1
if n2 > 0:
ans = ["1"] * n2 + ans
if len(ans) < len(n1):
for i in range(len(ans), len(n1)):
ans.append("0")
res = 0
c = 0
for i in range(len(ans) - 1, -1, -1):
if ans[i] == "1":
res += 2**c
c += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING FOR VAR VAR IF VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
a_bin = []
b_bin = []
while a >= 1:
a_bin.append(a % 2)
a = a // 2
a_bin.reverse()
while b >= 1:
b_bin.append(b % 2)
b = b // 2
b_bin.reverse()
if a_bin.count(1) == b_bin.count(1):
x = a_bin
elif a_bin.count(1) > b_bin.count(1):
x = a_bin
for i in range(1, len(x) + 1):
if x[-i] == 1:
x[-i] = 0
if x.count(1) == b_bin.count(1):
break
elif a_bin.count(1) < b_bin.count(1):
x = a_bin
for i in range(1, len(x) + 1):
if x[-i] == 0:
x[-i] = 1
if x.count(1) == b_bin.count(1):
break
if len(x) < b_bin.count(1):
for j in range(b_bin.count(1) - x.count(1)):
x.append(1)
num = 0
for i in range(1, len(x) + 1):
num = num + x[-i] * 2 ** (i - 1)
return num | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
def f(b):
cnt = 0
while b:
b &= b - 1
cnt += 1
return cnt
ans = []
bits = f(b)
l = list(bin(a)[2:])
for i in l:
if i == "1" and bits > 0:
bits -= 1
ans += ["1"]
elif i == "1" and bits == 0 or i == "0" and bits == 0:
ans += ["0"]
elif i == "0":
ans += ["0"]
for i in range(len(l) - 1, -1, -1):
if bits > 0 and ans[i] == "0":
ans[i] = "1"
bits -= 1
temp = []
while bits > 0:
temp += ["1"]
bits -= 1
ans = temp + ans
return int("".join(ans), 2) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER VAR LIST STRING IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR LIST STRING IF VAR STRING VAR LIST STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR LIST STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
def cnt(b):
c = 0
while b:
b = b & b - 1
c += 1
return c
ac = cnt(a)
ab = cnt(b)
d = abs(ac - ab)
if d == 0:
return a
elif ac > ab:
while d > 0:
a = a & a - 1
d -= 1
else:
while d > 0:
a |= a + 1
d -= 1
return a | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
count_bit_b = 0
while b:
count_bit_b += b % 2
b //= 2
ans = 0
for i in range(31, -1, -1):
if a & 1 << i:
ans |= 1 << i
count_bit_b -= 1
if count_bit_b == 0:
break
for i in range(0, 32):
if count_bit_b == 0:
break
if a & 1 << i == 0:
ans |= 1 << i
count_bit_b -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | def count(num):
c = 0
while num > 0:
if num % 2 == 1:
c += 1
num = num // 2
return c
class Solution:
def minVal(self, a, b):
c1 = count(a)
c2 = count(b)
if c1 == c2:
return a
elif c1 > c2:
diff = c1 - c2
while diff > 0:
a = a & a - 1
diff -= 1
return a
else:
diff = c2 - c1
while diff > 0:
a = a | a + 1
diff -= 1
return a | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
a = bin(a)[2:]
b = bin(b)[2:]
count = 0
for i in b:
count += i == "1"
s = ["0" for i in range(len(a))]
for i in range(len(a)):
if a[i] == "1" and count > 0:
s[i] = "1"
count -= 1
for i in range(len(s) - 1, -1, -1):
if s[i] == "0" and count > 0:
s[i] = "1"
count -= 1
if count > 0:
s.reverse()
while count:
s.append("1")
count -= 1
ans = 0
s.reverse()
for i in range(len(s) - 1, -1, -1):
if s[i] == "1":
ans += 1 << i
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR STRING ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
b_setbits = self.total_set_bits(b)
a_setbits = self.total_set_bits(a)
if a_setbits > b_setbits:
i = 0
remove_bits = a_setbits - b_setbits
while i < remove_bits:
a = a & a - 1
i += 1
return a
elif a_setbits < b_setbits:
diff = b_setbits - a_setbits
while diff:
a = a | a + 1
diff -= 1
return a
else:
return a
def total_set_bits(self, n):
count = 0
while n > 0:
n = n & n - 1
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
set_in_b = bin(b)[2:].count("1")
set_in_a = bin(a)[2:].count("1")
bi_a = len(bin(a)[2:])
bi_b = len(bin(b)[2:])
if set_in_a == set_in_b:
return a
if set_in_a > set_in_b:
ans = list("0" * abs(bi_b - bi_a) + bin(a)[2:])
for i in range(len(ans) - 1, -1, -1):
if ans.count("1") == set_in_b:
break
if ans[i] == "1":
ans[i] = "0"
else:
ans = list("0" * abs(bi_b - bi_a) + bin(a)[2:])
for i in range(len(ans) - 1, -1, -1):
if ans.count("1") == set_in_b:
break
if ans[i] == "0":
ans[i] = "1"
return int("".join(ans), 2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR STRING VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR STRING VAR IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
ba = db(a)
c1 = ba.count("1")
c2 = db(b).count("1")
if c1 == c2:
return a
if c2 > c1:
n = ""
j = c2 - c1
n = [i for i in ba]
for i in range(len(ba) - 1, -1, -1):
if j > 0:
if ba[i] == "0":
j = j - 1
n[i] = "1"
v = ""
for i in n:
v = v + i
if j > 0:
for i in range(j):
v = v + "1"
return int(v, 2)
if c1 > c2:
n = ""
l = len(ba)
k = 0
j = c2
for i in range(l):
if j > 0:
if ba[i] == "1":
j = j - 1
n = n + "1"
else:
n = n + "0"
else:
n = n + "0"
return int(n, 2)
def db(n):
return bin(n).replace("0b", "") | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING RETURN FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def countBits(self, n):
cnt = 0
while n:
n &= n - 1
cnt += 1
return cnt
def minVal(self, a, b):
setbits = self.countBits(b)
ans = 0
for i in range(31, -1, -1):
bit = a & 1 << i
if bit:
ans = ans | 1 << i
setbits -= 1
if setbits == 0:
return ans
i = 0
while setbits:
bit = a & 1 << i
if bit == 0:
ans = ans | 1 << i
setbits -= 1
i += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
res = a
a1 = a
b1 = b
n = 0
m = 0
length = 0
while a1 > 0:
n += a1 % 2
length += 1
a1 //= 2
while b1 > 0:
m += b1 % 2
b1 //= 2
if n == m:
res = a
elif n > m:
a1 = a
k = n - m
i = 0
while k > 0:
i += 1
if a1 % 2 == 1:
k -= 1
a1 //= 2
for j in range(i):
a1 *= 2
res = a1
elif length <= m:
res = 2**m - 1
else:
a1 = a
k = m - n
count = 0
while k > 0:
if a1 % 2 == 0:
k -= 1
res += 2**count
a1 //= 2
count += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
a_bin = str(bin(a))
b_bin = str(bin(b))
a_bin = a_bin[2:]
b_bin = b_bin[2:]
cnt = 0
cnt1 = 0
for i in range(len(b_bin)):
if b_bin[i] == "1":
cnt = cnt + 1
for i in range(len(a_bin)):
if a_bin[i] == "1":
cnt1 = cnt1 + 1
if cnt > len(a_bin):
dif = cnt - len(a_bin)
temp = "0" * dif
a_bin = temp + a_bin
temp = a_bin.replace("1", "x", cnt)
t1 = temp.replace("1", "0")
res = t1.replace("x", "1")
if cnt1 < cnt:
d = cnt - cnt1
rev = res[::-1]
t2 = rev.replace("0", "1", d)
res = t2[::-1]
return int(res, base=2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
a1 = bin(a)[2:]
b1 = bin(b)[2:]
sb = 0
for i in b1:
if i == "1":
sb += 1
out = ""
f = 0
n = len(a1)
if sb >= n:
ans = 2**sb - 1
return ans
for i in range(n):
k = "0"
if sb == n - i:
f = 1
if (f == 1 or a1[i] == "1") and sb > 0:
k = "1"
sb -= 1
out += k
p = 1
ans = 0
for i in range(n - 1, -1, -1):
ans += p * int(out[i])
p = p * 2
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR STRING VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | def countSetBits(num):
count = 0
while num > 0:
if num & 1 > 0:
count += 1
num = num >> 1
return count
def countNoOfBits(num):
count = 0
while num > 0:
num = num >> 1
count += 1
return count
class Solution:
def minVal(self, a, b):
setA = countSetBits(a)
setB = countSetBits(b)
if setA == setB:
ans = a
else:
new = a
diff = abs(setA - setB)
val = diff
count = 0
if setA > setB:
while val > 0:
if new & 1 != 0:
val -= 1
count += 1
new = new >> 1
for i in range(count):
new = new << 1
ans = new
else:
while val > 0:
if new & 1 == 0:
val -= 1
count += 1
new = new >> 1
for i in range(count):
new = new << 1
new += 1
ans = new
return ans | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
sa, sb = 0, 0
na, nb = a, b
while na != 0:
sa += 1
na &= na - 1
while nb != 0:
sb += 1
nb &= nb - 1
na, nb = a, b
if sa == sb:
return a
elif sa > sb:
for _ in range(sa - sb):
na &= na - 1
return na
else:
for _ in range(sb - sa):
na |= na + 1
return na | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
a_setbits = bin(a)[2:].count("1")
b_setbits = bin(b)[2:].count("1")
diff = abs(a_setbits - b_setbits)
if a_setbits == b_setbits:
return a
elif a_setbits > b_setbits:
while diff:
a = a & a - 1
diff -= 1
else:
while diff:
a = a | a + 1
diff -= 1
return a | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def count(self, a):
cnt = 0
while a:
a = a & a - 1
cnt += 1
return cnt
def fun(self, a, temp):
cnt = 0
i = 1
while temp > 0:
cnt += 1
if a & i > 0:
temp -= 1
i = i << 1
a = a >> cnt
return a * 2**cnt
def minVal(self, a, b):
a1 = self.count(a)
b1 = self.count(b)
temp = a1 - b1
if temp == 0:
return a
if temp < 0:
i = 1
res = 0
temp = -1 * temp
while temp > 0:
if i & a == 0:
res = res | i
temp -= 1
i = i << 1
a = a | res
return a
a = self.fun(a, temp)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
if b == 0:
return 0
setbit = 0
while b:
if b % 2:
setbit += 1
b = b // 2
flag = False
ans = 0
for i in range(31, -1, -1):
if not flag:
if setbit == i + 1:
ans += 2**setbit - 1
setbit = 0
break
elif 1 << i & a:
ans += 1 << i
setbit -= 1
flag = True
elif flag:
if setbit == i + 1:
ans += 2**setbit - 1
setbit = 0
break
elif 1 << i & a and setbit > 0:
ans += 1 << i
setbit -= 1
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
ca, cb = 0, 0
ba = [(0) for i in range(31)]
bb = [(0) for i in range(31)]
ans = [(0) for i in range(31)]
for i in range(31):
if b & 1 << i:
bb[i] = 1
cb += 1
if a & 1 << i:
ba[i] = 1
ca += 1
if ca == cb:
return a
else:
temp = cb
for i in range(30, -1, -1):
if ba[i] == 1 and temp > 0:
ans[i] = 1
temp -= 1
else:
ans[i] = 0
for i in range(31):
if temp == 0:
break
if ans[i] == 0:
ans[i] = 1
temp -= 1
total = 0
for i in range(31):
if ans[i] == 1:
total += 1 << i
return total | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def bitCount(self, a):
c = 0
for i in range(0, 34):
if a & 1 << i:
c += 1
return c
def minVal(self, a, b):
s = 0
x = self.bitCount(b)
for i in range(33, -1, -1):
if a & 1 << i and x != 0:
s = s + (1 << i)
x -= 1
for i in range(0, 34):
if a & 1 << i == 0 and x != 0:
s = s + (1 << i)
x -= 1
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
cb = 0
for i in range(32):
if 1 << i & b:
cb += 1
if cb == 0:
return 0
x = 0
for i in range(31, -1, -1):
if 1 << i & a:
x ^= 1 << i
cb -= 1
if cb == 0:
return x
for i in range(32):
if x & 1 << i == 0:
x ^= 1 << i
cb -= 1
if cb == 0:
return x | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def ones(self, n):
counter = 0
while n > 0:
counter += n % 2
n = n >> 1
return counter
def minVal(self, a, b):
o = self.ones(b)
bina = format(a, "b")
binb = format(b, "b")
binf = format(a, "b").zfill(max(len(binb), len(bina)))
res = ""
for i in range(len(binf)):
if o > 0 and binf[i] == "1":
res += "1"
o -= 1
elif o > 0 and len(binf) - i <= o:
res += "1"
o -= 1
else:
res += "0"
return int(res, 2) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR STRING VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR STRING VAR NUMBER VAR STRING RETURN FUNC_CALL VAR VAR NUMBER |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
c = 0
s1 = bin(a)
s2 = bin(b)
s1 = s1[2:]
s2 = s2[2:]
c = 0
for i in s2:
if i == "1":
c += 1
ans = 0
for i in range(31, -1, -1):
if i == c - 1:
return ans + (1 << i + 1) - 1
if a & 1 << i and c:
ans += 1 << i
c -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
setb = bin(b)[2:].count("1")
seta = bin(a)[2:].count("1")
ans = 0
for i in range(0, 32):
mask = 1 << i
set = a & mask
if set == 0 and setb > seta:
ans |= mask
setb -= 1
elif set and seta > setb:
seta -= 1
else:
ans |= set
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
b1 = bin(a)
b2 = bin(b)
count2 = b2.count("1")
res = 0
x = b1[2:]
n = len(x)
count1 = x.count("1")
if count2 <= count1:
for i in range(n):
if count2 == 0:
break
if x[i] == "1":
res += 2 ** (n - i - 1)
count2 -= 1
else:
new = [0] * n
for i in range(n):
if x[i] == "1":
new[i] = 1
count2 -= 1
if count2 == 0:
break
if count2 > 0:
for i in range(len(new) - 1, -1, -1):
if new[i] == 0:
new[i] = 1
count2 -= 1
if count2 == 0:
break
res = 0
for i in range(len(new)):
if new[i] == 1:
res += 2 ** (len(new) - i - 1)
j = len(new)
while count2 > 0:
res += 2**j
j += 1
count2 -= 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
x = 0
c = 0
k = 2**32
while k != 0:
if b & k == k:
c += 1
k = k >> 1
k = 2**32
while k != 0:
if a & k == k and c != 0:
x = x + k
c -= 1
k = k >> 1
if c != 0:
k = 1
while c != 0:
if x & k != k:
x = x + k
c -= 1
k = k << 1
return x | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)_{2}= 111
(12)_{2}= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.
Expected Time Complexity : O(log N)
Expected Auxiliary Space : O(1)
Constraints :
0 <= A, B <= 10^{9} | class Solution:
def minVal(self, a, b):
a = bin(a)[2:].zfill(32)
b = bin(b)[2:].zfill(32)
x = ["0" for i in range(32)]
cb = b.count("1")
i = 0
while cb and i < 32:
while i < 32 and a[i] == "0":
i += 1
if i == 32:
break
x[i] = "1"
i += 1
cb -= 1
i = 31
while cb:
while i >= 0 and x[i] == "1":
i -= 1
x[i] = "1"
i -= 1
cb -= 1
return int("".join(x), 2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR WHILE VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
str_set = set()
for i in range(len(s) - k + 1):
str_set.add(str(s[i : i + k]))
for i in range(2**k):
formatter = "{0:0%sb}" % str(k)
if formatter.format(i) not in str_set:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
if k >= len(s):
return False
d = set()
num = 0
for i in range(k):
m = ord(s[i]) - ord("0")
num = num * 2 + m
d.add(num)
mask = (1 << k - 1) - 1
for i in range(k, len(s)):
num &= mask
m = ord(s[i]) - ord("0")
num = num * 2 + m
d.add(num)
return len(d) >= 1 << k | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
def binary_code_of_length_k(k):
if k == 1:
return ["0", "1"]
t = binary_code_of_length_k(k - 1)
return [("0" + i) for i in t] + [("1" + i) for i in t]
k_string = binary_code_of_length_k(k)
for i in k_string:
if s.find(i) == -1:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
if len(s) < k:
return False
substring_set = set()
need = 1 << k
for start in range(len(s) - k + 1):
ss = s[start : start + k]
if ss not in substring_set:
substring_set.add(ss)
need -= 1
if need == 0:
return True
return False | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
need = 2**k
_new = set()
for i in range(len(s) - k + 1):
var = s[i : i + k]
if var not in _new:
_new.add(var)
need -= 1
if need == 0:
return True
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
if len(s) < 2**k + k - 1:
return False
target = 2**k
seen = set()
cur_len = 0
for end in range(k, len(s) + 1):
chunk = s[end - k : end]
if chunk not in seen:
cur_len += 1
seen.add(chunk)
if cur_len == target:
return True
return False | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
nums = set()
for i in range(0, len(s) - k + 1):
nums.add(s[i : i + k])
if len(nums) == 2**k:
return True
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
for i in range(2**k):
ss = bin(i)[2:].zfill(k)
if ss not in s:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
st = set()
for i in range(k, len(s) + 1):
st.add(s[i - k : i])
if len(st) == 1 << k:
break
return len(st) == 1 << k | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
l = len(s)
r = set()
for i in range(0, l - k + 1):
r.add(s[i : i + k])
if len(r) == 2**k:
return True
for i in product(["0", "1"], repeat=k):
_ = "".join(i)
if _ not in r:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR LIST STRING STRING VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | from itertools import product
class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
p = product("01", repeat=k)
for sub in p:
if s.find("".join(sub)) == -1:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
codes = set(format(i, f"0{k}b") for i in range(2**k))
found = [(False) for i in range(2**k)]
for i in range(len(s) - k + 1):
if s[i : i + k] in codes:
found[int(s[i : i + k], base=2)] = True
return all(found) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
check = []
for kk in range(2**k):
t = bin(kk)[2:]
check.append("0" * (k - len(t)) + t)
ss = set()
for i in range(k, len(s) + 1):
ss.add(s[i - k : i])
for t in check:
if t not in ss:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
def Hash_Function(b):
num = list(b)
num.reverse()
total = 0
increment = 1
for item in num:
if item == "1":
total = total + increment
increment = increment * 2
else:
increment = increment * 2
continue
return total
start = 0
end = k
buckets = []
buckets = set(buckets)
while end <= len(s):
num = Hash_Function(s[start:end])
buckets.add(num)
start += 1
end += 1
if len(buckets) == 2**k:
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
arr_2 = ["0", "1"]
m = k - 1
while m != 0:
arr_2 = [("0" + i) for i in arr_2] + [("1" + i) for i in arr_2]
m -= 1
for i in range(2**k):
if arr_2[i] in s:
continue
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | def binary_strings(k):
res = []
stack = [""]
while stack:
s = stack.pop()
if len(s) == k:
res.append(s)
else:
stack.append(s + "0")
stack.append(s + "1")
return res
class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
seen = set()
for i in range(0, len(s) - k + 1):
seen.add(s[i : i + k])
for bs in binary_strings(k):
if bs not in seen:
return False
return True | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
bins = {}
numbers = {}
st = s
print(k)
def bin2dec(a):
val = 0
for i in range(len(a)):
if a[i] == "1":
val += pow(2, len(a) - 1 - i)
return val
for i in range(len(st) - k + 1):
if st[i : i + k] in bins:
continue
else:
bins[st[i : i + k]] = 1
for i in bins:
numbers[bin2dec(i)] = 1
c = 0
for i in range(pow(2, k)):
if i in numbers:
c += 1
print((c, k))
if c == pow(2, k):
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
for i in range(2**k):
bin_value = (
bin(i)[2:]
if len(bin(i)[2:]) == k
else "0" * (k - len(bin(i)[2:])) + bin(i)[2]
)
if bin_value not in s:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | from itertools import permutations
class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
from itertools import permutations
c = dict()
count = 0
for i in range(0, len(s) - k + 1):
out = ""
for j in range(i, i + k):
out = out + s[j]
if out not in c:
c[out] = True
count = count + 1
if count == pow(2, k):
return True
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
arr = list(s)
arr = [(ord(el) - 48) for el in arr]
length = 0
n = len(s)
curr = 0
h = {}
for i in range(n - 1, -1, -1):
length += 1
if length > k:
length -= 1
curr = curr >> 1
if arr[i] == 1:
curr = 1 << length - 1 | curr
if length == k:
h[curr] = True
return len(h) == pow(2, k) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
codes = {format(i, f"0{k}b"): (False) for i in range(2**k)}
for i in range(len(s) - k + 1):
ss = s[i : i + k]
if ss in codes:
codes[ss] = True
return all(codes.values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a binary string s and an integer k.
Return True if every binary code of length k is a substring of s. Otherwise, return False.
Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "00110", k = 2
Output: true
Example 3:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 4:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
Example 5:
Input: s = "0000000001011100", k = 4
Output: false
Constraints:
1 <= s.length <= 5 * 10^5
s consists of 0's and 1's only.
1 <= k <= 20 | class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
result = set()
for i in range(len(s) - k + 1):
val = s[i : i + k]
result.add(val)
if len(result) >= 2**k:
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | n = int(input())
relMatrix = []
binArray = []
suggConn = 0
for i in range(0, n):
binValues = ["{0:04b}".format(int(x)) for x in input()]
relMatrix.append(binValues)
binArray.append(int("".join(binValues), 2))
for i in range(0, n):
for j in range(i, n):
if i != j and int(relMatrix[i][j]) == 0:
if binArray[i] & binArray[j] != 0:
suggConn += 2
print(suggConn) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | n = int(input())
array = [input() for i in range(n)]
frndship = [int(x, 2) for x in array]
count = 0
limit = 0
for i in range(n):
for j in range(limit):
if array[i][j] == "0" and i != j and frndship[i] & frndship[j]:
count += 2
limit += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | import sys
i = int(sys.stdin.readline())
arr = []
comp = [0] * i
count = 0
for _ in range(i):
p = input()
arr.append(list(p))
comp[_] = int(p, 2)
for t in range(i):
for a in range(t):
if arr[t][a] == "0":
if comp[t] & comp[a]:
count += 2
print(count) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | def check(a, b, n):
if a & b:
return True
return False
n = int(input())
l = []
ll = []
for _ in range(n):
x = input()
l.append(list(x))
ll.append(int(x, 2))
req = 0
for i in range(n):
for j in range(1 + i, n):
if l[i][j] == "0" and check(ll[i], ll[j], n):
req += 1
print(2 * req) | FUNC_DEF IF BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | n = int(input())
l = []
l2 = []
c = 0
m = []
for i in range(n):
f = input()
sub = set()
l2.append(f)
m.append(int(f, 2))
for i in range(n - 1):
for j in range(i + 1, n):
if l2[i][j] == "0" and m[i] & m[j] != 0:
c += 2
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | n = int(input())
a = [[(0) for _ in range(n)] for _ in range(n)]
bi = [(0) for _ in range(n)]
for i in range(n):
k = input()
a[i] = k
bi[i] = int(k, 2)
count = 0
for i in range(n):
for j in range(i + 1, n):
if a[i][j] == "0":
if bi[i] & bi[j]:
count += 1
print(count * 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING IF BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | a = []
b = []
res = 0
n = int(input())
for i in range(n):
s = input()
a.append(list(s))
b.append(int(s, 2))
for i in range(n):
for j in range(i + 1, n):
if a[i][j] == "0" and b[i] & b[j] != 0:
res += 1
print(res * 2) | ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | t = int(input())
l = []
l1 = []
for i in range(t):
s = input()
l.append(s)
l1.append(int(s, 2))
j = 0
k = 0
cout = 0
while j < t - 1:
k = j + 1
while k < t:
if l[j][k] == "0" and l1[j] & l1[k] >= 1:
cout = cout + 2
k = k + 1
j = j + 1
print(cout) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR STRING BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | n = int(input())
arr = []
a = []
for a0 in range(n):
arr.append(input())
for i in arr:
a.append(int(i, base=2))
ans = 0
for i in range(len(a) - 1):
for j in range(i + 1, len(a)):
if arr[i][j] == "0":
if a[i] & a[j] != 0:
ans += 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER 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 IF VAR VAR VAR STRING IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | a = int(input())
c = []
d = []
for i in range(a):
now = input()
c.append(now)
d.append(int(now, 2))
i = 0
for k in range(a):
for j in range(k + 1, a):
if c[k][j] != "1":
if int(d[k]) & int(d[j]):
i = i + 2
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | N = int(input())
matrix = []
is_friend = []
for i in range(0, N):
row = []
line = input()
is_friend.append(line)
matrix.append(int(line, 2))
count = 0
for i in range(0, N):
for j in range(i + 1, N):
if is_friend[i][j] != "1" and matrix[i] & matrix[j] > 0:
count += 2
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | n = int(input())
B = []
for _ in range(n):
x = input()
B.append(int(x, 2))
count = 0
J = 1 << n
for i in range(n):
j = 1
p = n - 1
while j != J:
if p == i:
j <<= 1
p -= 1
continue
if not B[i] & j:
if B[i] & B[p]:
count += 1
j <<= 1
p -= 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | import sys
n = int(sys.stdin.readline())
adj_mat = []
ints = []
for ln_i in range(n):
adj_mat.append(sys.stdin.readline())
ints.append(int(adj_mat[ln_i], 2))
requests = 0
for i in range(n):
for j in range(i):
if adj_mat[i][j] == "0":
if ints[i] & ints[j]:
requests += 2
sys.stdout.write(str(requests) + "\n") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Read problems statements in Mandarin Chinese and Russian as well.
After IOI Ilya decided to make a business. He found a social network called "TheScorpyBook.com". It currently has N registered users. As in any social network two users can be friends. Ilya wants the world to be as connected as possible, so he wants to suggest friendship to some pairs of users. He will suggest user u to have a friendship with user v if they are not friends yet and there is a user w who is friends of both of them. Note that u, v and w are different users. Ilya is too busy with IPO these days, so he asks you to count how many friendship suggestions he has to send over his social network.
------ Input ------
The first line contains an integer number N — the number of users in the network. Next N lines contain N characters each denoting friendship relations. j^{th} character if the i^{th} lines equals one, if users i and j are friends and equals to zero otherwise. This relation is symmetric, i.e. if user a is friend of b then b is also a friend of a.
------ Output ------
Output a single integer — number of friendship suggestions Ilya has to send.
------ Constraints ------
$1 ≤ N ≤ 2000$
------ Example ------
Input:
4
0111
1000
1000
1000
Output:
6
------ Explanation ------
Each of users [2, 3, 4] should receive two friendship suggestions, while user 1 does not need any, since he already has all other users in his friend-list. | total = 0
friends = int(input())
adjmat = [0] * friends
binreps = [0] * friends
for line in range(friends):
sa = input()
adjmat[line] = sa
binreps[line] = int(sa, 2)
for i in range(friends - 1):
for j in range(i + 1, friends):
if adjmat[i][j] != "1":
if binreps[i] & binreps[j] != 0:
total += 2
print(total) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
def check(res, arr):
count = 0
for i in range(len(arr)):
if arr[i] & res == res:
count += 1
return count
ans = 0
for i in range(16, -1, -1):
res = ans | 1 << i
x = check(res, arr)
if x > 1:
ans = res
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
ans = 0
for i in range(21, -1, -1):
k = 1 << i
k = k | ans
cnt = 0
for i in range(N):
if arr[i] & k == k:
cnt += 1
if cnt >= 2:
ans = k
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
res = 0
for setbit in range(31, -1, -1):
x = res | 1 << setbit
c = 0
for i in arr:
if x & i == x:
c += 1
if c >= 2:
res = res | 1 << setbit
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
lis = []
for i in range(31, -1, -1):
bitn = 1 << i
l = []
for j in arr:
if j & bitn:
l.append(j)
if len(l) >= 2:
lis.append(i)
arr = l
res = 0
for i in lis:
res += 2**i
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def countAND(self, arr, N, X):
count = 0
for i in range(N):
if arr[i] & X == X:
count += 1
return count
def maxAND(self, arr, N):
res = 0
j = 16
while j + 1 > 0:
x = 1 << j
x += res
countAv = self.countAND(arr, N, x)
if countAv >= 2:
res = x
j -= 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, g, n):
ans = 0
st = len(bin(max(g))[2:])
for i in range(st, -1, -1):
k = 0
for x in g:
if x & 1 << i != 0 and x & ans == ans:
k += 1
if k > 1:
ans += 1 << i
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
res = 0
count = 0
for i in range(32):
k = res | 1 << 31 - i
count = self.countKSetBit(arr, k)
if count >= 2:
res = k
return res
def countKSetBit(self, arr, k):
c = 0
for i in arr:
if i & k == k:
c += 1
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
m = 1
res = 0
i = 31
while i >= 0:
res = res | m << i
count = 0
for ele in arr:
if res & ele == res:
count += 1
if count < 2:
res = res & res - 1
i -= 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def checkBit(self, pattern, arr, N):
c = 0
for i in range(N):
if pattern & arr[i] == pattern:
c += 1
return c
def maxAND(self, arr, N):
res = 0
arr.sort()
k = int(math.log(arr[N - 1], 2))
for bit in range(k, -1, -1):
c = self.checkBit(res | 1 << bit, arr, N)
if c >= 2:
res |= 1 << bit
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
def pat(pattern, arr, n):
count = 0
for i in arr:
if i & pattern == pattern:
count = count + 1
return count
res = 0
for i in range(31, -1, -1):
out = pat(res | 1 << i, arr, N)
if out >= 2:
res = res | 1 << i
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
c = 1
res = 0
q = 0
for i in range(16, -1, -1):
c = 1 << i
d = 0
for j in arr:
if (res | c) & j == res | c:
d = d + 1
if d >= 2:
res = res | c
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def checkIfTwoExists(self, arr, pattern):
count = 0
for num in arr:
if num & pattern == pattern:
count += 1
return count >= 2
def maxAND(self, arr, N):
ans = 0
for place in range(16, -1, -1):
pattern = ans | 1 << place
if self.checkIfTwoExists(arr, pattern):
ans = pattern
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
maxand = 0
maxbit = max(arr)
maxbit = maxbit.bit_length()
for bit in range(maxbit, -1, -1):
count = 0
for n in arr:
pattern = maxand | 1 << bit
if n & pattern == pattern:
count += 1
if 2 <= count:
break
if 2 <= count:
maxand |= 1 << bit
return maxand | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF NUMBER VAR IF NUMBER VAR VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | def checkBit(pattern, arr, n):
count = 0
for i in range(0, n):
if pattern & arr[i] == pattern:
count = count + 1
return count
def maxAN(arr, n):
res = 0
for bit in range(18, -1, -1):
count = checkBit(res | 1 << bit, arr, n)
if count >= 2:
res = res | 1 << bit
return res
class Solution:
def maxAND(self, arr, N):
return maxAN(arr, N) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
if N <= 1:
return 0
m = arr[0]
for x in arr[1:]:
m = max(m, x)
numBits = 0
while m > 0:
numBits += 1
m //= 2
mask = 0
while numBits > 0:
mask |= 1 << numBits - 1
cnt = 0
for x in arr:
if mask & x == mask:
cnt += 1
if cnt < 2:
mask &= ~(1 << numBits - 1)
numBits -= 1
res = ~0
for x in arr:
if mask & x == mask:
res &= x
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
m = max(arr)
while m & m - 1 > 0:
m = m & m - 1
val = 0
j = m
while m > 0:
count = 0
for i in arr:
if j & i == j:
count += 1
if count > 1:
val = val | m
m = m >> 1
j = val | m
return val | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
max1 = 0
for i in range(31, -1, -1):
count = 0
z = max1 | 1 << i
for j in arr:
if j & z == z:
count += 1
if count >= 2:
max1 |= 1 << i
return max1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
def check(pattern, arr, n):
count = 0
for i in range(n):
if arr[i] & pattern == pattern:
count += 1
return count
res = 0
m = max(arr)
key = 0
for i in range(31, -1, -1):
if m & 1 << i:
key = i
break
for i in range(key + 1, -1, -1):
count = check(res | 1 << i, arr, N)
if count >= 2:
res = res | 1 << i
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
val = 0
for i in range(16, -1, -1):
count = 0
for j in arr:
if j & val >= val and j & 1 << i:
count += 1
if count >= 2:
val += 1 << i
return val | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, n):
res = 0
for i in range(31, -1, -1):
if self.checkBit(res | 1 << i, arr, n):
res |= 1 << i
return res
def checkBit(self, pattern, arr, n):
count = 0
for i in range(n):
if pattern & arr[i] == pattern:
count += 1
if count >= 2:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
res = 0
for bits in range(16, -1, -1):
count = self.checkBits(res | 1 << bits, arr, N)
if count >= 2:
res |= 1 << bits
return res
def checkBits(self, pattern, arr, N):
count = 0
for i in arr:
if i & pattern == pattern:
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
if N == 1:
return 0
ans = ""
newArr = arr.copy()
for i in range(18):
flag = False
cnt = 0
arr = newArr.copy()
for ii in range(len(arr)):
if arr[ii] & 1 << 18 - i - 1 == 1 << 18 - i - 1:
cnt += 1
if cnt > 1:
newArr = []
flag = True
if flag == False:
ans += "0"
else:
ans += "1"
if flag:
for ii in range(len(arr)):
if arr[ii] & 1 << 18 - i - 1 == 1 << 18 - i - 1:
newArr.append(arr[ii])
continue
return int(ans, 2) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
for i in range(31, -1, -1):
l = []
for j in arr:
if j & 1 << i != 0:
l.append(j)
if len(l) > 1:
arr = l
if len(arr) > 1:
ma = arr[0]
for i in arr:
ma = ma & i
return ma
else:
return 0 | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR RETURN NUMBER |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
numbits = 0
mx = 0
for i in range(N):
mx = max(mx, arr[i])
while mx > 0:
numbits += 1
mx >>= 1
mask = 0
for i in range(numbits - 1, -1, -1):
mask = mask | 1 << i
count = 0
for j in range(N):
if arr[j] & mask == mask:
count += 1
if count <= 1:
mask = mask & mask - 1
return mask | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, arr, N):
ans = 0
for i in range(16, -1, -1):
power = 1 << i
count = 0
for num in arr:
if num & ans == ans and num & power:
count += 1
if count >= 2:
ans += power
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR |
Given an array arr[] of N positive elements. The task is to find the Maximum AND Value generated by any pair(arr_{i, }arr_{j}) from the array such that i != j.
Note: AND is bitwise '&' operator.
Example 1:
Input:
N = 4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.
Example 2:
Input:
N = 4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array.
Expected Time Complexity: O(N * log M), where M is the maximum element of the array.
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{5}
1 <= arr[i] <= 10^{5} | class Solution:
def maxAND(self, a, N):
res = 0
x = max(a)
h = math.floor(math.log2(x)) + 1
for i in range(h - 1, -1, -1):
pattern = res | 1 << i
count = 0
for j in range(len(a)):
if a[j] & pattern == pattern:
count += 1
if count > 1:
res = pattern
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.