description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
tf_arr = [(True) for _ in range(len(transactions))]
new_transactions = []
for x in transactions:
x = x.split(",")
x[1] = int(x[1])
x[2] = int(x[2])
new_transactions.append(x)
def check(transaction, i, arr):
if transaction[2] > 1000:
tf_arr[i] = False
for j, e in enumerate(arr[i + 1 :]):
if (
transaction[0] == e[0]
and transaction[3] != e[3]
and abs(transaction[1] - e[1]) <= 60
):
tf_arr[i] = False
tf_arr[i + j + 1] = False
for i, t in enumerate(new_transactions):
check(t, i, new_transactions)
output = []
for i, truth in enumerate(tf_arr):
if not truth:
output.append(transactions[i])
return output | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Transaction:
def __init__(self, s):
params = s.split(",")
self.name, self.time, self.amount, self.city = (
params[0],
int(params[1]),
int(params[2]),
params[3],
)
def __str__(self):
return ",".join([self.name, str(self.time), str(self.amount), self.city])
class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
transactions = [Transaction(t) for t in transactions]
transactions.sort(key=lambda t: t.time)
trans_idxs = defaultdict(list)
for i, t in enumerate(transactions):
trans_idxs[t.name].append(i)
invalid = []
for name, idxs in list(trans_idxs.items()):
left = right = 0
for trans_idx in idxs:
t = transactions[trans_idx]
if t.amount > 1000:
invalid.append(str(t))
continue
while (
left <= len(idxs) - 2
and transactions[idxs[left]].time < t.time - 60
):
left += 1
while (
right <= len(idxs) - 2
and transactions[idxs[right + 1]].time < t.time + 60
):
right += 1
for j in range(left, right + 1):
if transactions[idxs[j]].city != t.city:
invalid.append(str(t))
break
return invalid | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL STRING LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
res = []
for i, t1 in enumerate(transactions):
item = t1.split(",")
val = int(item[2])
minute = int(item[1])
name = item[0]
city = item[-1]
if val > 1000:
res.append(t1)
continue
for j, t2 in enumerate(transactions):
if i != j:
item = t2.split(",")
val2 = int(item[2])
minute2 = int(item[1])
name2 = item[0]
city2 = item[-1]
if abs(minute2 - minute) <= 60 and name2 == name and city2 != city:
res.append(t1)
break
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
seen = {}
final = []
for i in transactions:
j = i.split(",")
name = j[0]
time = int(j[1])
amount = int(j[2])
city = j[3]
if name not in seen:
seen[name] = [[name, time, amount, city, i]]
else:
seen[name].append([name, time, amount, city, i])
for key in list(seen.keys()):
keys_by_time = {}
for i in range(len(seen[key])):
keys_by_time[seen[key][i][1]] = i
seen[key].append(keys_by_time)
for key in list(seen.keys()):
for i in range(len(seen[key]) - 1):
for j in range(i + 1, len(seen[key]) - 1):
current = seen[key][i]
nex = seen[key][j]
if current[3] != nex[3] and abs(current[1] - nex[1]) <= 60:
if current[4] not in final:
final.append(current[4])
if nex[4] not in final:
final.append(nex[4])
if seen[key][i][4] not in final and seen[key][i][2] > 1000:
final.append(seen[key][i][4])
return final | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution(object):
def invalidTransactions(self, transactions):
NAME, TIME, AMOUNT, CITY = list(range(4))
txrows = []
for row in transactions:
name, time, amount, city = row.split(",")
time = int(time)
amount = int(amount)
txrows.append((name, time, amount, city))
ans = []
for i, tx1 in enumerate(txrows):
if tx1[AMOUNT] >= 1000:
ans.append(transactions[i])
continue
for j, tx2 in enumerate(txrows):
if (
i != j
and abs(tx1[TIME] - tx2[TIME]) <= 60
and tx1[NAME] == tx2[NAME]
and tx1[CITY] != tx2[CITY]
):
ans.append(transactions[i])
break
return ans | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
temp = []
for t in transactions:
temp.append(t.split(","))
res = []
for i in range(len(temp)):
if int(temp[i][2]) > 1000 and ",".join(temp[i]) not in res:
res.append(",".join(temp[i]))
for j in range(i + 1, len(temp)):
if temp[i][0] == temp[j][0] and temp[i][3] != temp[j][3]:
if abs(int(temp[i][1]) - int(temp[j][1])) <= 60:
if ",".join(temp[i]) not in res:
res.append(",".join(temp[i]))
if ",".join(temp[j]) not in res:
res.append(",".join(temp[j]))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR IF FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
temp = []
for t in transactions:
temp.append(t.split(","))
temp = sorted(temp, key=lambda x: x[0])
res = set()
for i in range(len(temp)):
if int(temp[i][2]) > 1000:
res.add(",".join(temp[i]))
for j in range(i + 1, len(temp)):
if (
temp[i][0] == temp[j][0]
and temp[i][3] != temp[j][3]
and abs(int(temp[i][1]) - int(temp[j][1])) <= 60
):
res.add(",".join(temp[i]))
res.add(",".join(temp[j]))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
invalid = []
for i, t1 in enumerate(transactions):
name1, time1, amount1, city1 = t1.split(",")
if int(amount1) > 1000:
invalid.append(t1)
continue
for j, t2 in enumerate(transactions):
if i != j:
name2, time2, amount2, city2 = t2.split(",")
if (
name1 == name2
and city1 != city2
and abs(int(time1) - int(time2)) <= 60
):
invalid.append(t1)
break
return invalid | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
n = len(transactions)
invalid_check = [0] * n
for i in range(n):
i_info = transactions[i].split(",")
assert len(i_info) == 4
if int(i_info[2]) >= 1000:
invalid_check[i] = 1
for j in range(i + 1, n):
j_info = transactions[j].split(",")
assert len(j_info) == 4
if (
i_info[0] == j_info[0]
and i_info[3] != j_info[3]
and abs(int(i_info[1]) - int(j_info[1])) <= 60
):
invalid_check[i] = invalid_check[j] = 1
ret = []
for i in range(n):
if invalid_check[i]:
ret.append(transactions[i])
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
hash_table = {}
invalid = []
index = -1
lindex = []
for trans in transactions:
count_coma = 0
word = ""
name = ""
dl = []
index += 1
lenght = 0
ind = 0
for l in trans:
lenght += 1
if l == "," or lenght == len(trans):
count_coma += 1
if count_coma == 1:
name = word
elif count_coma == 2:
tl = [int(word)]
dl.append(tl)
elif count_coma == 3:
if int(word) > 1000:
ind = 1
else:
cl = [word]
dl.append(cl)
dl.append([index])
if name not in hash_table:
hash_table[name] = dl
if ind == 1:
invalid.append(trans)
lindex.append(index)
else:
if ind == 1:
invalid.append(trans)
lindex.append(index)
for i in range(0, len(hash_table.get(name)[0])):
if (
abs(hash_table.get(name)[0][i] - dl[0][0]) < 61
and hash_table.get(name)[1][i] != dl[1][0]
):
if index not in lindex:
invalid.append(trans)
lindex.append(index)
if hash_table.get(name)[2][i] not in lindex:
invalid.append(
transactions[hash_table.get(name)[2][i]]
)
lindex.append(hash_table.get(name)[2][i])
hash_table.get(name)[0].append(dl[0][0])
hash_table.get(name)[1].append(dl[1][0])
hash_table.get(name)[2].append(index)
word = ""
else:
word += l
return invalid | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR STRING VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
ret = set()
name = []
time = []
amount = []
city = []
for trans in transactions:
trans = trans.split(",")
name.append(trans[0])
time.append(int(trans[1]))
amount.append(int(trans[2]))
city.append(trans[3])
for i in range(len(transactions)):
if amount[i] > 1000:
ret.add(transactions[i])
for j in range(i + 1, len(transactions)):
if (
name[i] == name[j]
and abs(time[i] - time[j]) <= 60
and city[i] != city[j]
):
ret.add(transactions[i])
ret.add(transactions[j])
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
def giddy(item):
return item.split(",")
result = set()
seen = collections.defaultdict(list)
transactions = list(map(giddy, transactions))
for i in range(len(transactions)):
val = transactions[i]
if int(val[2]) > 1000:
result.add(",".join(val))
if val[0] in seen:
for x in seen[val[0]]:
comp = transactions[x]
if abs(int(comp[1]) - int(val[1])) <= 60 and comp[3] != val[3]:
result.add(",".join(val))
result.add(",".join(comp))
seen[val[0]].append(i)
return result | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
transactions = [string.split(",") for string in transactions]
ans, length = set(), len(transactions)
for i in range(length):
if int(transactions[i][2]) > 1000:
ans.add(",".join(transactions[i]))
dct = {}
for i in range(length):
name, time, city = (
transactions[i][0],
transactions[i][1],
transactions[i][-1],
)
if name in dct:
dct[name].append((time, city))
dct[name].sort()
else:
dct[name] = [(time, city)]
for name, time, amt, city in transactions:
for tme, cty in dct[name]:
if abs(int(time) - int(tme)) <= 60 and city != cty:
ans.add(",".join([name, time, amt, city]))
return list(ans) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
personDict = dict()
outSet = set()
for transaction in transactions:
name, time, amount, city = transaction.split(",")
if int(amount) > 1000:
outSet.add(transaction)
if name in personDict:
cityDict = personDict[name]
cityDict[city].append((time, amount))
for key in cityDict:
if key != city:
for pair in cityDict[key]:
prevTime, prevAmount = pair
timeDiff = abs(int(time) - int(prevTime))
if timeDiff <= 60:
outSet.add(transaction)
outSet.add(",".join([name, prevTime, prevAmount, key]))
else:
personDict[name] = defaultdict(list)
personDict[name][city].append((time, amount))
return outSet | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
n = len(transactions)
invalid = []
hashtime = {}
hashcity = {}
hashindex = {}
for i in range(n):
t = transactions[i].split(",")
n = t[0]
ti = int(t[1])
a = int(t[2])
c = t[3]
if hashtime.get(n) is None:
hashtime[n] = [ti]
hashcity[n] = [c]
hashindex[n] = [i]
else:
for h in range(len(hashindex[n])):
li = hashtime[n][h]
lc = hashcity[n][h]
in1 = hashindex[n][h]
if abs(li - ti) <= 60 and not lc == c:
if transactions[in1] not in invalid:
invalid.append(transactions[in1])
if transactions[i] not in invalid:
invalid.append(transactions[i])
if a > 1000:
if transactions[i] not in invalid:
invalid.append(transactions[i])
hashtime[n].append(ti)
hashcity[n].append(c)
hashindex[n].append(i)
return invalid | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR LIST VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
arr = []
for x in transactions:
n, a, t, p = x.split(",")
arr.append([n, a, t, p])
inv = []
for i in range(len(arr)):
n, t, a, p = arr[i]
added = False
if int(a) > 1000:
inv.append(arr[i])
added = True
for j in range(i + 1, len(arr)):
nn, tt, aa, pp = arr[j]
if abs(int(t) - int(tt)) <= 60 and n == nn and p != pp:
if not added:
inv.append(arr[i])
added = True
inv.append(arr[j])
inv = [",".join(x) for x in inv]
inv = list(set(inv))
return inv | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
if not transactions:
return []
res = set()
names = defaultdict(list)
time, amt, city = [], [], []
for i, ele in enumerate(transactions):
s = ele.split(",")
names[s[0]] += [(int(s[1]), int(s[2]), s[3], s[0])]
for k, v in list(names.items()):
v = sorted(v)
for i, ele in enumerate(v):
b, c, d, a = ele
for j, ele1 in enumerate(v):
if i != j:
f, g, h, e = ele1
if abs(b - f) <= 60 and a == e and d != h:
res.add(a + "," + str(b) + "," + str(c) + "," + d)
res.add(e + "," + str(f) + "," + str(g) + "," + h)
if c > 1000:
res.add(a + "," + str(b) + "," + str(c) + "," + d)
return res | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
record, res = collections.defaultdict(list), set()
for trans in transactions:
name, time, amount, city = trans.split(",")
amount, time = int(amount), int(time)
if amount > 1000:
res.add(trans)
if name in record:
i, j = bisect.bisect_left(
record[name], (time - 60, "", 0)
), bisect.bisect_right(record[name], (time + 61, "", 0))
valid = True
for item in record[name][i:j]:
if item[1] != city:
valid = False
res.add(",".join([name, str(item[0]), str(item[2]), item[1]]))
if not valid:
res.add(trans)
bisect.insort(record[name], (time, city, amount))
return list(res) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions):
if not transactions:
return []
n = len(transactions)
splits = [trans.split(",") for trans in transactions]
valid = [True] * n
for i in range(n):
if int(splits[i][2]) > 1000:
valid[i] = False
for i in range(n):
for j in range(i + 1, n):
if valid[i] or valid[j]:
name1, time1, city1 = splits[i][0], int(splits[i][1]), splits[i][3]
name2, time2, city2 = splits[j][0], int(splits[j][1]), splits[j][3]
if name1 == name2 and city1 != city2 and abs(time1 - time2) <= 60:
valid[i], valid[j] = False, False
return [transactions[i] for i in range(n) if not valid[i]] | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
trans, invalid = [], []
for t in transactions:
trans += (t.split(","),)
trans.sort(key=lambda x: int(x[1]))
invalid = set()
for i in range(len(trans)):
if int(trans[i][2]) > 1000:
invalid.add(",".join(trans[i]))
if i:
j = i
while j and int(trans[i][1]) - int(trans[j - 1][1]) < 61:
if (
trans[i][0] == trans[j - 1][0]
and trans[i][3] != trans[j - 1][3]
):
invalid.add(",".join(trans[j - 1]))
j -= 1
elif i and int(trans[i][1]) - int(trans[i - 1][1]) < 61:
j = i
while j and int(trans[i][1]) - int(trans[j - 1][1]) < 61:
if (
trans[i][0] == trans[j - 1][0]
and trans[i][3] != trans[j - 1][3]
):
invalid.add(",".join(trans[j - 1]))
invalid.add(",".join(trans[i]))
j -= 1
return invalid | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR IF VAR ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR NUMBER RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
if not transactions:
return []
invalid = set()
past_tran = {}
for transaction in transactions:
tran = transaction.split(",")
tran[2] = float(tran[2])
tran[1] = float(tran[1])
if tran[2] > 1000:
invalid.add(transaction)
if tran[0] in past_tran:
for other_transaction in past_tran[tran[0]]:
other_tran = other_transaction.split(",")
other_tran[1] = float(other_tran[1])
if other_tran[3] != tran[3] and abs(tran[1] - other_tran[1]) <= 60:
invalid.add(transaction)
invalid.add(other_transaction)
past_tran[tran[0]].append(transaction)
else:
past_tran[tran[0]] = [transaction]
return list(invalid) | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST VAR RETURN FUNC_CALL VAR VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
ans = []
length = len(transactions)
if not length:
return ans
name, time, money, city = [], [], [], []
add = [1] * length
for trans in transactions:
tran = trans.split(",")
name.append(tran[0])
time.append(eval(tran[1]))
money.append(eval(tran[2]))
city.append(tran[3])
for i in range(length):
if money[i] > 1000:
add[i] = False
for j in range(i + 1, length):
if (
name[i] == name[j]
and abs(time[i] - time[j]) <= 60
and city[i] != city[j]
):
add[i] = False
add[j] = False
for ind, val in enumerate(add):
if not val:
ans.append(transactions[ind])
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
res = []
records = []
for t in transactions:
rec = t.split(",")
rec[1] = int(rec[1])
rec[2] = int(rec[2])
records.append(rec)
for rec in records:
if rec[2] > 1000:
rec[1] = str(rec[1])
rec[2] = str(rec[2])
res.append(",".join(rec))
continue
for x in records:
if rec[0] == x[0] and abs(rec[1] - int(x[1])) <= 60 and rec[3] != x[3]:
rec[1] = str(rec[1])
rec[2] = str(rec[2])
res.append(",".join(rec))
break
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Transaction:
def __init__(self, name, time, amount, city):
self.name = name
self.time = int(time)
self.amount = int(amount)
self.city = city
class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
transactions = [Transaction(*t.split(",")) for t in transactions]
transactions.sort(key=lambda x: x.time)
nameMap = collections.defaultdict(list)
for i, t in enumerate(transactions):
nameMap[t.name].append(i)
invalid = []
for name, idxLst in nameMap.items():
left = 0
right = 0
for idx in idxLst:
t = transactions[idx]
if t.amount > 1000:
invalid.append(f"{t.name},{t.time},{t.amount},{t.city}")
continue
while (
left < len(idxLst) and transactions[idxLst[left]].time < t.time - 60
):
left += 1
while (
right < len(idxLst)
and transactions[idxLst[right]].time <= t.time + 60
):
right += 1
for i in range(left, right):
if t.city != transactions[idxLst[i]].city:
invalid.append(f"{t.name},{t.time},{t.amount},{t.city}")
break
return invalid | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR STRING VAR STRING VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR STRING VAR STRING VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
transactions_by_name = {}
suspicious_transactions = set()
for transaction in transactions:
name, time, amount, city = transaction.split(",")
if name not in transactions_by_name:
transactions_by_name[name] = [[name, time, amount, city]]
else:
transactions_by_name[name].append([name, time, amount, city])
for key in transactions_by_name.keys():
for transaction in transactions_by_name[key]:
for other_transaction in transactions_by_name[key]:
if (
int(transaction[2]) >= 1000
and ",".join(transaction) not in suspicious_transactions
):
suspicious_transactions.add(",".join(transaction))
continue
if transaction == other_transaction:
continue
time_diff = abs(int(transaction[1]) - int(other_transaction[1]))
print(time_diff)
if time_diff <= 60 and transaction[3] != other_transaction[3]:
suspicious_transactions.add(",".join(transaction))
return list(suspicious_transactions) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR LIST LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
tr = {}
invalids = set()
for t in transactions:
tl = t.split(",")
if tl[0] not in tr:
tr[tl[0]] = [t]
if int(tl[2]) > 1000:
invalids.add(t)
else:
if int(tl[2]) > 1000:
invalids.add(t)
tmp = tr[tl[0]]
for temp in tmp:
tempo = temp.split(",")
if tempo[3] != tl[3]:
if abs(int(tempo[1]) - int(tl[1])) <= 60:
invalids.add(t)
invalids.add(temp)
tr[tl[0]].append(t)
return invalids | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
res = []
for i in range(len(transactions)):
action = transactions[i].split(",")
person = action[0]
time = action[1]
amt = action[2]
city = action[3]
if int(amt) > 1000:
res.append(transactions[i])
for j in range(i + 1, len(transactions)):
act2 = transactions[j].split(",")
if (
act2[0] == person
and abs(int(act2[1]) - int(time)) <= 60
and city != act2[3]
):
res.extend([transactions[i], transactions[j]])
return list(set(res)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
A transaction is possibly invalid if:
the amount exceeds $1000, or;
if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form "{name},{time},{amount},{city}"
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000. | class Solution:
def invalidTransactions(self, transactions):
transactions = [t.split(",") for t in transactions]
t = defaultdict(list)
res = set()
for x in transactions:
t[x[0]].append([int(x[1]), int(x[2]), x[3]])
for name in t:
t[name].sort(key=lambda x: x[0])
for i in range(len(t[name])):
time1, amount1, city1 = t[name][i]
if amount1 > 1000:
res.add(",".join([name, str(time1), str(amount1), city1]))
for j in range(i + 1, len(t[name])):
time2, amount2, city2 = t[name][j]
if amount2 > 1000:
res.add(",".join([name, str(time2), str(amount2), city2]))
if city1 == city2:
continue
if time2 - time1 <= 60:
res.add(",".join([name, str(time1), str(amount1), city1]))
res.add(",".join([name, str(time2), str(amount2), city2]))
else:
break
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array A consisting of N integers A_{1},A_{2},…,A_{N}, determine if you can sort this array by applying the following operation several times (possibly, zero):
Pick a pair of indices (i,j) with i \neq j and A_{i} \mathbin{\&} A_{j} \neq 0, and swap the values of A_{i} and A_{j}. Here, \mathbin{\&} denotes the [bitwise AND operation].
For example, if A = [6, 4, 2], the two possible operations are (1, 2) and (1, 3). (2, 3) cannot be performed because A_{2} \mathbin{\&} A_{3} = 4 \mathbin{\&} 2 = 0.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}
------ Output Format ------
For each test case, output the answer on a new line — YES if the given array can be sorted by repeatedly applying the given operation, and NO otherwise.
You may print each character of the answer string in either uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$0 ≤ A_{i} < 2^{31}$ for each $1 ≤ i ≤ N$
- The sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$
------ subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
4
3
6 4 2
6
9 34 4 24 1 6
6
9 34 24 4 1 6
2
1 0
----- Sample Output 1 ------
Yes
Yes
No
No
----- explanation 1 ------
Test case $1$: $A$ can be sorted by applying the single operation $(1, 3)$.
Test case $2$: $A$ can be sorted by applying the following operations in order: $(1,5), (2,6), (2,3), (4,5)$.
Test cases $3$ and $4$: It can be shown that no sequence of operations will sort $A$. | import time
def solve(N, list):
if N <= 1:
return "Yes"
masks = {}
lm = []
ss = sorted(list)
for i in range(len(ss) - 1, -1, -1):
n = ss[i]
found = False
for j in range(len(lm)):
m = lm[j]
if m & n != 0:
found = True
lm[j] |= n
masks[n] = j
if not found:
lm.append(n)
masks[n] = len(lm) - 1
for jj in range(N):
if masks[ss[jj]] != masks[list[jj]]:
return "No"
return "Yes"
tic = time.perf_counter()
T = int(input())
res = []
for i in range(T):
N = int(input())
list = [int(x) for x in input().split()]
res.append(solve(N, list))
for el in res:
print(el)
toc = time.perf_counter() | IMPORT FUNC_DEF IF VAR NUMBER RETURN STRING ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR |
Given an array A consisting of N integers A_{1},A_{2},…,A_{N}, determine if you can sort this array by applying the following operation several times (possibly, zero):
Pick a pair of indices (i,j) with i \neq j and A_{i} \mathbin{\&} A_{j} \neq 0, and swap the values of A_{i} and A_{j}. Here, \mathbin{\&} denotes the [bitwise AND operation].
For example, if A = [6, 4, 2], the two possible operations are (1, 2) and (1, 3). (2, 3) cannot be performed because A_{2} \mathbin{\&} A_{3} = 4 \mathbin{\&} 2 = 0.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}
------ Output Format ------
For each test case, output the answer on a new line — YES if the given array can be sorted by repeatedly applying the given operation, and NO otherwise.
You may print each character of the answer string in either uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$0 ≤ A_{i} < 2^{31}$ for each $1 ≤ i ≤ N$
- The sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$
------ subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
4
3
6 4 2
6
9 34 4 24 1 6
6
9 34 24 4 1 6
2
1 0
----- Sample Output 1 ------
Yes
Yes
No
No
----- explanation 1 ------
Test case $1$: $A$ can be sorted by applying the single operation $(1, 3)$.
Test case $2$: $A$ can be sorted by applying the following operations in order: $(1,5), (2,6), (2,3), (4,5)$.
Test cases $3$ and $4$: It can be shown that no sequence of operations will sort $A$. | def find(parent, x):
if parent[x] != x:
parent[x] = parent[find(parent, parent[x])]
return parent[x]
def union(parent, rank, length, x, y):
u = find(parent, x)
v = find(parent, y)
if u == v:
return u
if rank[u] > rank[v]:
parent[v] = u
length[u] += length[v]
return u
elif rank[v] > rank[u]:
parent[u] = v
length[v] += length[u]
return v
if length[u] > length[v]:
parent[v] = u
length[u] += length[v]
rank[u] += 1
return u
parent[u] = v
length[v] += length[u]
rank[v] += 1
return v
def setup():
N = int(input())
arr = [x for x in map(int, input().split())]
length = [1] * N
rank = [1] * N
parent = list(range(N))
for ii in range(33):
i = 0
while i < N and not arr[i] & 1 << ii:
i += 1
if i < N:
u = find(parent, i)
i += 1
while i < N:
if arr[i] & 1 << ii:
v = union(parent, rank, length, u, i)
if length[v] == N:
return True
i += 1
if length[u] == N:
return True
dic = {}
for i in range(N):
x = parent[i]
if x in dic:
dic[x].append(i)
else:
dic[x] = [i]
for x in dic.values():
tempU = sorted(list(arr[y] for y in x))
for i in range(len(x)):
arr[x[i]] = tempU[i]
for i in range(1, N):
if arr[i - 1] > arr[i]:
return False
return True
T = int(input())
for _ in range(T):
if setup():
print("Yes")
else:
print("No") | FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Given an array A consisting of N integers A_{1},A_{2},…,A_{N}, determine if you can sort this array by applying the following operation several times (possibly, zero):
Pick a pair of indices (i,j) with i \neq j and A_{i} \mathbin{\&} A_{j} \neq 0, and swap the values of A_{i} and A_{j}. Here, \mathbin{\&} denotes the [bitwise AND operation].
For example, if A = [6, 4, 2], the two possible operations are (1, 2) and (1, 3). (2, 3) cannot be performed because A_{2} \mathbin{\&} A_{3} = 4 \mathbin{\&} 2 = 0.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N.
- The second line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N}
------ Output Format ------
For each test case, output the answer on a new line — YES if the given array can be sorted by repeatedly applying the given operation, and NO otherwise.
You may print each character of the answer string in either uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$0 ≤ A_{i} < 2^{31}$ for each $1 ≤ i ≤ N$
- The sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$
------ subtasks ------
Subtask #1 (100 points): Original constraints
----- Sample Input 1 ------
4
3
6 4 2
6
9 34 4 24 1 6
6
9 34 24 4 1 6
2
1 0
----- Sample Output 1 ------
Yes
Yes
No
No
----- explanation 1 ------
Test case $1$: $A$ can be sorted by applying the single operation $(1, 3)$.
Test case $2$: $A$ can be sorted by applying the following operations in order: $(1,5), (2,6), (2,3), (4,5)$.
Test cases $3$ and $4$: It can be shown that no sequence of operations will sort $A$. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def st():
return input().rstrip("\n")
def lis():
return list(map(int, input().split()))
def ma():
return map(int, input().split())
t = inp()
po = [1]
for i in range(30):
po.append(2 * po[-1])
while t:
t -= 1
n = inp()
a = lis()
bits = [set() for i in range(31)]
for i in range(n):
for j in range(31):
if po[j] & a[i]:
bits[j].add(i)
for i in range(31):
for j in range(i + 1, 31):
common = bits[i].intersection(bits[j])
if len(common):
new = set()
for k in bits[i]:
new.add(k)
for k in bits[j]:
new.add(k)
bits[i] = {}
bits[j] = new
break
a1 = [0] * n
for i in range(31):
new = []
ind = []
for j in bits[i]:
new.append(a[j])
ind.append(j)
new.sort()
ind.sort()
for j in range(len(ind)):
a1[ind[j]] = new[j]
a.sort()
if a == a1:
print("Yes")
else:
print("No") | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
min = nums[0]
start, end = 0, len(nums) - 1
while start < end:
mid = (start + end) // 2
if nums[mid] > nums[end]:
start = mid + 1
elif nums[mid] < nums[end]:
end = mid
else:
end = end - 1
return nums[start] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
if len(nums) == 1:
return nums[0]
minimun = nums[0]
for i in range(0, len(nums) - 1):
if nums[i + 1] >= nums[i]:
continue
else:
minimun = nums[i + 1]
return minimun | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution(object):
def findMin(self, nums):
if len(nums) == 1:
return nums.pop()
start, finish = 1, len(nums) - 1
while start < finish:
half = (start + finish) // 2
if nums[half] > nums[0]:
start = half + 1
elif nums[half] == nums[0]:
return min(
Solution.findMin(self, nums[:half]),
Solution.findMin(self, nums[half:]),
)
else:
finish = half
return min(nums[start], nums[0]) | CLASS_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
min = 99999999999
if len(nums) == 1:
return nums[0]
for i in range(len(nums) - 1):
if nums[i] >= nums[i + 1] and min > nums[i + 1]:
min = nums[i + 1]
elif nums[i] <= nums[i + 1] and min > nums[i]:
min = nums[i]
return min | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
n = len(nums)
if n == 0:
return None
l, r = 0, n
while l + 1 < r:
mid = l + r >> 1
if nums[mid] == nums[l]:
for i in range(l + 1, mid):
if nums[i] < nums[l]:
r = mid
break
if r != mid:
l = mid
elif nums[mid] > nums[l]:
l = mid
else:
r = mid
return nums[r % n] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP VAR VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
if len(nums) == 1:
return nums[0]
for index in range(-1, len(nums) - 1):
if nums[index] > nums[index + 1]:
return nums[index + 1]
if index + 1 == len(nums) - 1:
return nums[0]
return None | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN NONE |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
if not nums:
return -1
l, r = 0, len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] < nums[r]:
r = mid
elif nums[mid] > nums[r]:
l = mid + 1
else:
r -= 1
if nums[l] < nums[r]:
return nums[l]
else:
return nums[r] | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN VAR VAR RETURN VAR VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
l = 0
r = len(nums) - 1
res = nums[r]
while l < r:
if nums[l] < nums[r]:
return min(nums[l], res)
res = min(res, nums[r])
m = int((l + r) / 2)
if nums[l] == nums[r]:
temp = nums[r]
while nums[l] == temp and l < r:
l = l + 1
while nums[r] == temp and l < r:
r = r - 1
elif nums[m] >= nums[l]:
l = m + 1
elif nums[m] <= nums[r]:
res = min(nums[m], res)
r = m - 1
return min(nums[l], res) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
Find the minimum element.
The array may contain duplicates.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why? | class Solution:
def findMin(self, nums):
start = 0
end = len(nums) - 1
while start < end:
if start < len(nums) - 1 and nums[start] == nums[start + 1]:
start += 1
continue
if nums[end] == nums[end - 1]:
end -= 1
continue
mid = start + (end - start) // 2
if nums[mid] > nums[end]:
start = mid + 1
else:
end = mid
return nums[start] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
d = {}
for i in arr:
if i in d:
d[i] += 1
else:
d[i] = 1
fact = 1
for i in d:
fact *= d[i] + 1
fact %= 10**9 + 7
print(fact - 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for _ in range(t):
n = int(input())
x = input().split()
z = dict()
for i in x:
if i not in z:
z[i] = 1
else:
z[i] += 1
m = list(z.values())
l = 1
for i in m:
l = l * (i + 1) % 1000000007
print(l - 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | M = 1000000007
T = int(input())
for i in range(T):
N = int(input())
A = str(input()).split(" ")
dicto = {}
for j in range(N):
dicto[int(A[j])] = dicto.get(int(A[j]), 0) + 1
total = 1
for k, v in dicto.items():
total = total * (v + 1) % M
print(total - 1) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | def fi(arr, n):
mp = {}
lst = []
for i in range(n):
if arr[i] not in mp:
mp[arr[i]] = 0
mp[arr[i]] += 1
for i in mp:
lst.append(mp[i])
return lst
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(set(a))
one = 1
for j in fi(a, n):
one = one * (j + 1)
print((one - 1) % (10**9 + 7)) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for i in range(t):
n = int(input())
lst = list(map(int, input().split()))
dic = {}
for j in lst:
dic[j] = 0
for j in lst:
dic[j] += 1
p = 1
for j in dic.keys():
p = p * (dic[j] + 1)
ans = p - 1
print(ans % (10**9 + 7)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for _ in range(t):
p = 10**9 + 7
n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 1
for i in range(n):
if a[i] in d:
d[a[i]] += 1
else:
d[a[i]] = 1
for i, j in d.items():
ans *= 1 + j
ans %= p
ans = (ans - 1 + p) % p
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for tt in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
if len(set(arr)) == 1:
print(n)
continue
i = 0
mod = int(10**9 + 7)
result = 1
while i < n:
j = i
while j < n and arr[i] == arr[j]:
j += 1
result *= j - i + 1
result %= mod
i = j
result += mod - 1
print(result % mod) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
n = int(input())
x = list(map(int, input().split()))
x.sort()
sum = 1
last = 1
for i in range(1, len(x)):
if x[i] == x[i - 1]:
sum = (sum + last) % (10**9 + 7)
else:
last = sum + 1 % (10**9 + 7)
sum = (sum * 2 + 1) % (10**9 + 7)
print(sum) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
s = len(set(l))
d = dict.fromkeys(l, 0)
for i in l:
d[i] += 1
ans = 1
for i in list(d.keys()):
ans *= d[i] + 1
print((ans - 1) % 1000000007) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
mod = 10**9 + 7
d = {}
for i in arr:
if i not in d:
d[i] = 1
else:
d[i] += 1
ans = 1
for k, v in d.items():
ans = ans % mod * (v + 1) % mod % mod
print((ans % mod - 1 % mod) % mod) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | def getGood(arr):
dct = {}
ans = 1
for item in arr:
if item in dct:
dct[item] += 1
else:
dct[item] = 1
for item, freq in dct.items():
ans *= freq + 1
return (ans - 1) % 1000000007
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
print(getGood(arr)) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = 10**9 + 7
ans = 1
d = dict()
for i in range(n):
d[a[i]] = d.get(a[i], 0) + 1
for e in d:
ans = ans * (d[e] + 1)
ans = (ans - 1) % m
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | mod = 10**9 + 7
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
m = {}
for v in arr:
if v in m:
m[v] += 1
else:
m[v] = 1
ans = 1
for v in m:
ans = ans * (1 + m[v])
print((ans - 1) % mod) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
m = 1000000000.0 + 7
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
s = 1
for i in d.keys():
s *= d[i] + 1
s %= m
print(int(s) - 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | T = int(input())
for i in range(T):
N = int(input())
a = list(map(int, input().split()))
dic = {}
for j in a:
if j in dic.keys():
dic[j] += 1
else:
dic[j] = 1
mod = 1000000000.0 + 7
ans = 1
for key in dic:
ans = ans * (dic[key] + 1)
ans = int(ans % mod)
print(int(ans - 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for _ in range(t):
n = int(input())
lst = list(map(int, input().split()))
Dict = {}
for ele in lst:
if ele in Dict:
Dict[ele] += 1
else:
Dict[ele] = 1
res = 1
for freq in Dict.values():
res *= freq + 1
ans = (res - 1) % (pow(10, 9) + 7)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
d = {}
ans = 1
for j in l:
d[j] = d.get(j, 0) + 1
for j in d.values():
ans *= j + 1
print((ans - 1) % 1000000007) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | mod = 1000000007
for _ in range(int(input())):
input()
l = list(map(int, input().split()))
d = {}
for i in l:
if i in d:
d[i] += 1
else:
d[i] = 2
product = 1
for i in d.values():
product = product * i % mod
print(product - 1) | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR 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 VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
while t != 0:
n = int(input())
a = list(map(int, input().split()))
net = dict()
for i in a:
net[i] = net.get(i, 1) + 1
ans = 1
for i in net:
ans *= net[i]
print((ans - 1) % 1000000007)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
input()
x = sorted(map(int, input().split()))
sam = inc = 1
for i in range(1, len(x)):
if x[i] != x[i - 1]:
inc = (sam + 1) % (10**9 + 7)
sam = (sam + inc) % (10**9 + 7)
print(sam) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | def ans(arr):
x = set(arr)
hash_map = {}
for y in x:
hash_map[y] = 0
for y in arr:
hash_map[y] += 1
count = 1
for y in hash_map.values():
count *= 1 + y
return (count - 1) % (10**9 + 7)
test_cases = int(input())
while test_cases != 0:
d = int(input())
d2 = list(map(int, input().split()))
print(ans(d2))
test_cases -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | from sys import stdin, stdout
inp_num = lambda: int(input())
inp_lis = lambda: list(map(int, input().split()))
mo = 10**9 + 7
mo = int(mo)
for _ in range(inp_num()):
n = inp_num()
l = inp_lis()
d = {}
for i in l:
d[i] = 0
for i in l:
d[i] += 1
an = 1
for i in d.values():
an = an * (i + 1)
print(an % mo - 1) | 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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | MOD = 10**9 + 7
def solve(n, arr):
arr.sort()
res = 1
lookup = {arr[0]: 1}
for i in range(1, n):
if arr[i] != arr[i - 1]:
lookup[arr[i]] = res + 1
res += res + 1
else:
res += lookup[arr[i]]
res %= MOD
return res
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
print(solve(n, arr)) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | test = int(input())
MOD = int(1000000000.0 + 7)
while test:
n = int(input())
f = dict()
for i in input().split():
if int(i) in f.keys():
f[int(i)] += 1
else:
f[int(i)] = 2
ans = 1
for i in f.values():
ans *= i
print((ans - 1) % MOD)
test -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER WHILE VAR 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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | def main():
n = int(input())
arr = list(map(int, input().split()))
MOD = 1000000007
freq = {}
for x in arr:
if x not in freq:
freq[x] = 0
freq[x] += 1
ans = 1
for k, v in freq.items():
ans *= v + 1
return (ans - 1) % MOD
for _ in range(int(input())):
print(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 NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
if i not in d:
d[i] = 1
else:
d[i] += 1
mod = int(10**9 + 7)
s = 1
for j in d.values():
s *= j + 1
s = s - 1
print(s % mod) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
d = dict.fromkeys(a, 0)
for i in a:
d[i] += 1
ans = 1
for i in list(d.keys()):
ans *= d[i] + 1
print((ans - 1) % (pow(10, 9) + 7)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | M = 10**9 + 7
t = int(input())
for _ in range(t):
n = int(input())
dic = {}
A = list(map(int, input().split()))
for i in A:
dic[i] = 0
for i in A:
dic[i] += 1
answer = 1
for i in dic:
answer *= dic[i] + 1
answer %= M
answer = (answer - 1) % M
print(answer) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for i in range(int(input())):
F = 10**9 + 7
D, p, r = [], 1, 1
n = int(input())
e = input()
D = e.split()
D.sort()
D.append("a")
for k in range(n):
if D[k] == D[k + 1]:
p = p + 1
else:
r = r * (p + 1)
p = 1
print(r % F - 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | mod7 = 1000000000.0 + 7
for _ in range(int(input())):
N = int(input())
numbers = list(map(int, input().split()))
freq = {}
ans = 1
for i in numbers:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
for i in freq:
ans *= freq[i] + 1
ans %= mod7
print(int(ans - 1)) | ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for tc in range(t):
n = int(input())
arr = list(map(int, input().split()))
mod = 1000000007
d = {}
for i in range(n):
try:
d[arr[i]] += 1
except:
d[arr[i]] = 1
ans = 1
for freq in d.values():
ans *= freq + 1
ans %= mod
print((ans - 1 + mod) % mod) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
while t > 0:
n = int(input())
A = list(input().split())
A = A[:n]
dic = {}
ans = 1
mod = 1000000000.0 + 7
for i in A:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
for j in dic:
ans = ans * (dic[j] + 1)
ans = ans % mod
ans = (ans - 1 + mod) % mod
print(int(ans))
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
length = int(input())
arr = list(map(int, input().split()))
temp_dict = dict()
for element in arr:
if element in temp_dict:
temp_dict[element] += 1
else:
temp_dict[element] = 1
total = 1
for key in temp_dict.keys():
total *= temp_dict[key] + 1
print((total - 1) % (10**9 + 7)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | for _ in range(int(input())):
n = int(input())
a = []
ans = 1
a = map(int, input().split())
freq = {}
mod7 = 1000000000.0 + 7
for i in a:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
for a, b in freq.items():
f = b
ans = ans * (f + 1)
ans %= mod7
ans -= 1
print(int(ans)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array A containing N integers. A [subsequence] of this array is called *good* if all the elements of this subsequence are distinct.
Find the number of *good* non-empty subsequences of A. This number can be large, so report it modulo 10^{9} + 7.
Note that two subsequences are considered different if they differ in the indices chosen. For example, in the array [1, 1] there are 3 different non-empty subsequences: [1] (the first element), [1] (the second element) and [1, 1]. Out of these three subsequences, the first two are good and the third is not.
------ Input Format ------
- The first line contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains a single integer N, denoting the number of elements in A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer — the number of good subsequences modulo 10^{9} + 7
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
2
2
1 1
2
1 2
----- Sample Output 1 ------
2
3
----- explanation 1 ------
- Test Case $1$: Explained in the problem statement.
- Test Case $2$: There are 3 non-empty subsequences: $[1]$, $[2]$ and $[1, 2]$. All three subsequences are good. | t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
d = {}
for i in l:
d[i] = d.get(i, 0) + 1
sum = 1
for i in d:
sum *= d[i] + 1
print((sum - 1) % (10**9 + 7)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if n == 1:
return arr[n - 1]
arr.sort()
firstnum = ""
secondnum = ""
for i in range(len(arr)):
if i % 2 != 0:
firstnum += str(arr[i])
elif i % 2 == 0:
secondnum += str(arr[i])
return int(firstnum) + int(secondnum) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
ans1 = 0
ans2 = 0
i = 0
while i < n:
ans1 = ans1 * 10 + arr[i]
i += 2
i = 1
while i < n:
ans2 = ans2 * 10 + arr[i]
i += 2
ans = ans1 + ans2
return str(ans) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
sum1 = 0
sum2 = 0
for i in range(n):
if i % 2 == 0:
sum1 = sum1 * 10 + arr[i]
else:
sum2 = sum2 * 10 + arr[i]
return abs(sum1 + sum2) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
x = ""
y = ""
for i in range(0, n, 2):
x += str(arr[i])
if i + 1 < n:
y += str(arr[i + 1])
if x != "":
x = int(x)
else:
x = 0
if y != "":
y = int(y)
else:
y = 0
return x + y | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
a = 0
b = 0
i = 0
while i < len(arr):
a = a * 10 + arr[i]
i += 1
if i == len(arr):
break
b = b * 10 + arr[i]
i += 1
return str(a + b) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if n <= 2:
return sum(arr)
arr.sort()
ist = ""
nd = ""
i = 1
while i <= n:
ist += str(arr[i - 1])
if i == n:
break
else:
nd += str(arr[i])
i += 2
return int(ist) + int(nd) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
st1 = ""
st2 = ""
for i in range(n):
if i % 2 == 0:
st1 += str(arr[i])
else:
st2 += str(arr[i])
if st1 == "":
st1 = "0"
if st2 == "":
st2 = "0"
return int(st1) + int(st2) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if n == 1:
return arr[0]
arr.sort()
lst1 = ""
lst2 = ""
for i in range(0, n, 2):
q = str(arr[i])
lst1 = lst1 + q
for i in range(1, n, 2):
w = str(arr[i])
lst2 = lst2 + w
k = int(lst1)
m = int(lst2)
return k + m | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
first, second = arr[::2], arr[1::2]
first = "".join(str(x) for x in first) if first else "0"
second = "".join(str(x) for x in second) if second else "0"
return int(first) + int(second) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
frst = ""
sec = ""
if len(arr) == 1:
return arr[0]
for i in arr:
if len(frst) == len(sec):
frst = frst + str(i)
else:
sec = sec + str(i)
frst = int(frst)
sec = int(sec)
return frst + sec | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
a = ""
b = ""
for i in range(0, n, 2):
a = a + str(arr[i])
for j in range(1, n, +2):
b = b + str(arr[j])
if a == "" and b != "":
return int(b)
elif a != "" and b == "":
return int(a)
else:
return int(a) + int(b) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING VAR STRING RETURN FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
num1 = 0
num2 = 0
arr.sort()
arr.reverse()
while len(arr) > 1:
num1 *= 10
num2 *= 10
num1 += arr[-1]
num2 += arr[-2]
arr.pop()
arr.pop()
if arr:
num1, num2 = min(num1, num2) * 10 + arr[-1], max(num1, num2)
return num1 + num2 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if len(arr) == 1:
return arr[0]
arr.sort()
l = arr[::2]
arr = arr[1::2]
l = list(map(str, l))
arr = list(map(str, arr))
l = "".join(l)
arr = "".join(arr)
l = int(l)
arr = int(arr)
return l + arr | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
even_sum = 0
odd_sum = 0
arr.sort()
for i in range(n):
if i % 2 == 0:
even_sum = even_sum * 10 + arr[i]
else:
odd_sum = odd_sum * 10 + arr[i]
return even_sum + odd_sum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
s1 = ""
s2 = ""
arr.sort()
if len(arr) == 1:
return arr[0]
for i in range(n):
if i % 2 == 0:
s1 += str(arr[i])
else:
s2 += str(arr[i])
return int(s1) + int(s2) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
first = 0
last = 0
for i in range(0, len(arr), 2):
first = first * 10 + arr[i]
for i in range(1, len(arr), 2):
last = last * 10 + arr[i]
return first + last | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
ans1 = ans2 = "0"
for i in range(0, n - 1, 2):
ans1 += str(arr[i])
ans2 += str(arr[i + 1])
if n % 2:
ans1 += str(arr[-1])
return int("".join(ans2)) + int("".join(ans1)) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR FUNC_CALL STRING VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
e = 0
o = 0
arr.sort()
for j in range(1, n, 2):
o = str(o) + str(arr[j])
for i in range(0, n, 2):
e = str(e) + str(arr[i])
return int(o) + int(e) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
l = ""
m = ""
k = sorted(arr)
for i in range(0, n, 2):
l += str(k[i])
for i in range(1, n, 2):
m += str(k[i])
if len(l) == 0:
l = "0"
if len(m) == 0:
m = "0"
return int(l) + int(m) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if n == 1:
return arr[0]
num1 = ""
num2 = ""
arr.sort()
for i in range(0, n, 2):
num1 += str(arr[i])
for i in range(1, n, 2):
num2 += str(arr[i])
num1 = int(num1)
num2 = int(num2)
return num2 + num1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if len(arr) < 2:
return arr[0]
arr.sort()
flag = 1
str1 = ""
str2 = ""
for i in range(n):
if flag == 1:
str1 += str(arr[i])
flag = 0
elif flag == 0:
str2 += str(arr[i])
flag = 1
return int(str1) + int(str2) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
if n == 1:
return arr[0]
num1 = "".join(map(str, arr[::2]))
num2 = "".join(map(str, arr[1::2]))
return int(num1) + int(num2) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
arr.sort()
s1 = ""
s2 = ""
flag = 0
for i in arr:
if flag == 0:
s1 += str(i)
flag = 1
else:
s2 += str(i)
flag = 0
if s1 == "":
s1 = "0"
if s2 == "":
s2 = "0"
return int(s1) + int(s2) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if n == 1:
return str(arr[0])
arr.sort()
r = ""
l = ""
for i in range(0, n, 2):
l = l + str(arr[i])
for i in range(1, n, 2):
r = r + str(arr[i])
k = int(l) + int(r)
return str(k) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.
Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed
by numbers 358 and 246.
Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is
formed by numbers 35 and 047.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ Arr_{i} ≤ 9 | class Solution:
def solve(self, arr, n):
if n == 1:
return arr[0]
arr.sort()
s = ""
p = ""
for i in range(0, len(arr), 2):
s += str(arr[i])
for j in range(1, len(arr), 2):
p += str(arr[j])
return int(s) + int(p) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.