description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 ≤ q ≤ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 ≤ v, u ≤ 10^18, v ≠ u, 1 ≤ w ≤ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | def main():
d = {}
for _ in range(int(input())):
c, *l = input().split()
if c == "1":
v, u, w = map(int, l)
while u != v:
if u < v:
d[v] = d.get(v, 0) + w
u, v = v // 2, u
else:
d[u] = d.get(u, 0) + w
u //= 2
else:
res = 0
v, u = map(int, l)
while u != v:
if u < v:
res += d.get(v, 0)
u, v = v // 2, u
else:
res += d.get(u, 0)
u //= 2
print(res)
main() | FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
Example 1:
Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2]
Output: False
Note:
The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. | class Solution:
def judgePoint24(self, nums):
bad = "对撒剘劥圞剜劏哱掶桺泛揋掵従剟剣彫寣污悫壛梄甏咍哲汭剤堧点卋嬞勆叛汬泐塵栋劚嚮咃宠吖剗楗囧力桻攋壯劯嗏桹劙剢剚焧啫栕炸栫栖嚲彳剛撑烃洿宋汷彲剙揁妷埻撧汢吩壙劇剭埼吕剝汣敯憇勇剥咎囻匓"
return chr(int("".join(map(str, sorted(nums)))) + 19968) not in bad | CLASS_DEF FUNC_DEF ASSIGN VAR STRING RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR |
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
Example 1:
Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2]
Output: False
Note:
The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. | class Solution(object):
def judgePoint24(self, nums):
def cal(operands, operaters):
for opt in operaters:
op2, op1 = operands.pop(-1), operands.pop(-1)
if opt == "+":
ret = op1 + op2
elif opt == "-":
ret = op1 - op2
elif opt == "*":
ret = op1 * op2
elif opt == "/" and op2 != 0:
ret = op1 / op2
else:
return float("-inf")
operands.append(ret)
return operands[0] if abs(operands[0] - 24) > 1e-07 else 24
def comb(arr):
if len(arr) == 1:
return [arr]
ret = []
for i in range(len(arr)):
if i - 1 >= 0 and arr[i] == arr[i - 1]:
continue
for remain in comb(arr[:i] + arr[i + 1 :]):
ret.append([arr[i]] + remain)
return ret
def genopt(n):
if n == 0:
return [""]
ret = []
for i in "+-*/":
for res in genopt(n - 1):
ret.append(i + res)
return ret
nums.sort()
ops = comb(nums)
opts = genopt(3)
for op in ops:
for opt in opts:
if cal(op[:], opt) == 24:
return True
def base(a, b):
return set(
[a + b, a - b, b - a, a * b]
+ ([a / b] if b != 0 else [])
+ ([b / a] if a != 0 else [])
)
for i in range(4):
for j in range(i + 1, 4):
a, b = nums[i], nums[j]
remain = nums[:i] + nums[i + 1 : j] + nums[j + 1 : 4]
for x in base(a, b):
for y in base(remain[0], remain[1]):
if 24 in base(x, y):
return True
return False | CLASS_DEF VAR FUNC_DEF FUNC_DEF FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN LIST STRING ASSIGN VAR LIST FOR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP LIST BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER LIST BIN_OP VAR VAR LIST VAR NUMBER LIST BIN_OP VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
Example 1:
Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2]
Output: False
Note:
The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. | class Solution(object):
def judgePoint24(self, nums):
n = len(nums)
if n == 1:
return abs(nums[0] - 24) < 0.0001
for i in range(n - 1):
for j in range(i + 1, n):
remainder = nums[:i] + nums[i + 1 : j] + nums[j + 1 :]
if self.judgePoint24(remainder + [nums[i] + nums[j]]):
return True
if self.judgePoint24(remainder + [nums[i] - nums[j]]):
return True
if self.judgePoint24(remainder + [nums[j] - nums[i]]):
return True
if self.judgePoint24(remainder + [nums[i] * nums[j]]):
return True
if nums[j] != 0 and self.judgePoint24(remainder + [nums[i] / nums[j]]):
return True
if nums[i] != 0 and self.judgePoint24(remainder + [nums[j] / nums[i]]):
return True
return False | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def findAllcombinationSum(self, index, candidates, target, ans, ds):
if index == len(candidates):
if target == 0:
ans.append(ds[:])
return
return
if candidates[index] <= target and candidates[index] not in ds:
ds.append(candidates[index])
self.findAllcombinationSum(
index, candidates, target - candidates[index], ans, ds
)
ds.remove(candidates[index])
self.findAllcombinationSum(index + 1, candidates, target, ans, ds)
def combinationSum(self, k, target):
candidates = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans = []
self.findAllcombinationSum(0, candidates, target, ans, [])
mysubseq = []
for i in ans:
if len(i) == k:
mysubseq.append(i)
return mysubseq | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN RETURN IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, target):
arr = [i for i in range(1, 10)]
ans = []
dummy = []
self.answer(k, target, arr, ans, dummy, 0, 0)
return ans
def answer(self, k, target, arr, ans, dummy, index, ld):
if target == 0 and ld == k:
ans.append(list(dummy))
return
elif target < 0 or index > 8 or ld > k:
return
else:
for i in range(index, 9):
dummy.append(arr[i])
self.answer(k, target - arr[i], arr, ans, dummy, i + 1, ld + 1)
dummy.pop() | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, n):
res = []
candidates = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def sol(i, c, k, candidates, target):
if target == 0 and k == 0:
res.append(c.copy())
return
if k < 0 or target < 0 or i == 9:
return
c.append(candidates[i])
sol(i + 1, c, k - 1, candidates, target - candidates[i])
c.pop()
sol(i + 1, c, k, candidates, target)
sol(0, [], k, candidates, n)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER LIST VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
def find(K, target, i, temp):
if len(temp) == K and sum(temp) == target:
res.append(temp.copy())
elif len(temp) < K and sum(temp) < target:
for j in range(i + 1, 10):
if sum(temp) + j <= target:
temp.append(j)
find(K, target, j, temp)
temp.pop()
res = []
for i in range(1, 10):
find(K, target, i, [i])
return res | CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR LIST VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, target):
def helper(i, k, sumtill):
if k == 0:
if sumtill == target:
ans.append(subset.copy())
return
if sumtill > target:
return
if i > 9:
return
sumtill += i
subset.append(i)
helper(i + 1, k - 1, sumtill)
sumtill -= i
subset.pop()
helper(i + 1, k, sumtill)
ans = []
subset = []
sumtill = 0
helper(1, k, sumtill)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR VAR RETURN IF VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
result = []
def solve(pos, ds, total):
if len(ds) == K:
if total == target:
result.append(ds)
return
for i in range(pos, 9 + 1):
solve(i + 1, ds + [i], total + i)
solve(1, [], 0)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER LIST NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
inp_arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
res = []
def get_sum(inp, cursum, ans, tgt, cnt):
if cnt < K:
for i in range(len(inp)):
ele = inp[i]
if cnt + 1 == K and cursum + ele == tgt:
res.append(ans + [ele])
elif cursum + ele < tgt:
if i < len(inp) - 1:
get_sum(
inp[i + 1 :], cursum + ele, ans + [ele], tgt, cnt + 1
)
else:
break
get_sum(inp_arr, 0, [], target, 0)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER LIST VAR NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def gen(self, k, target, nums, arr, brr, pos, c):
if pos == 9 or c == k:
if target == 0 and c == k:
crr = brr.copy()
arr.add(tuple(crr))
return
if target < 0:
return
brr.append(nums[pos])
self.gen(k, target - nums[pos], nums, arr, brr, pos + 1, c + 1)
brr.pop()
self.gen(k, target, nums, arr, brr, pos + 1, c)
def combinationSum(self, K, target):
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr = set()
brr = []
self.gen(K, target, nums, arr, brr, 0, 0)
return sorted(list(arr)) | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def find(self, i, p, K, target, l, l1):
if p == K and target == 0:
l.append(l1.copy())
return
if p == K:
return
if target == 0:
return
if i == 10:
return
l1.append(i)
self.find(i + 1, p + 1, K, target - i, l, l1)
l1.pop()
self.find(i + 1, p, K, target, l, l1)
def combinationSum(self, K, target):
l = []
l1 = []
self.find(1, 0, K, target, l, l1)
return l | CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR VAR RETURN IF VAR NUMBER RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def solve(self, k, n, ans, lis, num):
if len(lis) == k:
if n == 0:
ans.append(tuple(lis.copy()))
return
for i in range(num, 10):
lis.append(i)
self.solve(k, n - i, ans, lis, i + 1)
lis.pop()
def combinationSum(self, K, target):
ans = []
self.solve(K, target, ans, [], 1)
return ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR LIST NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, target):
arr = [i for i in range(1, 10)]
res = []
def backtrack(subset, idx, sum, count):
if count == k and sum == target:
res.append(subset[:])
return
if idx >= len(arr) or count > k or target < 0:
return
subset.append(arr[idx])
backtrack(subset, idx + 1, sum + arr[idx], count + 1)
subset.pop()
backtrack(subset, idx + 1, sum, count)
backtrack([], 0, 0, 0)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | from itertools import combinations
class Solution:
def combinationSum(self, K, target):
from itertools import combinations
x = combinations([1, 2, 3, 4, 5, 6, 7, 8, 9], K)
y = []
for i in x:
if sum(i) == target:
y.append(i)
return y | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
res = []
ds = []
ans = []
for i in range(1, 10):
ds.append(i)
def getAns(ind, tar, ds, ans, res):
if ind == len(ds):
if len(ans) == K and tar == 0:
res.append(ans[:])
return
if ds[ind] <= tar:
ans.append(ds[ind])
getAns(ind + 1, tar - ds[ind], ds, ans, res)
ans.pop()
getAns(ind + 1, tar, ds, ans, res)
getAns(0, target, ds, ans, res)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
def helper(target, k, start, path, res):
if target == 0 and k == 0:
res.append(path)
return
if target < 0 or k < 0:
return
for i in range(start, 10):
helper(target - i, k - 1, i + 1, path + [i], res)
res = []
helper(target, K, 1, [], res)
return res | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER LIST VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
def combi(arr, p, target, sol, k, i):
if target == 0 and len(p) == k:
sol.append(p)
return
if target < 0 or len(p) > k or i == 9:
return
combi(arr, p + [arr[i]], target - arr[i], sol, k, i + 1)
combi(arr, p, target, sol, k, i + 1)
sol = []
k = K
combi([i for i in range(1, 10)], [], target, sol, k, 0)
return sol | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER LIST VAR VAR VAR NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def solve(self, ans, output, index, nums, K, target):
if index == len(nums):
if K == 0 and target == 0:
ans.append(output)
return
if K == 0 and target == 0:
ans.append(output)
return
self.solve(ans, output, index + 1, nums, K, target)
self.solve(
ans, output + [nums[index]], index + 1, nums, K - 1, target - nums[index]
)
def combinationSum(self, K, target):
nums = [i for i in range(1, 10)]
ans = []
self.solve(ans, [], 0, nums, K, target)
ans.sort()
return ans | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
nums = [(i + 1) for i in range(9)]
ans = []
def dfs(pos, res, total):
if total == target and len(res) == K:
ans.append(res.copy())
if total > target or pos >= len(nums) or len(res) > K:
return
for i in range(pos, 9):
res.append(nums[i])
dfs(i + 1, res, total + nums[i])
res.pop()
dfs(0, [], 0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
ans = []
arr = []
numlist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def funcall(index, sumtillnow, arr):
if len(arr) == K:
if sumtillnow == target:
ans.append(arr.copy())
return
if index < len(numlist):
for i in range(index, len(numlist)):
arr.append(numlist[i])
funcall(i + 1, sumtillnow + numlist[i], arr)
arr.pop()
funcall(0, 0, arr)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def __init__(self):
self.ans = []
def combinationSum(self, K, target):
self.util(range(1, 10), target, [], 0, 0)
return [k for k in self.ans if len(k) == K]
def util(self, A, B, elements, i, sumso):
if i == len(A):
return
if sumso + A[i] > B:
return
elif sumso + A[i] == B:
self.ans.append([*elements, A[i]])
else:
self.util(A, B, [*elements, A[i]], i + 1, sumso + A[i])
self.util(A, B, elements, i + 1, sumso) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR LIST NUMBER NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR VAR VAR VAR RETURN IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
k = K
ans = []
sum = target
temp = []
def combination(arr, sum, ans, temp, index, k):
if sum == 0 and len(temp) == k:
ans.append(temp.copy())
return
if len(temp) == k:
return
for i in range(index, len(arr)):
if sum - arr[index] >= 0:
temp.append(arr[i])
combination(arr, sum - arr[i], ans, temp, i + 1, k)
temp.remove(arr[i])
combination(arr, sum, ans, temp, 0, k)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def solve(self, i, Sum, k, temp, ans):
if Sum == 0 and k == 0:
ans.append(temp)
print(ans)
return
if k == 0 or Sum > i:
return
if i == 10:
return
if Sum - i >= 0:
temp.append(i)
self.solve(i + 1, Sum - i, k - 1, temp, ans)
temp.pop()
self.solve(i + 1, Sum, k, temo, ans)
def combinationSum(self, K, target):
def combi(arr, p, target, sol, k, i):
if target == 0 and len(p) == k:
sol.append(p)
return
if target < 0 or len(p) > k or i == 9:
return
combi(arr, p + [arr[i]], target - arr[i], sol, k, i + 1)
combi(arr, p, target, sol, k, i + 1)
sol = []
k = K
combi([i for i in range(1, 10)], [], target, sol, k, 0)
return sol | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR VAR RETURN IF VAR NUMBER RETURN IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER LIST VAR VAR VAR NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | def combisum2(i, target, subs, arr, ans, n, num, k):
if i == n:
if target == 0 and k == num:
ans.append(subs[:])
return
if target - arr[i] >= 0:
subs.append(arr[i])
num += 1
combisum2(i + 1, target - arr[i], subs, arr, ans, n, num, k)
num -= 1
subs.pop()
while i < n - 1 and arr[i] == arr[i + 1]:
i += 1
combisum2(i + 1, target, subs, arr, ans, n, num, k)
class Solution:
def combinationSum(self, k, N):
candidates = []
for i in range(9):
candidates.append(i + 1)
ans = []
subs = []
i = 0
num = 0
combisum2(i, N, subs, candidates, ans, 9, num, k)
return ans | FUNC_DEF IF VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, target):
results = []
candidates = list(range(1, 10))
def backtrack(start, target, combination):
if target == 0 and len(combination) == k:
results.append(combination)
return
for i in range(start, len(candidates)):
if candidates[i] > target:
break
if i > start and candidates[i] == candidates[i - 1]:
continue
backtrack(i + 1, target - candidates[i], combination + [candidates[i]])
backtrack(0, N, [])
return results | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER VAR LIST RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, n):
ans = []
res = []
def backtrack(num, res, target):
if len(res) == k:
if target == 0:
ans.append(res.copy())
return
for i in range(num + 1, 10):
if i <= target:
backtrack(i, res + [i], target - i)
else:
return
backtrack(0, res, n)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def helper(self, arr, K, target, sum1, list1, count):
if arr == []:
return
if count > K:
return
if sum1 > target:
return
if sum1 == target:
if count == K:
self.res.append(list1)
return
self.helper(arr[1:], K, target, sum1 + arr[0], list1 + [arr[0]], count + 1)
self.helper(arr[1:], K, target, sum1, list1, count)
def combinationSum(self, K, target):
self.res = []
arr = [i for i in range(1, 11)]
self.helper(arr, K, target, 0, [], 0)
return self.res | CLASS_DEF FUNC_DEF IF VAR LIST RETURN IF VAR VAR RETURN IF VAR VAR RETURN IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR LIST VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER LIST NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
ans = []
li = []
def helper(i, k, target, ans, li):
if k == 0:
if target == 0:
ans.append(list(li))
return
if i > 9:
return
li.append(i)
helper(i + 1, k - 1, target - i, ans, li)
li.pop()
helper(i + 1, k, target, ans, li)
return
helper(1, K, target, ans, li)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr, combinations = [], []
self.noOfCombinations(0, target, arr, combinations, nums, K)
return combinations
def noOfCombinations(self, n, target, arr, combinations, nums, k):
if target == 0 and len(arr) == k:
temp = [ele for ele in arr]
combinations.append(temp)
return
if n == len(nums):
return
if nums[n] <= target:
arr.append(nums[n])
self.noOfCombinations(n + 1, target - nums[n], arr, combinations, nums, k)
arr.pop()
self.noOfCombinations(n + 1, target, arr, combinations, nums, k) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
List = []
def recur(arr, summ, count, k, target, val):
if summ > target or count > k:
return
if summ == target and count == k:
List.append(arr[:])
return
if val > 9:
return
arr.append(val)
recur(arr, summ + val, count + 1, k, target, val + 1)
arr.pop()
recur(arr, summ, count, k, target, val + 1)
recur([], 0, 0, K, target, 1)
return List | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER VAR VAR NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
a = list(range(1, 10))
def recurs(i, sums, vals):
nonlocal res
if sums == target and len(vals) == K and vals.copy() not in res:
res.append(vals.copy())
return
if sums > target or len(vals) > K or i >= len(a):
return
vals.append(a[i])
recurs(i + 1, sums + a[i], vals)
vals.pop()
recurs(i + 1, sums, vals)
res = []
recurs(0, 0, [])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
setti = set([(0, ())])
output = []
for i in range(1, 10):
temp = set()
for item in setti:
summa = i + item[0]
items = item[1] + (i,)
if len(items) == K and summa == target:
output.append(items)
elif summa < target and len(items) < K:
temp.add((summa, items))
setti.update(temp)
return sorted(output) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | def rec(i, ans, res, target, K):
if K == 0 and target == 0:
ans.append(res.copy())
if K == 0 or target < 0:
return
for j in range(i, 10):
res.append(j)
rec(j + 1, ans, res, target - j, K - 1)
res.pop()
class Solution:
def combinationSum(self, K, target):
ans = []
res = []
rec(1, ans, res, target, K)
return ans | FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, n):
ans = []
def dfs(path, nums, k, n):
if k < 0:
return -1
if k == 0 and n == 0:
ans.append(path[:])
return
for i in range(len(nums)):
dfs(path + [nums[i]], nums[i + 1 :], k - 1, n - nums[i])
dfs([], list(range(1, 10)), k, n)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, target):
def calc(s, ind, temp):
if s == 0:
if len(temp) == k:
ans.append(temp[:])
return
return
for i in range(ind, len(arr)):
if i > ind and arr[i] == arr[i - 1]:
continue
if arr[i] > s:
break
temp.append(arr[i])
calc(s - arr[i], i + 1, temp)
temp.pop()
ans = []
arr = [i for i in range(1, 10)]
arr.sort()
calc(target, 0, [])
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER LIST RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | def bt(k, n):
times = {i: (1) for i in range(1, 10)}
res = []
helper(k, n, [], times, res)
return res
def helper(k, n, temp, times, res):
if n == 0 and len(temp) == k:
res.append(temp.copy())
for num in times:
if not temp and times[num] > 0 and n - num >= 0 and len(temp) <= k:
times[num] -= 1
temp.append(num)
helper(k, n - num, temp, times, res)
times[num] += 1
temp.remove(num)
if (
temp
and times[num] > 0
and n - num >= 0
and len(temp) <= k
and temp[-1] < num
):
times[num] -= 1
temp.append(num)
helper(k, n - num, temp, times, res)
times[num] += 1
temp.remove(num)
class Solution:
def combinationSum(self, K, target):
return bt(K, target) | FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def com(i, N, K, target, arr, count, res, s, l):
if s == target and count == K:
res[tuple(l)] = 1
return
if i == N:
return
if count > K:
return
l.append(arr[i])
s += arr[i]
count += 1
com(i + 1, N, K, target, arr, count, res, s, l)
l.pop()
s -= arr[i]
count -= 1
com(i + 1, N, K, target, arr, count, res, s, l)
count = 0
res = {}
com(0, 9, K, target, arr, count, res, 0, [])
final = []
for key in res:
final.append(list(key))
res.clear()
final.sort()
return final | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR RETURN IF VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def solve(self, prev, K, N, ans, lst=[]):
if K == 0:
if N == 0:
ans.append(lst.copy())
return
if N < 0 or K < 0:
return
if prev > 9:
return
for i in range(prev, 10):
lst.append(i)
self.solve(i + 1, K - 1, N - i, ans, lst)
lst.pop()
def combinationSum(self, K, target):
ans = []
self.solve(1, K, target, ans)
return ans | CLASS_DEF FUNC_DEF LIST IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR NUMBER VAR NUMBER RETURN IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
res = []
op = []
start = 0
self.comb(res, op, start, 1, K, target)
return res
def comb(self, res, op, start, i, K, target):
if start == K and target == 0:
res.append(op.copy())
if target == 0:
return
if start == K:
return
if i == 10:
return
op.append(i)
self.comb(res, op, start + 1, i + 1, K, target - i)
op.pop()
self.comb(res, op, start, i + 1, K, target) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN IF VAR VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
def dfs(idx, path, cur, size):
if cur > target or size > K:
return
if cur == target:
if size == K:
ans.append(path.copy())
return
for i in range(idx, 10):
dfs(i + 1, path + [i], cur + i, size + 1)
ans = []
dfs(1, [], 0, 0)
return sorted(ans) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER RETURN FUNC_CALL VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | def munny(arr, ans, res, ind, target, K, sum1, co):
if sum1 > target:
return
if sum1 == target and co == K:
res.append(ans)
return
if co >= K:
return
if ind > 8:
return
ans.append(arr[ind])
munny(arr, ans.copy(), res, ind + 1, target, K, sum1 + arr[ind], co + 1)
ans.pop()
munny(arr, ans.copy(), res, ind + 1, target, K, sum1, co)
class Solution:
def combinationSum(self, K, target):
ans = []
res = []
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
munny(arr, ans, res, 0, target, K, 0, 0)
return res | FUNC_DEF IF VAR VAR RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
res = []
self.dfs(res, arr, K, target, [])
return res
def dfs(self, res, arr, k, target, path):
if target < 0 or k < 0:
return
elif target == 0 and k == 0:
res.append(path)
return
for i in range(len(arr)):
self.dfs(res, arr[i + 1 :], k - 1, target - arr[i], path + [arr[i]]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR LIST RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR LIST VAR VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | from itertools import combinations
class Solution:
def combinationSum(self, K, target):
from itertools import combinations
l = []
c = combinations(range(1, 10), K)
s = []
for i in c:
if sum(i) == target:
s.append(i)
return s | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def solve(self, K, start, sum, ans, store):
if K == 0 and sum == 0:
store.append(ans)
return
if K <= 0:
return
for i in range(start, 10):
if sum - i >= 0:
self.solve(K - 1, i + 1, sum - i, ans + [i], store)
def combinationSum(self, K, target):
store = []
self.solve(K, 1, target, [], store)
return store | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR LIST VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def findAllcombinationSum(self, index, candidates, target, ans, ds, k):
if index == len(candidates):
if target == 0 and len(ds) == k:
ans.append(ds[:])
return
return
ds.append(candidates[index])
self.findAllcombinationSum(
index + 1, candidates, target - candidates[index], ans, ds, k
)
ds.remove(candidates[index])
self.findAllcombinationSum(index + 1, candidates, target, ans, ds, k)
def combinationSum(self, k, target):
candidates = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans = []
self.findAllcombinationSum(0, candidates, target, ans, [], k)
return ans | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR LIST VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
def solve(idx, arr, target, K, l, ans):
if target == 0 and len(l) == K:
ans.append(l.copy())
return
for i in range(idx, len(arr)):
if i > idx and arr[i] == arr[i - 1]:
continue
if arr[i] > target:
break
l.append(arr[i])
solve(i + 1, arr, target - arr[i], K, l, ans)
l.pop()
arr = [i for i in range(1, 10)]
ans = []
solve(0, arr, target, K, [], ans)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR LIST VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, k, n):
cand = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ds = []
a = []
cand.sort()
ind = 0
def comb(ind, arr, t, a, ds):
if t == 0 and len(ds) == k:
a.append(ds.copy())
return
if t < 0:
return
if len(ds) > k:
return
for i in range(ind, len(arr)):
if i > ind and arr[i] == arr[i - 1]:
continue
if arr[i] > n:
break
ds.append(arr[i])
comb(i + 1, arr, t - arr[i], a, ds)
ds.remove(arr[i])
comb(ind, cand, n, a, ds)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR NUMBER RETURN IF FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
def rec(ind, ans, ds, s, k, target):
if len(ds) == k and s[0] == target:
ans.append(ds.copy())
return
if ind > 9:
return
ds.append(ind)
s[0] += ind
rec(ind + 1, ans, ds, s, k, target)
s[0] -= ind
ds.pop()
rec(ind + 1, ans, ds, s, k, target)
ans = []
ds = []
s = [0]
rec(1, ans, ds, s, K, target)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combsum(self, k, arr, index, seq, target):
if k == 0:
if target == 0:
self.res.append(seq)
return
if index == len(arr):
return
self.combsum(k - 1, arr, index + 1, seq + [arr[index]], target - arr[index])
self.combsum(k, arr, index + 1, seq, target)
def combinationSum(self, K, target):
self.res = []
self.combsum(K, list(range(1, 10)), 0, [], target)
return self.res | CLASS_DEF FUNC_DEF IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER LIST VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
res = []
def backtracking(ind, total, curr):
if len(curr[:]) == K and total == 0:
res.append(curr[:])
return
if len(curr[:]) > K or total < 0:
return
if ind >= 10:
return
for i in range(ind, 10):
curr.append(i)
backtracking(i + 1, total - i, curr)
curr.pop()
backtracking(1, target, [])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR LIST RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target, start=1, till=[]):
if K == 1:
if target >= start:
if target < 10:
return [[*till, target]]
else:
return []
else:
return []
ans = []
for x in range(start, 10):
res = self.combinationSum(K - 1, target - x, x + 1, [*till, x])
ans += res
return ans | CLASS_DEF FUNC_DEF NUMBER LIST IF VAR NUMBER IF VAR VAR IF VAR NUMBER RETURN LIST LIST VAR VAR RETURN LIST RETURN LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER LIST VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
whole = []
def recsum(temp, ans, i):
whole.append(temp[:])
if len(temp) == K and target == sum(temp):
ans.append(temp[:])
return
for j in range(i, 10):
if len(temp) < K and j not in temp and (len(temp) == 0 or temp[-1] < j):
temp.append(j)
recsum(temp, ans, i + 1)
temp.pop()
else:
continue
return ans
return recsum([], [], 1) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR RETURN FUNC_CALL VAR LIST LIST NUMBER |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
if target > 45:
return []
ans = []
def solve(temp, i):
if len(temp) == K and sum(temp) == target:
ans.append(temp.copy())
return
for j in range(i, 10):
temp.append(j)
solve(temp, j + 1)
temp.pop()
solve([], 1)
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
if K > target:
return []
options = 1, 2, 3, 4, 5, 6, 7, 8, 9
res = []
def helper(idx, sumi, temp):
if sumi > target:
return
if len(temp) == K:
if sumi == target:
res.append(temp.copy())
return
for i in range(idx, len(options)):
temp.append(options[i])
helper(i + 1, sumi + options[i], temp)
temp.pop()
helper(0, 0, [])
return res | CLASS_DEF FUNC_DEF IF VAR VAR RETURN LIST ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def combinationSum(self, K, target):
valid = []
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def ans(ind, arr, K, target, temp):
if len(temp) == K or target == 0:
if target == 0 and len(temp) == K:
valid.append(temp[:])
return
for i in range(ind, 9):
if target < arr[i]:
break
temp.append(arr[i])
ans(i + 1, arr, K, target - arr[i], temp)
temp.pop()
temp = []
ans(0, arr, K, target, temp)
return valid | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | class Solution:
def solve(self, k, target, i, lst, ans):
if len(lst) == k or i > 9:
if target == 0 and len(lst) == k:
ans.append(tuple(lst))
return
lst.append(i)
self.solve(k, target - i, i + 1, lst, ans)
lst.pop()
self.solve(k, target, i + 1, lst, ans)
def combinationSum(self, K, target):
ans = []
lst = []
i = 1
self.solve(K, target, i, lst, ans)
return ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR |
Find all valid combinations of K numbers that sum upto to N such that the following conditions are true:
Only number 1 through 9 are used.
Each number is used atmost once.
Return the list of all possible valid combinations.
Note: The list must not contain the same combination twice, and the combinations returned in any order.
Example 1:
Input:
K = 3
N = 7
Output: { {1, 2, 4} }
Explanation:
1+ 2+ 4 = 7
There are no other valid combinations.
Example 2:
Input:
K = 3
N = 9
Output: { {1, 2, 6}, {1, 3, 5}, {2, 3, 4} }
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
Example 3:
Input:
K = 4
N = 3
Output: { { } }
Explanation: There are no valid combinations.
Using 4 different numbers in the range {1, 9}, the smallest sum we can get is 1 + 2 + 3 + 4 = 10 and since 10 > 3, there are no valid combinations.
Your Task:
You don't need to read input or print anything. Your task is to complete the function combinationSum() which takes the integers K and N as parameters and returns a list of all possible valid combinations.
Expected Time Complexity: O(2^{K})
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ K ≤ 9
^{1 }≤ N_{ }≤ 60 | from itertools import combinations
class Solution:
def combinationSum(self, K, target):
ans = []
arr = range(1, 10)
comb = combinations(arr, K)
for i in comb:
if sum(i) == target:
ans.append(i)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = list(map(int, input().split()))
even = {}
odd = {}
for i in range(len(a)):
if a[i] % 2 == 0:
if a[i] in even:
even[a[i]] += 1
else:
even[a[i]] = 1
elif a[i] in odd:
odd[a[i]] += 1
else:
odd[a[i]] = 1
count = 0
count2 = 0
for keys, values in even.items():
for i in range(33):
p = int(pow(2, i)) - keys
if p > 0:
if p in even:
if keys == p:
count += values * (values - 1) // 2
else:
count2 += values * even[p]
for keys, values in odd.items():
for i in range(33):
p = int(pow(2, i)) - keys
if p > 0:
if p in odd:
if keys == p:
count += values * (values - 1) // 2
else:
count2 += values * odd[p]
print(count + count2 // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = list(map(int, input().split()))
M = dict()
res = 0
for i in range(n):
for j in range(31):
res += M.get(2**j - a[i], 0)
M[a[i]] = M.get(a[i], 0) + 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
l = list(map(int, input().split()))
twos = set()
for i in range(1, 36):
twos.add(2**i)
dic = {}
for i in range(len(l)):
if l[i] not in dic:
dic[l[i]] = 1
else:
dic[l[i]] += 1
ans = 0
for i in dic:
for two in twos:
x = two - i
if x > 0:
if x in dic:
if x == i:
ans += dic[i] * (dic[i] - 1)
else:
ans += dic[i] * dic[x]
print(int(ans / 2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | from sys import stdin, stdout
n = int(stdin.readline())
power = [(2**i) for i in range(31)]
d = {}
ans = 0
for value in map(int, stdin.readline().split()):
for m in power:
if m >= value and m - value in d:
ans += d[m - value]
if value in d:
d[value] += 1
else:
d[value] = 1
stdout.write(str(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
s = sum(arr)
d = {}
ans = 0
for i in arr:
start = 1
while start <= s:
if start - i in d:
ans += d[start - i]
start *= 2
if i in d:
d[i] += 1
else:
d[i] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
d = dict()
for i in input().split():
if int(i) in d:
d[int(i)] += 1
else:
d[int(i)] = 1
mylist = []
ans = 0
for a in range(1, 31):
mylist.append(2**a)
for a in d:
for b in mylist:
if b - a in d:
if b - a != a:
ans += d[b - a] * d[a]
else:
ans += (d[a] - 1) * d[a]
print(ans // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input().strip())
a = list(map(int, input().strip().split()))
res = 0
countVal = {}
for i in range(n):
pow2 = 1
for j in range(30):
pow2 *= 2
if pow2 > a[i]:
remain = pow2 - a[i]
if remain in countVal:
res += countVal[remain]
if a[i] in countVal:
countVal[a[i]] += 1
else:
countVal[a[i]] = 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | freq = {}
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for x in a:
for b in range(1, 30 + 1):
ans += freq.get(2**b - x, 0)
freq[x] = freq.get(x, 0) + 1
print(ans) | ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | twoPowers = [(2 << x) for x in range(32)]
numbers = {}
n = input()
for x in [int(x) for x in input().split()]:
if x not in numbers:
numbers[x] = 0
numbers[x] += 1
sum = 0
for twoPower in twoPowers:
for key in numbers:
if key <= twoPower >> 1:
oppositeKey = twoPower - key
if key == oppositeKey:
sum += numbers[key] * (numbers[key] - 1) // 2
elif oppositeKey in numbers:
sum += numbers[key] * numbers[oppositeKey]
print(sum) | ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | input()
c = {}
s = 0
for i in map(int, input().split()):
for p in range(32):
s += c.get((1 << p) - i, 0)
c[i] = c.get(i, 0) + 1
print(s) | EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
l = list(map(int, input().split()))
twos = []
twos.append(1)
freq = {}
while twos[-1] < int(1000000000.0):
twos.append(twos[-1] * 2)
for i in range(n):
freq[l[i]] = 0
for i in range(n):
freq[l[i]] += 1
answer = 0
for i in range(0, l.__len__()):
for j in range(0, twos.__len__()):
if twos[j] >= l[i] and twos[j] - l[i] in freq and twos[j] - l[i] != l[i]:
answer += freq[twos[j] - l[i]]
elif twos[j] >= l[i] and twos[j] - l[i] in freq and twos[j] - l[i] == l[i]:
answer += freq[twos[j] - l[i]] - 1
print(answer // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT WHILE VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | def answer(n, A):
count = 0
d = {A[0]: 1}
for i in range(1, n):
for j in range(1, 32):
if 2**j - A[i] in d:
count += d[2**j - A[i]]
if A[i] in d:
d[A[i]] += 1
else:
d[A[i]] = 1
return count
n = int(input())
arr = list(map(int, input().split()))
print(answer(n, arr)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
s = 0
k = 0
a = list(map(int, input().split()))
d = {}
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
for i in set(a):
for j in range(32):
if 1 << j == 2 * i and i in d:
s += d[i] * (d[i] - 1) // 2
elif (1 << j) - i in d:
k += d[i] * d[(1 << j) - i]
print(s + k // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
arr = input().split()
arr = [int(i) for i in arr]
d = {}
for a in arr:
if a in d:
d[a] += 1
else:
d[a] = 1
count = 0
for a in arr:
d[a] -= 1
for n in range(1, 32):
cp = 2**n
if cp <= a:
continue
if cp - a in d:
count += d[cp - a]
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
m = {}
for x in list(map(int, input().split())):
if x in m:
m[x] += 1
else:
m[x] = 1
ans = 0
for x in range(0, 31):
p2 = 2**x
for i in m.keys():
if p2 - i in m:
if p2 - i == i:
ans += m[i] * (m[i] - 1)
else:
ans += m[i] * m[p2 - i]
print(ans // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | input()
ans = 0
s = dict()
for x in map(int, input().split()):
for i in range(1, 32):
if (1 << i) - x in s:
ans += s[(1 << i) - x]
if x in s:
s[x] += 1
else:
s[x] = 1
print(ans) | EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | R = lambda: map(int, input().split())
n = int(input())
res = 0
vsts = {}
for num in R():
res += sum(vsts.get((1 << pwr) - num, 0) for pwr in range(32))
vsts[num] = vsts.get(num, 0) + 1
print(res) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | from sys import stdin, stdout
def main():
from sys import stdin, stdout
n = int(stdin.readline())
t = tuple(map(int, stdin.readline().split()))
dic = {}
asum = 0
for i in t:
for j in range(32):
x = (1 << j) - i
if x in dic:
asum += dic[x]
if i not in dic:
dic[i] = 1
else:
dic[i] += 1
stdout.write(str(asum))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | input()
sequence = list(map(int, input().split()))
sequence.sort()
repeated_numbers = {}
exponent = 1
sum = 0
for number in sequence:
while 2**exponent <= number:
exponent += 1
sum += repeated_numbers.get(2**exponent - number, 0)
repeated_numbers[number] = repeated_numbers.get(number, 0) + 1
print(sum) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
numbers = list(map(int, input().split()))
sced = set(numbers)
counter = {}
for k in numbers:
if k in counter:
counter[k] += 1
else:
counter[k] = 1
x = 1
ans = 0
for i in range(32):
for j in sced:
if j <= x:
if j == x:
ans += counter[x] * (counter[x] - 1) // 2
elif x * 2 - j in counter:
ans += counter[j] * counter[x * 2 - j]
x *= 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
s = list(map(int, input().split()))
g = {}
ans = 0
for i in s:
for j in range(1, 33):
check = 1 << j
if check - i in g:
ans += g[check - i]
if i in g:
g[i] += 1
else:
g[i] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | input()
a = list(map(int, input().split()))
a = sorted(a)
total = 0
d = {}
for i in range(len(a)):
if a[i] in d.keys():
d[a[i]] += 1
else:
d[a[i]] = 1
values = set([(2**i) for i in range(1, 33)])
for i in range(len(a)):
for val in values:
diff = val - a[i]
if diff in d.keys():
if diff == a[i]:
total += d[diff] - 1
else:
total += d[diff]
print(total // 2) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
ar = list(map(int, input().split()))
d = {}
for i in ar:
if i not in d:
d[i] = 1
else:
d[i] = d[i] + 1
ans = 0
for p in d:
for j in range(31):
x = 1 << j
if x == 2 * p:
ans += d[p] * (d[p] - 1)
elif x - p in d:
ans += d[p] * d[x - p]
print(ans // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | lst = [(2**i) for i in range(1, 34)]
s = dict()
n = int(input())
ss = [int(i) for i in input().split()]
sss = 0
for i in range(0, n):
for j in lst:
if j - ss[i] in s:
sss += s[j - ss[i]]
if ss[i] in s:
s[ss[i]] += 1
else:
s[ss[i]] = 1
print(sss) | ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = [int(x) for x in input().split()]
p2 = [(2**i) for i in range(31)]
d = {}
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
k = 0
for i in d:
for p in p2:
j = p - i
if j > i:
break
if j in d:
if i == j:
k += d[i] * (d[i] - 1) // 2
else:
k += d[i] * d[j]
print(k) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = [int(x) for x in input().split()]
st2 = [1]
while st2[-1] <= 10**10:
st2.append(st2[-1] * 2)
r = 0
d = {}
for i in range(n):
for s2 in st2:
if s2 - a[i] in d:
r += d[s2 - a[i]]
d[a[i]] = d.get(a[i], 0) + 1
print(r) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER WHILE VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | t = input
p = print
r = range
n = int(t())
a = list(map(int, t().split()))
c = 0
co = {x: (0) for x in range(1000000)}
po = [(2**x) for x in range(33)]
for i in range(n):
if a[i] in co:
c += co.get(a[i])
for j in range(len(po)):
if a[i] < po[j]:
co.update({(po[j] - a[i]): co.get(po[j] - a[i], 0) + 1})
p(c) | ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | input()
sequence = list(map(int, input().split()))
repeated_even_numbers = {}
repeated_odd_numbers = {}
sum = 0
for number in sequence:
if number % 2 == 0:
for index in range(31):
sum += repeated_even_numbers.get((1 << index) - number, 0)
repeated_even_numbers[number] = repeated_even_numbers.get(number, 0) + 1
else:
for index in range(31):
sum += repeated_odd_numbers.get((1 << index) - number, 0)
repeated_odd_numbers[number] = repeated_odd_numbers.get(number, 0) + 1
print(sum) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = map(int, input().split())
pow2 = []
x = 1
for i in range(32):
x <<= 1
pow2.append(x)
ans = 0
cnt = {}
for x in a:
for y in pow2:
need = y - x
if need in cnt:
ans += cnt[need]
cnt[x] = cnt.get(x, 0) + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | def binary(x):
b_left = -1
b_right = x - 1
b_power = power(mas[x])
while b_right != b_left:
b_middle = (b_left + b_right + 1) // 2
if mas[b_middle] + mas[x] >= b_power:
b_right = b_middle - 1
else:
b_left = b_middle
left_border = b_left
b_right = x
b_left = 0
while b_right != b_left:
b_middle = (b_left + b_right) // 2
if mas[b_middle] + mas[x] > b_power:
b_right = b_middle
else:
b_left = b_middle + 1
right_border = b_right
return right_border - left_border - 1
def power(x):
p_answer = 2
while x != 1:
x = x // 2
p_answer = p_answer * 2
return p_answer
N = int(input())
mas = [int(x) for x in input().split()]
mas.sort()
answer = 0
for i in range(N - 1, 0, -1):
answer = answer + binary(i)
print(answer) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
ar = list(map(int, input().split()))
ans = 0
s = {}
for x in ar:
for i in range(1, 32):
if 2**i - x in s:
ans += s[2**i - x]
s[x] = s.get(x, 0) + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
ai = list(map(int, input().split()))
nums = {}
ans = 0
for i in range(n):
num = ai[i]
length = len(bin(num)[2:])
for j in range(length, 31):
num2 = 2**j - num
num4 = 0
try:
num4 = nums[num2]
except:
num4 = 0
ans += num4
try:
nums[ai[i]] += 1
except:
nums[ai[i]] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = [int(x) for x in input().split()]
cnt = 0
D = {}
MAX = max(a)
for i in range(0, n):
D[a[i]] = D.get(a[i], 0) + 1
DK = D.keys()
for k in DK:
e0 = 0
while 1 << e0 <= k:
e0 += 1
for e in range(e0, 32):
pow2 = 1 << e
if pow2 - k > MAX:
break
nk = D.get(k, 0)
if k == pow2 - k:
cnt += nk * (nk - 1)
else:
np = D.get(pow2 - k, 0)
cnt += nk * np
print(cnt // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | import sys
n = int(input())
a = list(map(int, input().split()))
a.sort()
two = [(2**i) for i in range(32)]
cnt = 0
m = {}
for i in a:
for j in two:
cnt += m.get(j - i, 0)
m[i] = m.get(i, 0) + 1
print(cnt) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = list(map(int, input().split()))
dic = {}
t = 0
for i in range(len(a)):
sh = 0
while 2**sh <= a[i]:
sh += 1
sh -= 1
for j in range(sh, 31):
t += dic.get(2**j - a[i], 0)
dic[a[i]] = dic.get(a[i], 0) + 1
print(t) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = [int(i) for i in map(int, input().split())]
power = [1]
while power[-1] < 2 * 1000000000.0 + 1:
power.append(power[-1] * 2)
numbers = {str(i): (0) for i in set(a)}
for i in a:
numbers[str(i)] += 1
ans = 0
for i in a:
number = numbers[str(i)]
if numbers[str(i)] == 0:
continue
numbers[str(i)] = 0
for j in power:
if j <= i:
continue
need = j - i
if str(need) in numbers:
if i != need:
ans += numbers[str(need)] * number
else:
ans += number * (number - 1) // 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER WHILE VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | input()
arr = list(map(int, input().split()))
def sumK(arr, k):
m = {}
total = 0
for number in arr:
if number in m:
total += m[number]
compl = k - number
m[compl] = m.get(compl, 0) + 1
return total
k = 1
ans = 0
while k <= 2 * 10**9:
ans += sumK(arr, k)
k *= 2
print(ans) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
a = input().split()
for i in range(n):
a[i] = int(a[i])
a.sort()
found = {}
x = 1
out = 0
for j in a:
while 2**x <= j:
x += 1
left = 2**x - j
try:
out += found[left]
except:
pass
try:
found[j] += 1
except:
found[j] = 1
print(out) | 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 EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | def main():
input()
m = list(map(int, input().split()))
mp = dict()
ans = 0
MX = 1 << 32 + 1
for x in m:
cur = 1
while cur < x:
cur <<= 1
while cur < MX:
if cur - x in mp:
ans += mp[cur - x]
cur <<= 1
if x in mp:
mp[x] += 1
else:
mp[x] = 1
print(ans)
main() | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | import sys
input = sys.stdin.readline
print = sys.stdout.write
n = int(input())
ar = list(map(int, input().split()))
p2 = [(2**i) for i in range(1, 64)]
d = dict()
for x in ar:
try:
d[x] += 1
except:
d[x] = 1
ans = 0
for x in p2:
for el in set(ar):
if el * 2 != x:
try:
ans += d[el] * d[x - el]
except:
pass
else:
ans += d[el] * (d[el] - 1)
print(str(int(ans) // 2)) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).
-----Input-----
The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | n = int(input())
arr = list(map(int, input().split()))
p = []
cnt = 0
i = 2
while i < 10000000007:
p.append(i)
i *= 2
d = {}
for i in arr:
if i not in d:
d[i] = 0
d[i] += 1
cnt = 0
for i in arr:
d[i] -= 1
for j in p:
v = j - i
if v in d:
cnt += d[v]
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.