description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
xor = 0
for num in nums:
xor ^= num
bit = 0
while xor % 2 == 0:
xor //= 2
bit += 1
group1 = []
group2 = []
for num in nums:
if (num >> bit) % 2 == 0:
group1.append(num)
else:
group2.append(num)
num1 = 0
for num in group1:
num1 ^= num
num2 = 0
for num in group2:
num2 ^= num
return [num1, num2] if num1 < num2 else [num2, num1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR VAR LIST VAR VAR LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
sums = 0
for i in range(0, len(nums)):
sums = sums ^ nums[i]
sums = sums & -sums
sum1 = 0
sum2 = 0
for i in range(0, len(nums)):
if nums[i] & sums > 0:
sum1 = sum1 ^ nums[i]
else:
sum2 = sum2 ^ nums[i]
return sorted([sum1, sum2]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
xor = 0
for i in nums:
xor = i ^ xor
listt1 = []
listt2 = []
num = 0
a = xor
while xor > 0:
if xor % 2 == 1:
break
else:
num += 1
xor //= 2
xor1 = 0
for i in nums:
if i // 2**num % 2 == 0:
xor1 = xor1 ^ i
return min(xor1, xor1 ^ a), max(xor1, a ^ xor1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
hmap = {}
for i in range(len(nums)):
if nums[i] in hmap:
hmap[nums[i]] += 1
else:
hmap[nums[i]] = 1
result = []
for item in nums:
if hmap[item] == 1:
result.append(item)
result = sorted(result)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
val = nums[0]
for i in range(1, len(nums)):
val = val ^ nums[i]
rsb = val & -val
num1 = 0
num2 = 0
for i in nums:
if i & rsb == 0:
num1 = num1 ^ i
else:
num2 = num2 ^ i
return [min(num1, num2), max(num1, num2)] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, arr):
res = 0
for elm in arr:
res ^= elm
res = bin(res)[2:]
pos = -1
for i in range(-1, -len(res) - 1, -1):
if res[i] == "1":
pos = i
break
res = int(res, 2)
new = res
for elm in arr:
if -len(bin(elm)[2:]) <= pos and bin(elm)[2:][pos] == "1":
res ^= elm
ans = [res]
ans.append(res ^ new)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR STRING VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
xor = nums[0]
for i in nums[1:]:
xor = xor ^ i
ans = []
right_bit = xor & ~(xor - 1)
x = 0
y = 0
for i in range(len(nums)):
if nums[i] & right_bit:
x = x ^ nums[i]
else:
y = y ^ nums[i]
ans.append(x)
ans.append(y)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, arr):
xorOfAllElements = arr[0]
for i in range(1, len(arr)):
xorOfAllElements ^= arr[i]
rightSetBit = xorOfAllElements & ~(xorOfAllElements - 1)
x = 0
y = 0
for i in range(len(arr)):
if arr[i] & rightSetBit > 0:
x ^= arr[i]
else:
y ^= arr[i]
ans = []
if x < y:
ans.append(x)
ans.append(y)
else:
ans.append(y)
ans.append(x)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
xor = 0
for i in nums:
xor ^= i
xor = xor & ~(xor - 1)
a = 0
b = 0
for i in nums:
if xor & i == 0:
a ^= i
else:
b ^= i
if a > b:
return [b, a]
return [a, b] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR RETURN LIST VAR VAR RETURN LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
xr = 0
for i in nums:
xr ^= i
a = []
b = []
i = 0
while 1:
if xr & 1 << i:
break
i += 1
v = 1 << i
for i in nums:
if v & i:
a.append(i)
else:
b.append(i)
xa = 0
xb = 0
for i in a:
xa ^= i
for i in b:
xb ^= i
if xa < xb:
return [xa, xb]
return [xb, xa] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR RETURN LIST VAR VAR RETURN LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, n):
m = 0
for i in n:
m = m ^ i
i = 0
while m > 0:
if m & 1 == 1:
break
m = m >> 1
i = i + 1
l = []
r = []
for j in n:
if j & 1 << i:
l.append(j)
else:
r.append(j)
s = []
x = 0
y = 0
for i in l:
x = x ^ i
s.append(x)
for i in r:
y = y ^ i
s.append(y)
s.sort()
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
nums.sort()
count = 0
ini = nums[0]
listt = []
for i in nums:
if i == ini:
count += 1
else:
if count % 2 == 1:
listt.append(ini)
ini = i
count = 1
if count % 2 == 1:
listt.append(ini)
return listt[0], listt[1] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR NUMBER |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
c = []
d = {}
for i in nums:
if i in d:
c.remove(i)
else:
c.append(i)
d[i] = 1
c.sort()
return c | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
hash = {}
res = []
for n in nums:
if hash.get(n, None) == None:
hash[n] = 1
else:
hash[n] += 1
for key, value in hash.items():
if value == 1:
res.append(key)
if res[0] > res[1]:
res[0], res[1] = res[1], res[0]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NONE NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
nums.sort()
c = 0
a = []
for h in nums:
if c == 0:
c ^= h
elif c == h:
c = 0
else:
a.append(c)
c = h
if c != 0:
a.append(c)
return a | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
res = 0
for num in nums:
res = res ^ num
bRes = bin(res)[2:]
pos = -1
for i in range(len(bRes) - 1, -1, -1):
if bRes[i] == "0":
pos -= 1
else:
break
temp1 = res
temp2 = res
for num in nums:
bNum = bin(num)[2:]
if pos >= -1 * len(bNum) and bNum[pos] == "1":
temp1 = temp1 ^ num
else:
temp2 = temp2 ^ num
a = [temp1, temp2]
a.sort()
return a
return [3, 4] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR RETURN VAR RETURN LIST NUMBER NUMBER |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
nums.sort(reverse=True)
res = []
while nums:
if len(nums) == 1:
res.append(nums[0])
break
curr1 = nums.pop()
curr2 = nums.pop()
if curr1 != curr2:
res.append(curr1)
nums.append(curr2)
if len(res) == 2:
break
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
if len(nums) == 0:
return
if 1 > len(nums) > 10**6:
return
final = []
nums_set = list(set(nums))
for i in nums_set:
if 1 <= i <= 50 * 10**6 and nums.count(i) == 1:
final.append(i)
if len(final) == 2:
break
final.sort()
return final | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER RETURN ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
nums.sort()
output = []
i = 0
while i < len(nums):
if i == len(nums) - 1:
output = output + [nums[i]]
break
if nums[i] != nums[i + 1]:
output = output + [nums[i]]
i = i + 1
else:
i = i + 2
return output | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
bitmask = 0
for num in nums:
bitmask ^= num
diff = bitmask & -bitmask
x = 0
for num in nums:
if num & diff:
x ^= num
y = bitmask ^ x
return [x, y] if x < y else [y, x] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR LIST VAR VAR LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
d = dict()
for i in nums:
if i in d:
d[i] += 1
else:
d[i] = 1
c = []
for i in d:
if d[i] == 1:
c.append(i)
if c[0] > c[1]:
c[0], c[1] = c[1], c[0]
return c | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
d = {}
l = []
for i in nums:
try:
d[i] += 1
l.remove(i)
except:
d[i] = 1
l.append(i)
return sorted(l) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
sum = 0
for i in range(len(nums)):
sum = sum ^ nums[i]
right = sum & ~(sum - 1)
x, y = 0, 0
for i in range(len(nums)):
if nums[i] & right > 0:
x = x ^ nums[i]
else:
y = y ^ nums[i]
ans = []
ans.append(x)
ans.append(y)
return sorted(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
h = {}
ans = []
for i in nums:
if str(i) in h.keys():
h[str(i)] += 1
else:
h.update({str(i): 1})
for i in h.keys():
if h[str(i)] == 1:
ans.append(int(i))
if len(ans) == 0:
ans.append(-1)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR DICT FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, s):
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
unique_chars = [ch for ch in freq if freq[ch] == 1]
return sorted(unique_chars) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
res = 0
for i in nums:
res = res ^ i
setbit = 0
setbit = res & ~(res - 1)
res1 = 0
res2 = 0
for i in nums:
if i & setbit:
res1 = res1 ^ i
else:
res2 = res2 ^ i
x = min(res1, res2)
y = max(res1, res2)
return x, y | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
xor = 0
for n in nums:
xor ^= n
x = 0
y = 0
rmsb = xor & ~(xor - 1)
for n in nums:
if n & rmsb == rmsb:
x ^= n
else:
y ^= n
return sorted([x, y]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
hash_map = {}
for i in nums:
if i not in hash_map:
hash_map[i] = 1
else:
hash_map.pop(i)
ans = []
for key in hash_map:
ans.append(key)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
temp_set = set()
for i in range(len(nums)):
if nums[i] in temp_set:
temp_set.remove(nums[i])
else:
temp_set.add(nums[i])
return sorted(temp_set) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
nl = {}
for x in nums:
if nl.get(x):
nl.pop(x)
else:
nl[x] = 1
result = list(nl.keys())
if result[0] > result[1]:
return result[::-1]
else:
return result | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
res = []
for n in nums:
if n in res:
res.remove(n)
else:
res.append(n)
res.sort()
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
ans = 0
for i in nums:
ans ^= i
ans = ans & ~(ans - 1)
set1, set2 = 0, 0
for i in nums:
if i & ans:
set1 ^= i
else:
set2 ^= i
res = []
if set1 > set2:
res.extend([set2, set1])
else:
res.extend([set1, set2])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
res = []
dic = {}
for i in nums:
dic[i] = dic.get(i, 0) + 1
for i, j in dic.items():
if j == 1:
res.append(i)
if len(res) == 2:
return sorted(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def singleNumber(self, nums):
summ = 0
for i in nums:
summ = summ ^ i
summ = summ & -summ
a = 0
b = 0
for i in nums:
if i & summ > 0:
a = a ^ i
else:
b = b ^ i
if a > b:
return [b, a]
else:
return [a, b] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN LIST VAR VAR RETURN LIST VAR VAR |
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.
Example 1:
Input:
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4
Explanation:
3 and 4 occur exactly once.
Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.
Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= length of array <= 10^{6 }
1 <= Elements in array <= 5 * 10^{6} | class Solution:
def ithBitSet(self, n: int, i: int) -> bool:
return n & 1 << i != 0
def singleNumber(self, nums):
xor = 0
for num in nums:
xor ^= num
i = 0
while xor & 1 == 0:
xor = xor >> 1
i += 1
res1 = 0
res2 = 0
for num in nums:
if self.ithBitSet(num, i):
res1 ^= num
else:
res2 ^= num
return sorted([res1, res2]) | CLASS_DEF FUNC_DEF VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
st = 0
for i in range(len(s)):
if st == 0:
if s[i] == "1":
st = 1
elif st == 1:
if s[i] == "1":
st = 0
else:
st = 2
elif s[i] == "0":
st = 1
return int(st == 0) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
x = len(s) - 1
summ = 0
j = 0
for i in range(x, -1, -1):
summ = summ + (1 << i) * int(s[j])
j = j + 1
if summ % 3 == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
res = 0
for i in range(len(s)):
if s[i] == "1":
if i % 2 == 0:
res += 1
else:
res += 2
if res % 3 == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
n = len(s)
j = 0
ans = 0
for i in range(n - 1, -1, -1):
if s[i] == "1":
ans += 2 << j
else:
ans += 0
j += 1
if ans % 3 == 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
c = str(int(s, 2))
d = 0
for i in c:
d = d + int(i)
if d % 3 == 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, S):
oddPosition, evenPosition = 0, 0
for i in range(len(S)):
if i % 2 and S[i] == "1":
oddPosition += 1
if i % 2 == 0 and S[i] == "1":
evenPosition += 1
return 1 if abs(oddPosition - evenPosition) % 3 == 0 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
od, ev = 0, 0
for i in range(len(s)):
if s[i] == "1":
if i & 1:
od += 1
else:
ev += 1
ans = abs(od - ev)
if ans % 3 == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
e = 0
o = 0
for i in range(len(s)):
if i & 1 == 0 and s[i] == "1":
e += 1
if i & 1 and s[i] == "1":
o += 1
if (e - o) % 3 == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
num = 0
for i in s:
num = num * 2 + int(i)
return int(num % 3 == 0)
def isDivisible(self, s):
return int(int(s, 2) % 3 == 0)
def isDivisible(self, s):
pos, even, odd = 0, 0, 0
for i in s:
if pos % 2:
if s[pos] == "1":
odd += 1
elif s[pos] == "1":
even += 1
pos += 1
return int((odd - even) % 3 == 0) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
n = len(s)
even_sum = odd_sum = 0
for i in range(n - 1, -1, -1):
if s[i] == "1":
if (n - i - 1) % 2 == 0:
even_sum += 1
else:
odd_sum += 1
return (abs(even_sum - odd_sum) % 3 == 0) * 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
oddsetbits, evensetbits = 0, 0
n = len(s)
for i, c in enumerate(s):
if c == "1":
if i & 1:
oddsetbits += 1
else:
evensetbits += 1
return 1 if not abs(oddsetbits - evensetbits) % 3 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
i = 0
n = len(s)
s = s[::-1]
ans = 0
j = 1
num = 1
while i < n:
if s[i] == "1":
ans += num
i += 1
if num == 1:
num = 2
else:
num *= 2
return 1 if not ans % 3 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
e = 0
o = 0
for i in range(len(s)):
if i % 2 == 0:
e += int(s[i])
else:
o += int(s[i])
diff = abs(e - o)
if diff % 3 == 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
number = 0
k = len(s) - 1
for i in range(len(s)):
number = number + int(s[i]) * (1 << k)
k = k - 1
if number % 3 == 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER |
Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5} | class Solution:
def isDivisible(self, s):
num = 0
for i in s:
num = num * 2 + int(i)
return int(num % 3 == 0)
def isDivisible(self, s):
return int(int(s, 2) % 3 == 0) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
if not nums:
return 0
seen = set([])
counts = {}
for num in nums:
if num in seen:
counts[num] += 1
else:
counts[num] = 1
seen.add(num)
for key in counts:
if counts[key] == 1:
return key
return 0 | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER RETURN VAR RETURN NUMBER |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
nums.sort()
if len(nums) < 3:
return nums[0]
for i in range(0, len(nums) - 1, 3):
if nums[i] != nums[i + 2]:
return nums[i]
return nums[-1] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN VAR NUMBER |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
counts = {}
for num in nums:
if num not in counts:
counts[num] = 1
else:
counts[num] += 1
for k, v in list(counts.items()):
if v == 1:
return k | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN VAR |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
zero = 0
one = 0
for num in nums:
one, zero = (one ^ num & zero) & ~(~zero & one & num), (zero ^ num) & ~(
~zero & one & num
)
return zero | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
a = b = 0
for c in nums:
a, b = a & ~b & ~c | ~a & b & c, ~a & b & ~c | ~a & ~b & c
return a | b | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
dict = {}
for i in nums:
if i in dict:
dict[i] = dict[i] + 1
else:
dict[i] = 1
for i in list(dict.keys()):
if dict[i] == 1:
return i
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN VAR RETURN NUMBER |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
res = {}
for i in nums:
if str(i) not in res:
res[str(i)] = 1
else:
res[str(i)] += 1
for i in res:
if res[i] == 1:
return int(i) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
if len(nums) == 1:
return nums[0]
seen = set()
for a in nums:
if a not in seen:
seen.add(a)
s1 = sum(nums)
s2 = sum(seen)
return int(s2 - (s1 - s2) / 2) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
ones, twos = 0, 0
for i in nums:
ones = ones ^ i & ~twos
twos = twos ^ i & ~ones
return ones | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
_dict = {}
for num in nums:
if num not in _dict:
_dict[num] = 1
elif _dict[num] == 1:
_dict[num] += 1
else:
del _dict[num]
return list(_dict.keys())[0] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER |
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99 | class Solution:
def singleNumber(self, nums):
ans = 0
for i in range(32):
count = 0
for num in nums:
if num >> i & 1:
count += 1
ans |= count % 3 << i
if ans >= 2**31:
ans -= 2**32
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP NUMBER NUMBER VAR BIN_OP NUMBER NUMBER RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
start = 0
for pos in range(16, -1, -1):
bit = 1 << pos
choose = None
for i in range(start, n):
if a[i] & bit:
choose = i
break
if choose is not None:
a[start], a[choose] = a[choose], a[start]
for i in range(n):
if a[i] & bit and i != start:
a[i] ^= a[start]
start += 1
if start == n:
break
ans = 0
for i in range(n):
ans ^= a[i]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
index = 0
for i in range(31, -1, -1):
maxindex = index
maxele = -999999999
for j in range(index, n):
if a[j] & 1 << i != 0 and a[j] > maxele:
maxele = a[j]
maxindex = j
if maxele == -999999999:
continue
temp = a[index]
a[index] = a[maxindex]
a[maxindex] = temp
maxindex = index
for j in range(n):
if a[j] & 1 << i != 0 and j != maxindex:
a[j] = a[j] ^ a[maxindex]
index = index + 1
res = 0
for i in range(n):
res = res ^ a[i]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, arr, n):
j = 0
for i in range(31, -1, -1):
for k in range(j, n):
if arr[k] & 1 << i:
arr[k], arr[j] = arr[j], arr[k]
break
if arr[j] & 1 << i:
for k in range(j + 1, n):
if arr[k] & 1 << i:
arr[k] ^= arr[j]
j += 1
if j == n:
break
res = 0
for i in range(j):
res = max(res, res ^ arr[i])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
ix = 0
for i in range(31, -1, -1):
mx = -1 * float("inf")
mxIx = -1
for j in range(ix, n):
if a[j] >> i & 1 and a[j] > mx:
mx = a[j]
mxIx = j
el = a[mxIx]
if mxIx == -1:
continue
a[ix], a[mxIx] = a[mxIx], a[ix]
mxIx = ix
ix += 1
for j in range(n):
if a[j] >> i & 1 and j != mxIx:
a[j] = a[j] ^ el
res = a[0]
for i in range(1, n):
res = res ^ a[i]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
array = list(a)
if not array:
return 0
x = 0
while True:
y = max(array)
if y == 0:
return x
x = max(x, x ^ y)
array = [min(z, z ^ y) for z in array] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
idx = 0
for i in range(31, -1, -1):
maxidx, maxval = idx, -1
for j in range(idx, n):
if a[j] & 1 << i != 0 and a[j] > maxval:
maxval, maxidx = a[j], j
if maxval < 0:
continue
a[idx], a[maxidx] = a[maxidx], a[idx]
for j in range(n):
if j != idx and a[j] & 1 << i != 0:
a[j] = a[j] ^ a[idx]
idx += 1
res = 0
for x in a:
res ^= x
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
start = 0
for i in range(18, -1, -1):
ma = -1
k = -1
for j in range(start, n):
if a[j] & 1 << i and (ma == -1 or ma < a[j]):
ma = a[j]
k = j
if k == -1:
continue
a[k], a[start] = a[start], a[k]
start += 1
for j in range(start, n):
if a[j] & 1 << i:
a[j] ^= ma
ans = 0
for j in range(n):
if ans < ans ^ a[j]:
ans ^= a[j]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
BITS = 32
ind = 0
offset = 0
for offset in range(BITS - 1, -1, -1):
maxInd = ind
maxVal = -1
for i in range(ind, n):
val = a[i]
if val > 0 and 1 << offset & val != 0 and val > maxVal:
maxVal = val
maxInd = i
if maxVal == -1:
continue
temp = a[ind]
a[ind] = a[maxInd]
a[maxInd] = temp
maxInd = ind
for j in range(n):
val = a[j]
if j != maxInd and 1 << offset & val != 0:
a[j] = val ^ a[maxInd]
ind += 1
res = 0
for item in a:
res = res ^ item
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
index = 0
for i in range(31, -1, -1):
max_ind = index
max_ele = -2147483648
for j in range(index, n):
if a[j] & 1 << i and a[j] > max_ele:
max_ele = a[j]
max_ind = j
if max_ele == -2147483648:
continue
a[index], a[max_ind] = a[max_ind], a[index]
max_ind = index
index += 1
for j in range(n):
if a[j] & 1 << i and j != max_ind:
a[j] ^= a[max_ind]
res = 0
for i in a:
res ^= i
return res
if __name__ == "__main__":
T = int(input())
for i in range(T):
n = int(input())
a = [int(x) for x in input().split()]
ob = Solution()
ans = ob.maxXor(a, n)
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
index = 0
for i in range(31, -1, -1):
el = -99999999
mi = index
for j in range(index, n, 1):
sb = a[j] >> i & 1
if sb != 0 and el < a[j]:
el = a[j]
mi = j
if el == -99999999:
continue
a[index], a[mi] = a[mi], a[index]
mi = index
for j in range(n):
sb = a[j] >> i & 1
if sb != 0 and j != mi:
a[j] = a[j] ^ a[mi]
index = index + 1
res = 0
for i in a:
res = res ^ i
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[ ] of positive integers, the task is to find the maximum XOR value of the elements from all the possible subsets.
Example 1:
Input: N = 3, arr[] = {2, 4, 5}
Output: 7
Explanation: Subset {2, 5} has maximum xor
Example 2:
Input: N = 1, arr[] = {1}
Output: 1
Explanation: Subset {1} has maximum xor
Your Task:
You don't need to read input or print anything. Complete the function maxXor() which takes N and array arr as input parameter and returns the maximum xor value.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5} | class Solution:
def maxXor(self, a, n):
currIndex = 0
for bit in range(31, -1, -1):
maxElement, maxIndex = float("-inf"), currIndex
for k in range(currIndex, n):
if a[k] & 1 << bit and a[k] > maxElement:
maxElement = a[k]
maxIndex = k
if maxElement != float("-inf"):
a[maxIndex], a[currIndex] = a[currIndex], a[maxIndex]
maxIndex = currIndex
for k in range(n):
if k != maxIndex and a[k] & 1 << bit:
a[k] ^= a[maxIndex]
currIndex += 1
res = 0
for k in range(n):
res ^= a[k]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
mod = pow(10, 9) + 7
total = 0
for i in range(0, 32):
count = 0
for v in A:
if v & 1 << i:
count += 1
total = (total + count * (N - count) * 2) % mod
return total | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
ans = 0
bit1s = [0] * 32
for i in range(N):
j = 0
k = j
while A[i] > 0:
bit1s[k] += A[i] & 1
A[i] = A[i] >> 1
k = k + 1
for i in range(32):
ans = (ans + bit1s[i] * (N - bit1s[i]) * 2) % 1000000007
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
ans = 0
for i in range(31, -1, -1):
count_1 = 0
count_0 = 0
for ele in A:
binary_no = bin(ele)[2:]
l = len(binary_no)
zero_str = ""
for _ in range(32 - l):
zero_str = zero_str + "0"
final_bno = zero_str + binary_no
if final_bno[i] == "1":
count_1 += 1
else:
count_0 += 1
ans = ans + count_1 * count_0
return ans * 2 % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
res = 0
for i in range(32):
count1 = 0
for j in range(N):
bit = 1 << i
if A[j] & bit == 0:
count1 += 1
res += count1 * (N - count1) * 2
return res % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | def countNoOfBits(num):
count = 0
while num > 0:
num = num >> 1
count += 1
return count
class Solution:
def countBits(self, N, A):
maxBits = countNoOfBits(max(A))
count1 = 0
count0 = 0
mask = 1
ans = 0
while maxBits > 0:
for i in range(N):
if A[i] & mask > 0:
count1 += 1
else:
count0 += 1
ans += count1 * count0 * 2
count1 = 0
count0 = 0
mask = mask << 1
maxBits = maxBits - 1
return ans % (10**9 + 7) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
ans = 0
for mask in range(31):
ones = 0
zeros = 0
for i in range(N):
if A[i] & 1 << mask:
ones += 1
else:
zeros += 1
ans = ans + 2 * (ones * zeros)
return ans % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, n, arr):
ans = 0
for i in range(0, 32):
count = 0
for j in range(0, n):
if arr[j] & 1 << i:
count += 1
ans += count * (n - count) * 2
return ans % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
mod = 1000000007
ans = 0
for i in range(31):
count = 0
for j in A:
if j & 1 << i:
count += 1
ans += count * (N - count) * 2
ans %= mod
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, nums):
mask = 1
ans = 0
for i in range(32):
cnt = 0
for num in nums:
if num & mask:
cnt += 1
ans += 2 * (cnt * (N - cnt))
mask <<= 1
return ans % int(10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | MOD = 10**9 + 7
class Solution:
def countBits(self, N, A):
def _calc(bit):
c1 = c0 = 0
for v in A:
if v & bit:
c1 += 1
else:
c0 += 1
return c1 * c0 * 2
ans = 0
maxv = max(A)
bit = 1
while bit <= maxv:
ans = (ans + _calc(bit)) % MOD
bit <<= 1
return ans | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
maxi = 1000000000.0 + 7
ans = 0
for i in range(0, 32):
cnt = 0
for j in range(N):
if A[j] & 1 << i != 0:
cnt += 1
ans = (ans + cnt * (N - cnt) * 2) % maxi
return int(ans)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
A = input().split()
for it in range(N):
A[it] = int(A[it])
ob = Solution()
print(ob.countBits(N, A)) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR 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 FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, n, x):
bits = 32
z = [(0) for i in range(32)]
for i in x:
s = bin(i)[2:].zfill(32)
for j in range(32):
if s[j] == "1":
z[j] += 1
ans = 0
mo = 10**9 + 7
for i in x:
s = bin(i)[2:].zfill(32)
for j in range(32):
if s[j] == "1":
ans = (ans + n - z[j]) % mo
else:
ans = (ans + z[j]) % mo
return ans % mo | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def f(self, n):
s = 0
for i in range(32):
if n & 1 == 1:
s += 1
n >>= 1
return s
def countBits(self, N, A):
s = 0
MAX = 1000000007
for i in range(32):
c = 0
for j in range(N):
if A[j] & 1 << i != 0:
c += 1
s = (s + c * (N - c) * 2) % MAX
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
res = 0
for i in range(0, 32):
count = 0
for val in A:
if val & 1 << i != 0:
count += 1
off = N - count
diff = count * off * 2
res += diff
return res % 1000000007 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
M = 1000000000.0 + 7
res = 0
for i in range(32):
z = 0
for j in range(N):
if A[j] & 1 << i:
z += 1
res += z * (N - z) * 2
return int(res % M) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
bits = [[(0) for i in range(32)], [(0) for i in range(32)]]
for i in A:
power = 0
while i > 1:
bits[i % 2][power] += 1
i //= 2
power += 1
bits[1][power] += 1
for j in range(power + 1, 32):
bits[0][j] += 1
ans = 0
for i in range(32):
ans = (ans + bits[0][i] * bits[1][i]) % (10**9 + 7)
return ans * 2 % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | from itertools import combinations
class Solution:
def countBits(self, N, A):
s = 0
mod = 10**9 + 7
for i in range(0, 32):
count = 0
for j in range(0, N):
if A[j] & 1 << i:
count += 1
s += count * (N - count) * 2 % mod
if s >= mod:
s -= mod
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | MOD = int(1000000000.0 + 7)
class Solution:
def countBits(self, N, A):
ans = 0
for i in range(31, -1, -1):
setbits = 0
for j in A:
if j & 1 << i:
setbits += 1
ans += 2 * setbits * (N - setbits)
ans %= MOD
return ans | ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
i = 0
count = 0
while i < 32:
setbits = 0
for ele in A:
if ele & 1 << i:
setbits += 1
count += setbits * (N - setbits) * 2
i += 1
return count % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
arr = [0] * 35
for i in A:
pos = 0
while i:
if i & 1:
arr[pos] += 1
pos += 1
i = i >> 1
count = 0
for i in arr:
count = (count + i * (N - i) * 2) % 1000000007
return count | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
ans = 0
for bit in range(32):
count = 0
for i, el in enumerate(A):
if el & 1 << bit:
ans += (i - count) % (10**9 + 7)
count += 1
else:
ans += count % (10**9 + 7)
ans = ans % (10**9 + 7)
return ans * 2 % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, n, arr):
ans = 0
mod = 1000000000.0 + 7
for i in range(32):
zero, one = 0, 0
for num in arr:
if num & 1 << i:
one += 1
else:
zero += 1
ans += int(one * zero % mod)
ans = int(ans % mod)
return int(2 * ans % mod) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
m = 1000000007
cnt = 0
maxi = max(A)
z = len(bin(maxi)[2:])
li = [bin(x)[2:].zfill(z) for x in A]
l = [(0) for i in range(z)]
for x in li:
i = 0
for ch in x:
if ch == "0":
l[i] += 1
i += 1
for x in l:
cnt += 2 * x * (N - x) % m
return cnt % m | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
mod = 10**9 + 7
res = 0
for i in range(32):
cur = 1 << i
ones = 0
for j in A:
if j & cur:
ones += 1
res += 2 * ones * (N - ones)
return res % mod | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
max_ele = max(A)
pos = 0
while max_ele > 0:
max_ele = max_ele >> 1
pos += 1
final_count = 0
p = 10**9 + 7
for i in range(pos):
zero_count = 0
mask = 1 << i
for ele in A:
if ele & mask == 0:
zero_count += 1
final_count = (final_count + 2 * zero_count * (N - zero_count)) % p
return final_count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
c = 0
m = pow(10, 9) + 7
for i in range(32):
set0 = 0
set1 = 0
for i in range(N):
if A[i] & 1:
set1 += 1
else:
set0 += 1
A[i] = A[i] >> 1
c += set0 * set1 * 2
return c % m | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
ans = 0
for i in range(31, -1, -1):
count_1 = 0
count_0 = 0
for ele in A:
if ele & 1 << i:
count_1 += 1
else:
count_0 += 1
ans = ans + count_1 * count_0
return ans * 2 % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum of f(A_{i}, A_{j}) for all ordered pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 10^{9}+7.
Example 1:
Input: N = 2
A = {2, 4}
Output: 4
Explaintion: We return
f(2, 2) + f(2, 4) +
f(4, 2) + f(4, 4) =
0 + 2 +
2 + 0 = 4.
Example 2:
Input: N = 3
A = {1, 3, 5}
Output: 8
Explaination: We return
f(1, 1) + f(1, 3) + f(1, 5) +
f(3, 1) + f(3, 3) + f(3, 5) +
f(5, 1) + f(5, 3) + f(5, 5) =
0 + 1 + 1 +
1 + 0 + 2 +
1 + 2 + 0 = 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function countBits() which takes the value N and the array A as input parameters and returns the desired count modulo 10^{9}+7.
Expected Time Complexity: O(N * log_{2}(Max(A_{i})))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
2^{0} ≤ A[i] < 2^{31} | class Solution:
def countBits(self, N, A):
res = 0
MOD = 10**9 + 7
for i in range(32):
bt = 1 << i
c = 0
for v in A:
if v & bt:
c += 1
res = (res + 2 * c * (N - c)) % MOD
return res % MOD | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.