description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer ― the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | t = int(input())
while t != 0:
a = input()
b = input()
t -= 1
if int(b, 2) == 0:
print(0)
elif int(a, 2) == 0 or int(a, 2) & int(b, 2) == 0:
print(1)
else:
a = "0" * (len(b) - len(a)) + a
b = "0" * (len(a) - len(b)) + b
count = 0
c2 = 0
flag = 0
for i in range(len(b) - 1, -1, -1):
ai = int(a[i])
bi = int(b[i])
if flag == 1:
if ai ^ bi == 1:
count += 1
else:
flag = 0
count = 0
if ai & bi == 1:
flag = 1
c2 = max(c2, count)
print(c2 + 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer ― the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = input()
b = input()
bog = b
an = len(a)
bn = len(b)
ans = -1
p = 0
if an > bn:
b = b.zfill(an - bn + bn)
p = an
elif bn > an:
a = a.zfill(bn - an + an)
p = bn
else:
p = an
al = list(map(int, a))
bl = list(map(int, b))
res = [7]
if bog == "0":
ans = 0
print(ans)
continue
else:
for i in range(p):
if al[i] == 0 and bl[i] == 0:
res.append(7)
elif al[i] == 0 and bl[i] == 1 or al[i] == 1 and bl[i] == 0:
res.append(8)
elif al[i] == 1 and bl[i] == 1:
res.append(9)
if res.count(9) == 0:
ans = 1
else:
po = 0
ma = 1
flag = 1
for i in range(p + 1):
if res[i] == 7:
po = i
elif res[i] == 9:
if ma < abs(i - po + 1):
ma = abs(i - po + 1)
po = i
ans = ma
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer ― the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = input()
b = input()
x = int(a, 2)
y = int(b, 2)
if y == 0:
print(0)
elif x == 0 and y != 0:
print(1)
else:
ans = 0
count = 1
f = 0
if len(a) < len(b):
d = len(b) - len(a)
a = a.zfill(d + 1 + len(a))
b = b.zfill(1 + len(b))
n = len(a)
for i in range(n - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if f == 1:
ans = max(ans, count)
count = 1
else:
f = 1
if a[i] == "0" and b[i] == "0":
if f == 1:
ans = max(ans, count)
count = 1
f = 0
if f == 1:
if a[i] != b[i]:
count += 1
ans = max(ans, count)
elif len(a) > len(b):
d = len(a) - len(b)
a = a.zfill(1 + len(a))
b = b.zfill(d + 1 + len(b))
n = len(a)
for i in range(n - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if f == 1:
ans = max(ans, count)
count = 1
else:
f = 1
if a[i] == "0" and b[i] == "0":
if f == 1:
ans = max(ans, count)
count = 1
f = 0
if f == 1:
if a[i] != b[i]:
count += 1
ans = max(ans, count)
elif len(a) == len(b):
a = a.zfill(1 + len(a))
b = b.zfill(1 + len(b))
n = len(a)
for i in range(n - 1, -1, -1):
if a[i] == "1" and b[i] == "1":
if f == 1:
ans = max(ans, count)
count = 1
else:
f = 1
if a[i] == "0" and b[i] == "0":
if f == 1:
ans = max(ans, count)
count = 1
f = 0
if f == 1:
if a[i] != b[i]:
count += 1
ans = max(ans, count)
print(ans + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Recently, Chef studied the binary numeral system and noticed that it is extremely simple to perform bitwise operations like AND, XOR or bit shift on non-negative integers, while it is much more complicated to perform arithmetic operations (e.g. addition, multiplication or division).
After playing with binary operations for a while, Chef invented an interesting algorithm for addition of two non-negative integers $A$ and $B$:
function add(A, B):
while B is greater than 0:
U = A XOR B
V = A AND B
A = U
B = V * 2
return A
Now Chef is wondering how fast this algorithm is. Given the initial values of $A$ and $B$ (in binary representation), he needs you to help him compute the number of times the while-loop of the algorithm is repeated.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single string $A$.
- The second line contains a single string $B$.
-----Output-----
For each test case, print a single line containing one integer ― the number of iterations the algorithm will perform during addition of the given numbers $A$ and $B$.
-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le |A|, |B| \le 10^5$
- $A$ and $B$ contain only characters '0' and '1'
- the sum of $|A| + |B|$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $|A|, |B| \le 30$
Subtask #2 (30 points):
- $|A|, |B| \le 500$
- the sum of $|A| + |B|$ over all test cases does not exceed $10^5$
Subtask #3 (50 points): original constraints
-----Example Input-----
3
100010
0
0
100010
11100
1010
-----Example Output-----
0
1
3
-----Explanation-----
Example case 1: The initial value of $B$ is $0$, so while-loop is not performed at all.
Example case 2: The initial values of $A$ and $B$ are $0_2 = 0$ and $100010_2 = 34$ respectively. When the while-loop is performed for the first time, we have:
- $U = 34$
- $V = 0$
- $A$ changes to $34$
- $B$ changes to $2 \cdot 0 = 0$
The while-loop terminates immediately afterwards, so it is executed only once.
Example case 3: The initial values of $A$ and $B$ are $11100_2 = 28$ and $1010_2 = 10$ respectively. After the first iteration, their values change to $22$ and $16$ respectively. After the second iteration, they change to $6$ and $32$, and finally, after the third iteration, to $38$ and $0$. | for _ in range(int(input())):
a = input().strip()
b = input().strip()
if len(a) < len(b):
a = "0" * (len(b) - len(a)) + a
a = list(a)
b_ones = []
for i in range(len(b)):
if b[i] == "1":
b_ones.append(len(b) - i)
iter = 0
while len(b_ones) != 0:
new_bones = []
for bit in b_ones:
aind = len(a) - bit - iter
if aind >= 0:
if a[aind] == "1":
a[aind] = "0", new_bones.append(bit)
else:
a[aind] = "1"
b_ones = new_bones
iter += 1
print(iter) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
bit = "{:b}".format(n)
consbit = 0
nbit = ""
for i in bit:
if i == "1" and consbit == 2:
nbit += "0"
consbit = 0
elif i == "1":
consbit += 1
nbit += "1"
else:
nbit += "0"
consbit = 0
return int(nbit, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
if n < 7:
return n
a = list(bin(n))
a = a[2:]
for i in range(len(a) - 2):
if a[i : i + 3].count("1") == 3:
a[i + 2] = "0"
a = "".join(a)
ans = int(a, 2)
return ans | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
num = bin(n)[2:]
i = 0
c = 1
pos = len(num) - 1
while pos >= 0:
if num[pos] == "1":
i += 1
if num[pos] == "0":
while i >= 3:
num = num[: pos + 3] + "0" + num[pos + 4 :]
pos += 3
i -= 3
else:
i = 0
pos -= 1
if i >= 3:
while i >= 3:
num = num[: pos + 3] + "0" + num[pos + 4 :]
pos += 3
i -= 3
return int(num, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
bs = bin(n)[2:]
tl = [i for i in bs]
i = 0
while i < len(bs) - 1:
if bs[i] == "1":
c = 1
while c != 3:
i += 1
if i < len(bs) and bs[i] == "1":
c += 1
else:
i += 1
break
else:
tl[i] = "0"
i += 1
else:
i += 1
bs = "".join(tl)
return int(bs, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
s = bin(n)[2:]
count = 0
res = []
if len(s) < 3:
return n
else:
i = 0
while i < len(s):
if i < len(s) - 2 and s[i] == s[i + 1] == s[i + 2] == "1":
res.append("110")
i += 3
elif s[i] == "1":
res.append("1")
i += 1
else:
res.append("0")
i += 1
ans = "".join(res)
return int(ans, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
x = bin(n).replace("0b", "")
ans = ""
cnt = 0
for i in x:
if i == "1" and cnt == 2:
ans += "0"
cnt = 0
continue
elif i == "0":
ans += i
cnt = 0
continue
ans += i
cnt += 1
return int(ans, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR STRING VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
n = str(bin(n))
n = n[2:]
for i in range(len(n) - 2):
if n[i : i + 3] == "111":
n = n[:i] + "110" + n[i + 3 :]
return int(n, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
j = bin(n)[2:]
stri = str(j)
if "111" in stri:
stri = stri.replace("111", "110")
for i in range(n, 0, -1):
a = bin(i)[2:]
if str(a) == stri:
k = i
break
else:
k = n
return k | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
num = bin(n)
num = num[2:]
consec_1 = 0
max_num = 0
for i in range(len(num)):
if num[i] == "1":
consec_1 += 1
if consec_1 == 3:
consec_1 = 0
else:
max_num += 2 ** (len(num) - i - 1)
else:
consec_1 = 0
return max_num | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
s = ""
if n == 2:
return 2
z = bin(n)[2:]
arr = list(z)
for i in range(len(arr) - 2):
if arr[i] == arr[i + 1] == arr[i + 2] == "1":
arr[i + 2] = "0"
for i in arr:
s = s + i
return int(s, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
t = bin(n)[2:]
if "111" not in t:
return n
x = ""
x += t[0]
x += t[1]
for i in t[2:]:
if i == "1" and x[-1] == "1" and x[-2] == "1":
x += "0"
else:
x += i
return int(x, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF STRING VAR RETURN VAR ASSIGN VAR STRING VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER STRING VAR NUMBER STRING VAR STRING VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
if n < 7:
return n
binary = bin(n)[2:]
i = 2
while i < len(binary):
if binary[i - 2 : i + 1] == "111":
n -= 2 ** (len(binary) - i - 1)
i += 2
i += 1
return n | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
binary = bin(n)[2:].zfill(32)
j = 0
store = set()
for index, i in enumerate(binary):
if i == "0":
j = 0
else:
j += 1
if j % 3 == 0:
store.add(index)
newNum = []
for index, i in enumerate(binary):
if index in store:
newNum.append("0")
else:
newNum.append(i)
return int("".join(newNum), 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
bits = []
while n:
bits.append(n % 2)
n //= 2
bits.reverse()
bits.append(0)
i = 0
for j, bit in enumerate(bits):
if bit:
continue
if j - i >= 3:
for k in range(i + 2, j + 1, 3):
bits[k] = 0
i = j + 1
bits.pop()
ans = 0
for x in bits:
ans = ans * 2 + x
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
cnt = 0
for i in range(31, -1, -1):
if n & 1 << i:
cnt += 1
if cnt == 3:
n = n ^ 1 << i
cnt = 0
else:
cnt = 0
return n | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
if n > 6:
x = bin(n)
k = list(x)
k = k[2:]
c = 0
for i in range(len(k)):
if k[i] == "1":
c += 1
else:
c = 0
if c == 3:
k[i] = "0"
c = 0
s = "".join(k)
return int(s, 2)
else:
return n | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
i = 31
c = 0
while i >= 0:
if n & 1 << i:
c = c + 1
else:
c = 0
if c == 3:
n = n & ~(1 << i)
c = 0
i = i - 1
return n | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
bina = format(n, "b")
size = len(bina)
fin = ""
if size <= 2:
return n
fin = bina[0] + bina[1]
for i in range(2, size):
if bina[i] == "1":
if fin[i - 1] == "1" and fin[i - 2] == "1":
fin = fin + "0"
else:
fin = fin + "1"
else:
fin = fin + "0"
return int(fin, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
numB = bin(n)[2:]
count = 0
for i in range(len(numB)):
if int(numB[i]):
count += 1
if count == 3:
numB = numB[:i] + "0" + numB[i + 1 :]
count = 0
else:
count = 0
return int(numB, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
if not n:
return 0
def binary(n):
ans = []
while n:
mod = n % 2
ans.append(mod)
n //= 2
return ans[::-1]
setbit = 0
bi = binary(n)
res = ""
for i in bi:
if i and setbit == 2:
res += "0"
setbit = 0
continue
if i:
res += "1"
setbit += 1
else:
res += "0"
setbit = 0
return int(res, 2) | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
l = [1]
for i in range(0, 31):
l.append(l[i] * 2)
m = 32
c = 0
ans = 0
for i in range(m - 1, -1, -1):
if l[i] & n:
c += 1
elif c < 3:
c = 0
if c == 3:
c = 0
elif l[i] & n:
ans += l[i]
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
b = bin(n)[2:]
b = b.replace("111", "110")
res = int(b, 2)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
b = format(n, "b")
a = b
for i in range(len(b)):
if b[i : i + 3] == "111":
a = b[: i + 2] + "0" + b[i + 3 :]
b = a
y = int(b, 2)
return y | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
def fun(n):
s = bin(n).replace("0b", "")
for i in range(len(s) - 2):
if s[i] == s[i + 1] == s[i + 2] == "1":
s = s[: i + 2] + "0" + s[i + 3 :]
if "111" not in s:
return int(s, 2)
else:
return fun(k)
return fun(n) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER IF STRING VAR RETURN FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
i = 32
while i >= 2:
if n & 1 << i > 0 and n & 1 << i - 1 > 0 and n & 1 << i - 2 > 0:
n ^= 1 << i - 2
i -= 1
return n | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
bit_count, ans = 0, 0
i = 31
while i >= 0:
if bit_count <= 1 and n & 1 << i:
ans = ans | 1 << i
bit_count += 1
else:
bit_count = 0
i -= 1
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
a = bin(n)
b = list(a)
if len(b) < 5:
g = n
for i in range(0, len(b)):
if i == 0 and i == 1:
continue
if i == len(b) - 2:
break
if b[i] == "1" and b[i + 1] == "1" and b[i + 2] == "1":
b[i + 2] = "0"
c = "".join(b)
g = int(c, 2)
return g | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
binary = bin(n)[2:]
for i in range(2, len(binary)):
if binary[i] == "1" and binary[i - 1] == "1" and binary[i - 2] == "1":
binary = binary[:i] + "0" + binary[i + 1 :]
return int(binary, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
for i in range(27, -1, -1):
mask = 7 << i
if n & mask == mask:
bit = 1 << i
n ^= bit
return n | CLASS_DEF FUNC_DEF VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
if n < 6:
return n
binary = bin(n)[2:]
for i in range(len(binary) - 2):
if "111" == binary[i : i + 3]:
binary = list(binary)
binary[i + 2] = "0"
binary = "".join(binary)
return int(binary, 2) | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
for i in range(30, 1, -1):
mask1 = 1 << i
mask2 = 1 << i - 1
mask3 = 1 << i - 2
if mask1 & n and mask2 & n and mask3 & n:
n = n ^ mask3
return n | CLASS_DEF FUNC_DEF VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
n = bin(n)[2:]
n = list(n)
i = 0
count = 0
while i <= len(n) - 1:
if n[i] == "1" and count < 3:
count += 1
elif count > 0 and n[i] == "0":
count = 0
if count == 3:
count = 0
n[i] = "0"
i += 1
n = "".join(n)
return int(n, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
arr = [0] * 32
for i in range(31, -1, -1):
if n & 1 << 31 - i != 0:
arr[i] = 1
ans = 0
for i in range(30):
if arr[i] == 1:
ans += 1 << 31 - i
if arr[i + 1] == 1:
arr[i + 2] = 0
if arr[30] == 1:
ans += 2
if arr[31] == 1:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
n = bin(n)[2:]
i = 0
b = n
while i <= len(n):
if n[i : i + 3] == "111":
b = n[: i + 2] + "0" + n[i + 3 :]
n = b
i += 1
return int(n, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
num = bin(n)[2:]
output = ""
i = 0
while i < len(num):
if num[i : i + 3] == "111":
output += "110"
i += 3
else:
output += num[i]
i += 1
return int(output, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING VAR NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
b = bin(n)[2:]
c = b.count("111")
while c > 0:
b = b.replace("111", "110")
c -= 1
ans = 0
j = 0
for i in range(len(b) - 1, -1, -1):
ans += int(b[i]) * 2**j
j += 1
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
a = bin(n)[2:]
b = ""
c = 0
for i in a:
if i == "1":
c += 1
if c == 3:
c = 0
else:
c = 0
b += "1" if c else "0"
return int(b, 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR STRING STRING RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
s = bin(n)[2:]
s = [i for i in s]
ans = 0
ind = 0
while ind < len(s):
c = s[ind]
if c == "0":
ind += 1
continue
cnt = 0
while ind < len(s) and s[ind] == "1":
cnt += 1
if cnt == 3:
s[ind] = "0"
cnt = 0
ind += 1
return int("".join(s), 2) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
i, j, k = 0, 1, 2
n = list(bin(n)[2:])
while k < len(n):
if n[i] == "1" and n[j] == "1" and n[k] == "1":
n[k] = "0"
i += 1
j += 1
k += 1
return eval("0b" + "".join(n)) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING VAR VAR |
Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits.
Example 1:
Input:
n = 2
Output:
2
Explanation:
2's binary form is 10, no 3 consecutive set bits are here.
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output:
6
Explanation:
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set
bit to unset bit. Now, the number
becomes 6 that is .....00110. It satifies the
given condition. Hence, the maximum possible
value is 6.
Your Task: You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9} | class Solution:
def noConseBits(self, n: int) -> int:
nii = 40
result = [0] * nii
for ni in range(0, nii + 1):
if n >= 2 ** (nii - ni):
result[ni - 1] = 1
n = n - 2 ** (nii - ni)
ones_counter = 0
for ni in range(0, nii):
if result[ni] == 1:
ones_counter = ones_counter + 1
elif result[ni] == 0:
ones_counter = 0
if ones_counter == 3:
result[ni] = 0
ones_counter = 0
final = 0
for ni in range(0, nii):
if result[ni] == 1:
final = final + 2 ** (nii - ni - 1)
return final | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
c = 0
for i in A:
if i and not i & i - 1:
c += 1
return ((1 << c) - 1) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
mod = 10**9 + 7
count = 0
for i in A:
if i and not i & i - 1:
count += 1
return pow(2, count, mod) - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def solve(ob, n):
return n > 0 and n & n - 1 == 0
def numberOfSubsequences(ob, n, a):
c = 0
mod = 10**9 + 7
for i in range(n):
if ob.solve(a[i]):
c += 1
return pow(2, c, mod) - 1 | CLASS_DEF FUNC_DEF RETURN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def getPow(ob, n):
if n == 0:
return 1
mod = 1000000007
x = ob.getPow(n // 2) % mod
if n % 2 == 0:
return x * x % mod
return 2 * x * x % mod
def numberOfSubsequences(ob, N, A):
c = 0
for v in A:
if v == 1 or v != 0 and v & v - 1 == 0:
c += 1
return ob.getPow(c) - 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def is2pow(self, n):
return n and not n & n - 1
def numberOfSubsequences(self, N, A):
mod = 1000000007
count = 0
for i in A:
if self.is2pow(i):
count += 1
return pow(2, count, mod) - 1 | CLASS_DEF FUNC_DEF RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
c = 0
for i in range(N):
if A[i] & A[i] - 1 == 0:
c += 1
mod = 1000000000.0 + 7
ans = 1
for i in range(c):
ans = ans * 2 % mod
return int(ans - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def is_power(self, x):
return x and not x & x - 1
def numberOfSubsequences(self, N, A):
res = 0
for n in A:
res += 1 if self.is_power(n) else 0
ans = 2**res
return ans % (10**9 + 7) - 1 | CLASS_DEF FUNC_DEF RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
d = [(True) for i in A if not i & i - 1]
return (2 ** sum(d) - 1) % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
c = 0
for i in range(N):
val = A[i]
if val & val - 1 == 0:
c += 1
MOD = int(1000000000.0 + 7)
ans = 1
a = 2
while c:
if c & 1:
ans *= a
ans %= MOD
a = a * a
a %= MOD
c >>= 1
return ans - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
def helper(x):
return x and not x & x - 1
count = 0
for i in A:
if helper(i):
count += 1
return pow(2, count, 1000000007) - 1 | CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, n, a):
sub = []
for i in range(n):
if a[i] & a[i] - 1 == 0:
sub.append(a[i])
c = len(sub)
l = 2**c - 1
return l % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def power_two(ob, a):
return int(a / 2), a % 2
def numberOfSubsequences(ob, N, A):
Mod = 1000000000.0 + 7
l = 0
tot = 1
for i in range(N):
p = A[i]
mod = 0
while p != 1 and p % 2 == 0:
p, mod = ob.power_two(p)
if p == 1 and mod == 0:
l += 1
while l:
tot = tot * 2 % Mod
l = l - 1
return int((tot - 1) % Mod) | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
modulo = 10**9 + 7
def numberOfSubsequences(self, N, A):
power_2_count = 0
for i in range(N):
if A[i] & A[i] - 1 == 0:
power_2_count += 1
ans = 0
tmp_ans = 1
choose_i = 1
while power_2_count > 0:
tmp_ans = tmp_ans * power_2_count // choose_i
ans += tmp_ans
power_2_count -= 1
choose_i += 1
return ans % self.modulo | CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
count_2 = 0
for i in range(len(A)):
if A[i] & A[i] - 1 == 0:
count_2 += 1
ans = 2**count_2 - 1
return ans % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
M = 10**9 + 7
c = 0
def powerof2(n):
if n == 1:
return True
elif n % 2 != 0 or n == 0:
return False
return powerof2(n / 2)
for i in range(0, N):
if powerof2(A[i]):
c += 1
return (2**c - 1) % M | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
count = 0
for item in A:
count += bin(item).count("1") == 1
return (2**count - 1) % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, n, arr):
count = 0
mod = 10**9 + 7
for i, ele in enumerate(arr):
if ele and ele & ele - 1 == 0:
count += 1
return ((1 << count) - 1) % mod | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
c = 0
for i in range(N):
b = bin(A[i])[2:]
co = b.count("0")
if co == len(b) - 1 and b[0] == "1":
c += 1
return (2**c - 1) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def isp(ob, n: int) -> bool:
d = True
if n == 1:
return True
elif n == 0:
return False
elif n < 0:
return False
while n != 1:
if n % 2 == 0:
d = True
else:
return False
n = int(n / 2)
return d
def numberOfSubsequences(ob, N, A):
cnt = 0
for i in range(N):
if ob.isp(A[i]):
cnt += 1
return (2**cnt - 1) % 1000000007 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
c = 0
mod = 10**9 + 7
for i in A:
if i & i - 1 == 0:
c += 1
ans = 1
while c > 0:
ans = ans * 2 % mod
c -= 1
return ans - 1
return (2**c - 1) % mod | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | MOD = 10**9 + 7
class Solution:
def numberOfSubsequences(ob, N, A):
c = 0
for i in range(N):
if A[i] & A[i] - 1 == 0:
c += 1
return pow(2, c, MOD) - 1 | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
temp = {}
for i in A:
if i == 1:
temp[1] = temp.get(1, 0) + 1
elif i & i - 1 == 0:
temp[2] = temp.get(2, 0) + 1
temp[1] = temp.get(1, 0)
temp[2] = temp.get(2, 0)
return int(pow(2, temp[1]) * pow(2, temp[2]) - 1) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
s = set()
toadd = 1
for i in range(33):
toadd = 1 << i
s.add(toadd)
count = 0
for i in A:
if i in s:
count += 1
return (1 << count) % 1000000007 - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | def p2(n):
return n and not n & n - 1
class Solution:
def numberOfSubsequences(ob, N, A):
k = len([v for v in A if p2(v)])
m = 1000000007
return pow(2, k, m) - 1 | FUNC_DEF RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
f = 2**30
j = 0
for i in A:
if f % i == 0:
j = j + 1
return (2**j - 1) % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
def isPowerOf2(num):
if num == 0:
return False
if num == 1:
return True
if num & num - 1:
return False
return True
def powm(a, b, mod):
res = 1
while b:
if b & 1:
res = res * a % mod
a = a * a % mod
b >>= 1
return res
count = 0
for i in range(N):
if isPowerOf2(A[i]):
count += 1
return (powm(2, count, 1000000007) - 1 + 1000000007) % 1000000007 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(ob, N, A):
mod = 1000000007
c = 0
lst = []
for i in range(28):
lst.append(2**i)
for i in range(N):
if A[i] in lst:
c += 1
return (2**c - 1) % mod | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def isPowerOfTwo(self, num):
if num == 0:
return False
return num & num - 1 == 0
def numberOfSubsequences(ob, N, A):
counter = 0
for element in A:
if ob.isPowerOfTwo(element):
counter += 1
return (pow(2, counter) - 1) % 1000000007 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def isPower2(self, n):
return n & n - 1 == 0
def numberOfSubsequences(self, N, A):
count = 0
for x in A:
count += self.isPower2(x)
return ((1 << count) - 1) % (10**9 + 7) | CLASS_DEF FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given is an array A[] of size N. Return the number of non-empty subsequences such that the product of all numbers in the subsequence is Power of 2. Since the answer may be too large, return it modulo 10^{9} + 7.
Example 1:
Input:
N = 3
A[] = {1, 6, 2}
Output:
3
Explanation:
The subsequence that
can be chosen is {1},
{2} and {1,2}.
Example 2:
Input:
N = 3
A[] = {3, 5, 7}
Output:
0
Explanation:
No subsequences exist.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubsequences() which takes an integer N and an array A and returns the number of subsequences that exist. As this number can be very large return the result under modulo 10^{9}+7.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= A[i] <= 10^{9} | class Solution:
def numberOfSubsequences(self, N, A):
count = 0
for i in range(N):
if A[i] & A[i] - 1 == 0:
count += 1
return (2**count - 1) % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def findMaxPowerWithinRange(self, num):
x = 0
while pow(2, x) <= num:
x = x + 1
return x - 1
def countBits(self, N):
x = self.findMaxPowerWithinRange(N)
if N == 0:
return 0
firstExpression = pow(2, x - 1) * x
secondExpression = N - pow(2, x) + 1
ans = firstExpression + secondExpression + self.countBits(N - pow(2, x))
ans = int(ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | def powerLargest(N):
x = 0
while 1 << x <= N:
x += 1
return x - 1
class Solution:
def countBits(self, N):
if N <= 1:
return N
x = powerLargest(N)
return x * pow(2, x - 1) + (N - pow(2, x) + 1) + self.countBits(N - pow(2, x)) | FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def largestTwoPower(N):
x = 0
while 1 << x <= N:
x += 1
return x - 1
if N <= 1:
return N
x = largestTwoPower(N)
return x * int(2 ** (x - 1)) + (N - (1 << x) + 1) + self.countBits(N - (1 << x)) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def nearest(self, A):
i = 0
while 1 << i <= A:
i = i + 1
return i - 1
def countBits(self, N):
if N <= 1:
return N
x = self.nearest(N)
a = x * (1 << x - 1)
b = N - (1 << x) + 1
return a + b + self.countBits(N - (1 << x)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
ans = 0
for i in range(20, 0, -1):
if N & 1 << i:
ans += N % (1 << i) + 1 + (1 << i - 1) * i
N &= (1 << i) - 1
if N % 2:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def getpowerof2(n):
x = 0
while 2**x <= n:
x += 1
return x - 1
if N == 0:
return 0
x = getpowerof2(N)
bitstill2x = 2 ** (x - 1) * x
msbrest = N - 2**x + 1
rest = N - 2**x
ans = bitstill2x + msbrest + self.countBits(rest)
return int(ans) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
set_bits = 0
while N > 0:
mx_pwr_2_lt_n, mx_pwr_2_lt_n_val = 0, 1
while mx_pwr_2_lt_n_val << 1 <= N:
mx_pwr_2_lt_n_val <<= 1
mx_pwr_2_lt_n += 1
set_bits += (mx_pwr_2_lt_n_val >> 1) * mx_pwr_2_lt_n + (
N - mx_pwr_2_lt_n_val + 1
)
N -= mx_pwr_2_lt_n_val
return set_bits | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
n = N
ans = 0
pre = 1
add = 1
while n > 0:
if n & 1 == 1:
ans += pre
if add > 1:
ans += N & add - 1
n = n >> 1
pre = pre * 2 + (add - 1)
add = add * 2
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | def countTotalBits(n):
count = 0
while n >= 1:
n = n / 2
count += 1
return count
class Solution:
def countBits(self, N):
if N == 1:
return 1
if N == 0:
return 0
x = countTotalBits(N) - 1
val = 2 ** (x - 1) * x + (N + 1 - 2**x) + Solution.countBits(self, N - 2**x)
return val | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
if N <= 1:
return N
p = self.highestPower(N)
leftBits = p * (1 << p - 1)
midBits = N - (1 << p) + 1
rightBits = self.countBits(N - (1 << p))
return leftBits + midBits + rightBits
def highestPower(self, n):
count = 0
while 1 << count <= n:
count += 1
return count - 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def highest_power(n):
a = 1
power = 1
while a * 2 <= n:
a = a * 2
power += 1
return power - 1
def rec(n):
if n == 0:
return 0
if n == 1:
return 1
temp = highest_power(n)
return 2 ** (temp - 1) * temp + (n - 2**temp + 1) + rec(n - 2**temp)
return rec(N) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
if N <= 1:
return N
x = self.left_set_bit(N)
return x * (1 << x - 1) + (N - (1 << x) + 1) + self.countBits(N - (1 << x))
def left_set_bit(self, n):
res = -1
while n:
n = n >> 1
res += 1
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, n):
c = 2
ans = 0
n += 1
while True:
ans += n // c * (c // 2)
ans += max(0, n % c - c // 2)
if n // c == 0:
break
c *= 2
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
n = N
def get_left_most_set_bit(n):
left_most_set_bit_indx = 0
while n > 0:
left_most_set_bit_indx += 1
n >>= 1
return left_most_set_bit_indx
left_most_set_bit_indx = get_left_most_set_bit(n)
total_rep = 0
mod = 0
nearest_pow = 0
total_set_bit_count = 0
add_remaining = 0
curr = 0
for i in range(1, left_most_set_bit_indx + 1):
nearest_pow = pow(2, i)
if nearest_pow > n:
last_pow = pow(2, i - 1)
mod = n % last_pow
total_set_bit_count += mod + 1
else:
if i == 1 and n % 2 == 1:
total_rep = (n + 1) / nearest_pow
mod = nearest_pow % 2
add_remaining = 0
else:
total_rep = int(n / nearest_pow)
mod = n % nearest_pow
add_remaining = (
int(mod - nearest_pow / 2 + 1) if mod >= nearest_pow / 2 else 0
)
curr = int(total_rep * (nearest_pow / 2) + add_remaining)
total_set_bit_count += curr
return total_set_bit_count | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
two = 2
ans = 0
n = N
while n != 0:
ans += int(N / two) * (two >> 1)
if N & two - 1 > (two >> 1) - 1:
ans += (N & two - 1) - (two >> 1) + 1
two <<= 1
n >>= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
N = N + 1
k = 0
res = 0
while True:
f = pow(2, k)
r = N % f
q = N // f
if q == 0:
return res
if r == 0 or q % 2 == 0:
q = q // 2
res = res + q * pow(2, k)
else:
q = q // 2
res = res + q * pow(2, k) + r
k = k + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
count = 0
N = N + 1
powerOf2 = 1
while powerOf2 <= N:
pairs = N // powerOf2
count += pairs // 2 * powerOf2
if pairs & 1:
count += N % powerOf2
powerOf2 <<= 1
return count
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.countBits(N)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def getAmountOfBits(self, n: int) -> int:
start = 1
cnt = 1
while start <= n:
start *= 2
cnt += 1
return cnt - 1
def helper(self, n: int, start: int = 4, total: int = 3):
count = n + 1 - start
for i in range(count):
total += i + 1
if count > 2:
total -= count - 2
return total
def countBits(self, n: int) -> int:
numDigits = self.getAmountOfBits(n)
total = 1
if numDigits == 1:
return total
else:
if n == 2**numDigits - 1:
const = 1
for i in range(1, numDigits):
const *= 2
total += const + i * (const >> 1)
else:
const = 1
for i in range(1, numDigits - 1):
const *= 2
total += const + i * (const >> 1)
const *= 2
number_per_group_full = 1
number_per_group_non_full = 0
k = n - const + 1
total += k
while k >= 1:
total += k // 2 * number_per_group_full
if k % 2 == 1:
total += number_per_group_non_full
number_per_group_non_full += number_per_group_full
k = k // 2
number_per_group_full *= 2
return total | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def f(self, n):
x = 0
while 1 << x <= n:
x += 1
return x - 1
def countBits(self, N):
if N == 0:
return 0
if N == 1:
return 1
x = self.f(N)
return x * 2 ** (x - 1) + N - 2**x + 1 + self.countBits(N - 2**x) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
n = len(bin(N))
ans = 0
N += 1
for i in range(1, n + 1):
q = N // 2**i
r = N % 2**i
first = q * 2 ** (i - 1)
if r > 2 ** (i - 1):
second = r - 2 ** (i - 1)
else:
second = 0
ans = ans + first + second
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def getleftmost(n):
m = 0
while n > 1:
n >>= 1
m += 1
return m
def backtrack(n, m):
if not n:
return 0
if n == (1 << m + 1) - 1:
return (m + 1) * (1 << m)
n = n - (1 << m)
return n + 1 + self.countBits(n) + m * (1 << m - 1)
m = getleftmost(N)
return backtrack(N, m) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def getpower(temp):
x = 0
while 1 << x <= temp:
x += 1
return x - 1
def solve(n):
if n == 0 or n == 1:
return n
p = getpower(n)
temp = 1 << p - 1
return p * temp + n - (1 << p) + 1 + solve(n - (1 << p))
return solve(N) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def rec(N):
if N == 0 or N == 1:
return N
m = 1
c = 0
while m <= N:
c = c + 1
m = m << 1
c = c - 1
a = (1 << c - 1) * c
b = N - (1 << c) + 1
return a + b + rec(N - 2**c)
c = rec(N)
return c | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, n):
i = 0
bitmap = 1
total = 0
while 2**i <= n:
powi = 1 << i
if powi & n:
rem = (n & bitmap) - powi
total += powi // 2 * i + 1 + rem
i += 1
bitmap <<= 1
bitmap += 1
return total | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
last = N
N = N + 1
count = 0
a, b, c = 2, 1, 0
while last:
last = last >> 1
count += N // a * b
rem = N % a
if rem > b:
count += rem - b
a = a << 1
b = b << 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
You are given a number N. Find the total number of setbits in the numbers from 1 to N.
Example 1:
Input: N = 3
Output: 4
Explaination:
1 -> 01, 2 -> 10 and 3 -> 11.
So total 4 setbits.
Example 2:
Input: N = 4
Output: 5
Explaination: 1 -> 01, 2 -> 10, 3 -> 11
and 4 -> 100. So total 5 setbits.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6} | class Solution:
def countBits(self, N):
def solve(n):
if n < 2:
return n
x = 1
i = 0
while 1:
if x << i > n:
break
i += 1
i -= 1
temp = (1 << i - 1) * i
temp += n - (1 << i) + 1
return temp + solve(n - (1 << i))
return solve(N) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.