description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | l = []
cnt = 0
for i in input():
if i in "[<{(":
l.append(i)
elif len(l) > 0:
if l.pop() != {"}": "{", ")": "(", "]": "[", ">": "<"}[i]:
cnt += 1
else:
print("Impossible")
break
else:
if len(l) > 0:
print("Impossible")
else:
print(cnt) | ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | import sys
INF = 10**18 + 3
def main():
opening = "<{[("
closing = ">}])"
s = input()
stack = []
res = 0
for i, sym in enumerate(s):
if sym in opening:
kind = opening.find(sym)
stack.append(kind)
else:
kind = closing.find(sym)
if... | IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER VA... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | s = input()
stack = []
answer = 0
corrects = "[]", "{}", "<>", "()"
for i in s:
if i == "[" or i == "{" or i == "<" or i == "(":
stack.append(i)
elif len(stack) == 0:
print("Impossible")
exit()
else:
top = stack.pop()
if top + i not in corrects:
answer += ... | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | s = input()
cnt = 0
st = []
for elem in s:
if elem in "([{<":
st.append(elem)
else:
if len(st) == 0:
print("Impossible")
break
elem2 = st.pop()
if elem2 + elem not in "()[]{}<>":
cnt += 1
else:
if len(st) == 0:
print(cnt)
else:
... | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | opens = ["(", "{", "<", "["]
closes = [")", "}", ">", "]"]
def solve(inp):
stack = []
res = 0
for i, x in enumerate(inp):
if x in opens:
stack.append((i, x))
if x in closes:
if not stack:
return "Impossible"
ii, xx = stack.pop()
... | ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR RET... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | def main(s: str):
if len(s) == 0:
return "Impossible"
leftchr = ["(", "{", "<", "["]
rightchr = [")", "}", ">", "]"]
stack = list()
opdict = {"(": ")", "{": "}", "<": ">", "[": "]"}
mix = 0
for oper in s:
if oper in leftchr:
stack.append(oper)
elif oper in... | FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF FU... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | def main(s: str):
if len(s) == 0 or len(s) % 2 != 0:
return "Impossible"
leftchr = ["(", "{", "<", "["]
stack = list()
opdict = {"(": ")", "{": "}", "<": ">", "[": "]"}
mix = 0
for oper in s:
if oper in leftchr:
stack.append(oper)
else:
try:
... | FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL ... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | def check_open(char):
if char in "<{[(":
return 1
return 0
def find_type(char):
if char in "()":
return 0
elif char in "[]":
return 1
elif char in "{}":
return 2
else:
return 3
s = input()
stack = []
impossible = 0
count = 0
for char in s:
if check... | FUNC_DEF IF VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER A... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | s = input()
stack = []
open = ["<", "{", "(", "["]
close = [">", "}", ")", "]"]
ans = 0
for i in range(len(s)):
if s[i] in open:
stack.append(s[i])
else:
if len(stack) == 0:
print("Impossible")
exit()
head = stack.pop()
k = close.index(s[i])
if hea... | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CA... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | IMPOSSIBLE = "Impossible"
scopes = {"{": "}", "(": ")", "[": "]", "<": ">"}
stack = []
swap = 0
for i in input():
if i in scopes:
stack.append(i)
elif stack:
if scopes[stack[-1]] != i:
swap += 1
stack.pop()
else:
print(IMPOSSIBLE)
break
else:
print(swa... | ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | s = input()
def check_str(s):
brackets = {"<": ">", "{": "}", "[": "]", "(": ")"}
res = 0
stack = []
for c in s:
if c in brackets:
stack.append(c)
else:
if stack:
top = stack.pop()
if top in brackets:
if bracke... | ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER RETURN STRING IF VAR RETURN STRING RETURN VAR EXPR FUNC_CALL VAR ... |
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a... | s = input()
open = "[<({"
close = "]>)}"
clopen = {"]": "[", "}": "{", ">": "<", ")": "("}
popen = []
c = 0
for i in range(len(s)):
if s[i] in open:
popen.append(s[i])
else:
if len(popen) == 0:
print("Impossible")
return
p = popen.pop()
if clopen[s[i]] != ... | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, string):
first = 0
second = 2
Lps_arr = [0] * (len(string) + 1)
string = " " + string
while second < len(string):
if string[first + 1] == string[second]:
Lps_arr[second] = first + 1
first += 1
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR NUM... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
l = len(s)
l1 = [i for i in range(l) if s[i] == s[0]]
n = len(l1)
if n > 1:
for i in range(1, n):
d = l - l1[i]
a = s[:d]
b = s[l1[i] :]
if a == b:
retur... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
j = -1
p = [j]
for c in s:
while j >= 0 and s[j] != c:
j = p[j]
j += 1
p.append(j)
return len(s[:j]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
lps = [0] * len(s)
prevLPS, i = 0, 1
while i < len(s):
if s[prevLPS] == s[i]:
lps[i] = prevLPS + 1
prevLPS += 1
i += 1
elif prevLPS == 0:
lps[i] = 0
i +=... | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
for substrlen in range(len(s) - 1, 0, -1):
index = 0
while True:
if index == substrlen:
return substrlen
if s[index] != s[len(s) - substrlen + index]:
break
inde... | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
arr = []
n = len(s)
for i in range(len(s) - 1):
if s[i] == s[-1]:
arr.append(i)
out = float("-inf")
for i in arr[::-1]:
if s[: i + 1] == s[n - i - 1 :]:
return i + 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
lps = [0] * n
i, j = 1, 0
while i < n:
if s[i] == s[j]:
lps[i] = j + 1
i += 1
j += 1
elif j != 0:
j = lps[j - 1]
else:
lps... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, str):
A = [(0) for i in range(len(str))]
j = 0
i = 1
while i < len(str):
if str[i] == str[j]:
A[i] = j + 1
j += 1
i += 1
elif j == 0:
i += 1
else:
... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
if s[0] * len(s) == s:
return len(s) - 1
i = 0
if len(s) % 2 == 0:
mid = len(s) // 2
else:
mid = len(s) // 2 + 1
j = 1
count = 0
k = 1
while i < len(s) - 1 and j < len(s):
... | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, S):
lenpf = 0
i = 1
lps = [(0) for i in range(len(S))]
while i < len(S):
if S[i] == S[lenpf]:
lps[i] = lenpf + 1
lenpf += 1
i += 1
elif lenpf != 0:
lenpf = lps[lenpf... | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR BIN_OP FUNC_CALL V... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
if n == 1:
return 0
ele = s[0]
gle = s[-1]
i = 1
j = n - 2
ans = 0
str = ""
while i < n and j > 0:
if s[j] == gle:
str = s[0 : j + 1]
j -= 1
... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
k = 0
j = 1
while j + k < n:
if s[k] == s[j + k]:
k += 1
else:
j += 1
k = 0
return k | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
i, j, k, cnt = 0, 1, 1, 0
n = len(s)
while i < n - 1 and j < n:
if s[i] == s[j]:
cnt = cnt + 1
i = i + 1
j = j + 1
elif s[i] != s[j]:
i = 0
k = k + 1
... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NU... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
table = [0] * n
i, j = 1, 0
while i < n:
if s[i] == s[j]:
table[i] = j + 1
i += 1
j += 1
elif j > 0:
j = table[j - 1]
else:
... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR ... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, pat):
n = len(pat)
lps = [0] * n
le = 0
i = 1
lps[0] = 0
while i < n:
if pat[i] == pat[le]:
le += 1
lps[i] = le
i += 1
elif le != 0:
le = lps[le - 1]... | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR N... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
for index in range(1, len(s)):
prefixindex = 0
while (
index + prefixindex < len(s)
and s[prefixindex] == s[index + prefixindex]
):
prefixindex += 1
if index + prefixindex == le... | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
pi = [0] * n
j = 0
for i in range(1, n):
while j > 0 and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
arr = [0] * len(s)
j = 0
if len(s) == 1:
return 0
i = 1
while i < len(s):
if s[i] == s[j]:
arr[i] = j + 1
j += 1
i += 1
else:
if j != 0:
... | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR ... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
for i in range(n - 1, 0, -1):
for j in range(i):
if s[j] != s[n - i + j]:
break
else:
return i
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR RETURN NUMBER |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
if len(s) == 0:
return 0
if len(s) == 1:
return 1
b = "".join(sorted(s))
if b[0] == b[len(b) - 1]:
return len(b) - 1
i = 0
j = 1
n = len(s)
count = 0
while j < n:
... | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER... |
Given a string of characters, find the length of the longest proper prefix which is also a proper suffix.
NOTE: Prefix and suffix can be overlapping but they should not be equal to the entire string.
Example 1:
Input: s = "abab"
Output: 2
Explanation: "ab" is the longest proper
prefix and suffix.
Example 2:
Input: s ... | class Solution:
def lps(self, s):
n = len(s)
ans = 0
fli = []
for i in range(1, n):
if s[i] == s[0]:
fli.append(i)
for i in fli:
if s[i:] == s[: n - i]:
ans = n - i
break
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | def almost_difference():
n = int(input())
if n == 1:
return 0
dict_equal = dict()
array = [int(el) for el in input().split()]
ad_sum = 0
prev_sum = 0
for i in range(n):
if not array[i] in dict_equal.keys():
dict_equal[array[i]] = 0
if not array[i] - 1 in d... | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | import sys
n = int(input())
a = list(map(int, input().split()))
s = 0
ans = 0
cnt = {}
for i in range(n):
m = i
tmp = s
for j in range(a[i] - 1, a[i] + 2):
if j in cnt:
m -= cnt[j]
tmp -= cnt[j] * j
ans += m * a[i] - tmp
s += a[i]
if a[i] in cnt:
cnt[a[i]... | IMPORT 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 NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
nums = list(map(int, input().split()))
cnt = {}
answer = 0
summ = 0
for i, item in enumerate(nums):
cnt[item] = cnt.get(item, 0) + 1
summ += item
answer += (i + 1) * item - summ
if item + 1 in cnt:
answer += cnt[item + 1]
if item - 1 in cnt:
answer -= cnt[item - 1]
p... | 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 ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
arr = [int(k) for k in input().split()]
res = 0
tot = 0
um = {(-100): -100}
for i in range(0, n):
if arr[i] in um:
um[arr[i]] += 1
else:
um[arr[i]] = 1
res += i * arr[i] - tot
if arr[i] + 1 in um:
res += um[arr[i] + 1]
if arr[i] - 1 in um:
res -= um[a... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VA... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [0] + list(map(int, input().split()))
s = [0] * len(a)
mp = {}
for i in range(1, n + 1):
s[i] = s[i - 1] + a[i]
if mp.get(a[i]):
mp[a[i]] += 1
else:
mp[a[i]] = 1
ans = 0
for i in range(1, n + 1):
ans += s[n] - s[i - 1]
l = mp.get(a[i] - 1, 0)
m = mp.get(a[i],... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | d = {}
n = int(input())
arr = list(map(int, input().split()))
prefix = [(0) for i in range(n)]
prefix[0] = arr[0]
for i in range(1, n):
prefix[i] = prefix[i - 1] + arr[i]
for i in range(1, n):
try:
d[arr[i]] += 1
except:
d[arr[i]] = 1
s = 0
for i in range(n - 1):
cnt = 0
minus = 0
... | ASSIGN VAR DICT 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
t = [int(x) for x in input().split()]
ans = 0
d = {}
for i in range(n):
ans += (-(n - 1) + 2 * i) * t[i]
for i in range(n):
ans -= d.get(t[i] - 1, 0)
ans += d.get(t[i] + 1, 0)
d[t[i]] = d.get(t[i], 0) + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | def main():
n = int(input())
arr = list(map(int, input().split()))
cnt = dict()
ss = dict()
for i in range(n):
cnt[arr[i]] = 0
cnt[arr[i] - 1] = 0
cnt[arr[i] + 1] = 0
ss[arr[i] - 1] = 0
ss[arr[i] + 1] = 0
ss[arr[i]] = 0
sm = 0
ans = 0
for i... | 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | summ = 0
glob = 0
N = int(input())
a = {}
cnt = 0
huh = list(map(int, input().split()))
for i in huh:
if i - 1 not in a:
a[i - 1] = 0
if i + 1 not in a:
a[i + 1] = 0
if i not in a:
a[i] = 0
glob += (cnt - a[i - 1] - a[i + 1]) * i - (
summ - a[i - 1] * (i - 1) - a[i + 1] *... | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
l = list(map(int, input().split()))
s = 0
for i in range(n):
s += l[i] * i - l[i] * (n - i - 1)
d = {}
c = 0
for i in range(n):
if l[i] not in d:
d[l[i]] = 1
else:
d[l[i]] += 1
if l[i] + 1 in d:
c -= d[l[i] + 1]
if l[i] - 1 in d:
c += d[l[i] - 1]
prin... | 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 FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NU... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
xs = [int(_) for _ in input().split()]
cnt = {}
sm = 0
ans = 0
for i in range(n):
if xs[i] - 1 not in cnt:
cnt[xs[i] - 1] = 0
if xs[i] + 1 not in cnt:
cnt[xs[i] + 1] = 0
if xs[i] not in cnt:
cnt[xs[i]] = 0
ans = ans + i * xs[i] - sm - cnt[xs[i] - 1] + cnt[xs[i] +... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
lst = input().split()
lst = [int(x) for x in lst]
ans = 0
cnt = {}
for i, e in enumerate(lst):
ans += (2 * i + 1 - n) * e
cnt[e] = 0
for e in lst:
if e - 1 in cnt.keys():
ans -= cnt[e - 1]
if e + 1 in cnt.keys():
ans += cnt[e + 1]
cnt[e] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
ssum = 0
fans = 0
mp = {(0): 0}
a = list(map(int, input().strip().split()))[:n]
def find(x):
if mp.get(x):
return mp[x]
else:
return 0
for i in range(n):
if not mp.get(a[i]):
mp[a[i]] = 0
mp[a[i]] += 1
fans += i * a[i] - ssum + find(a[i] + 1) - find(a[i] ... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBE... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
s = 0
mx = a[0]
dic = dict()
for i in range(n):
dic[a[i]] = 0
for i in range(n):
s = s - a[i] * (n - i - 1) + a[i] * i
if a[i] - 1 in dic:
s = s + dic[a[i] - 1] * (a[i] - 1 - a[i])
if a[i] + 1 in dic:
s = s + dic[a[i] + 1] * (a[i] + 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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBE... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
v = [0] + [int(x) for x in input().split(" ")]
ma = {}
st = 0
ris = 0
for p in range(n, 0, -1):
val = v[p]
v2 = val + 1
c2 = ma.get(v2, 0)
v1 = val
c1 = ma.get(v1, 0)
v0 = val - 1
c0 = ma.get(v0, 0)
cnt = n - p - c2 - c1 - c0
sp = st - v2 * c2 - v1 * c1 - v0 * c0
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(x) for x in input().split()]
cnt = {}
for i in a:
cnt[i] = 0
cnt[i - 1] = 0
cnt[i + 1] = 0
c = -(n - 1)
ans = 0
for i in range(n):
ans += c * a[i]
c += 2
cnt[a[i]] += 1
ans += cnt[a[i] + 1] - cnt[a[i] - 1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NU... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(m) for m in input().split()]
mp = dict()
pre = 0
ans = 0
for i in range(0, n):
ans += a[i] * i - pre + mp.get(a[i] + 1, 0) - mp.get(a[i] - 1, 0)
pre += a[i]
mp[a[i]] = mp.get(a[i], 0) + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUM... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
x = input().split()
s = 0
ans = 0
mp = {}
for i in range(0, n):
s = s + int(x[n - i - 1])
ans += s - (i + 1) * int(x[n - i - 1])
if int(x[n - i - 1]) + 1 in mp:
ans -= mp[int(x[n - i - 1]) + 1]
if int(x[n - i - 1]) - 1 in mp:
ans += mp[int(x[n - i - 1]) - 1]
if int(x... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BI... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
arr = list(map(int, input().split()))
sum = 0
sum2 = 0
for i in range(n - 1):
sum = sum - arr[i] * (n - i - 1)
for i in range(n - 1, 0, -1):
sum2 += arr[i]
sum += sum2
dict = {arr[n - 1]: 1}
for i in range(n - 2, -1, -1):
if arr[i] + 1 in dict:
sum -= dict[arr[i] + 1]
if arr... | 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VA... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | numCnt = int(input())
nums = [int(i) for i in input().split(" ")]
cnt = dict()
curSum = 0
curCnt = 0
ans = 0
for a in nums:
ans += curCnt * a - curSum
ans += cnt.get(a + 1, 0)
ans -= cnt.get(a - 1, 0)
curSum += a
curCnt += 1
if not a in cnt:
cnt[a] = 0
cnt[a] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VA... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | class BIT:
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def _sum(self, i):
s = 0
while i > 0:
s += self.bit[i]
i -= i & -i
return s
def add(self, i, val):
i = i + 1
while i <= self.n:
self.bit[i] += ... | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF F... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(x) for x in input().split()]
count = {}
cum = 0
tot = 0
for i in range(0, n):
tot = tot + i * a[i] - cum
if a[i] - 1 in count:
tot = tot - count[a[i] - 1]
if a[i] + 1 in count:
tot = tot + count[a[i] + 1]
if a[i] in count:
count[a[i]] = count[a[i]] + 1
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = input().split()
a = [int(x) for x in a]
cnt = {}
for i in range(n):
cnt[a[i]] = 0
cnt[a[i] + 1] = 0
cnt[a[i] - 1] = 0
ans = 0
res = 0
for i in range(n):
ans = ans + (i - cnt[a[i]] - cnt[a[i] + 1] - cnt[a[i] - 1]) * a[i]
ans = ans - (
res - cnt[a[i] - 1] * (a[i] - 1) - cn... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(x) for x in input().split()]
m = {}
total = 0
ans = 0
for i in range(n):
ans += a[i] * i - total
if a[i] + 1 in m:
ans += m[a[i] + 1]
if a[i] - 1 in m:
ans -= m[a[i] - 1]
if a[i] in m:
m[a[i]] += 1
else:
m[a[i]] = 1
total += a[i]
print(an... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR N... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = input().split()
dic = {}
ans = 0
sum = 0
for i in range(0, n):
x = int(a[i])
sum += x
ans += (i + 1) * x - sum
if x in dic:
dic[x] += 1
else:
dic[x] = 1
if x + 1 in dic:
ans += dic[x + 1]
if x - 1 in dic:
ans -= dic[x - 1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split(" ")))
sum1 = a[n - 1]
ans = 0
map1 = {}
map1[a[n - 1]] = 1
for i in range(n - 2, -1, -1):
c1 = map1.get(a[i], 0)
c2 = map1.get(a[i] + 1, 0)
c3 = map1.get(a[i] - 1, 0)
temp = sum1 - c1 * a[i] - c2 * (a[i] + 1) - c3 * (a[i] - 1)
cnt2 = (n - 1 - i - c1 ... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
t = list(map(int, input().split()))
c = {}
u = {}
a = [(0) for _ in range(n)]
for i in range(n):
a[i] = t[i] * i - t[i] * (n - 1 - i)
if t[i] not in u:
u[t[i]] = [i]
else:
u[t[i]].append(i)
l = []
for x in u:
l.append(x)
l.sort()
p = len(l)
res = 0
for i in range(p - 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 DICT ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
sm = 0
ans = 0
mp = {}
for i in range(n):
ans += i * a[i] - sm
ans -= mp.get(a[i] - 1, 0)
ans += mp.get(a[i] + 1, 0)
mp[a[i]] = mp.get(a[i], 0) + 1
sm += a[i]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
f = dict()
s = 0
ret = 0
a = list(map(int, input().split()))
for i in range(0, n):
ret += a[i] * i - s
ret -= f.get(a[i] - 1, 0)
ret += f.get(a[i] + 1, 0)
s += a[i]
f[a[i]] = f.get(a[i], 0) + 1
print(ret) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | dic = {}
n = int(input())
arr = [int(x) for x in input().split()]
for x in arr:
if x not in dic:
dic[x] = 1
else:
dic[x] += 1
total = 0
su = 0
for x in arr:
su += x
for x in range(len(arr)):
su -= arr[x]
hold = 0
cnt = n - x - 1
for y in range(-1, 2):
num = arr[x] + y... | ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | N = int(input())
A = list(map(int, input().split()))
dic = {}
ans = 0
for i, j in enumerate(A):
ans += (2 * i - N + 1) * j - dic.get(j - 1, 0) + dic.get(j + 1, 0)
dic[j] = dic.get(j, 0) + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
occ = {}
for i in a:
occ[i] = occ.get(i, 0) + 1
s = sum(a)
ans = 0
j = n
for i in a:
val = s
nb_cnt = j
if i - 1 > 0:
val -= (i - 1) * occ.get(i - 1, 0)
nb_cnt -= occ.get(i - 1, 0)
val -= i * occ.get(i, 0)
nb_cnt -= occ.get(i, ... | 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
v = []
mp = {}
for i in input().split():
v.append(int(i))
mp[int(i)] = 0
mp[int(i) - 1] = 0
mp[int(i) + 1] = 0
sum = 0
ele = 0
ans = 0
for i in v[::-1]:
ans = ans + (sum - ele * i)
a = (i + 1) * mp[i + 1] - mp[i + 1] * i
b = (i - 1) * mp[i - 1] - mp[i - 1] * i
ans = ans ... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | vcnt = {}
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, n):
ans += a[i] * (i * 2 + 1 - n)
for i in range(0, n):
v = a[i]
if v - 1 in vcnt:
ans -= vcnt[v - 1]
if v + 1 in vcnt:
ans += vcnt[v + 1]
if not v in vcnt:
vcnt[v] = 1
else:
... | ASSIGN VAR DICT 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 FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split(" ")))
b = {}
ans = 0
for i in range(0, n):
if a[i] - 1 in b:
ans -= b[a[i] - 1]
if a[i] + 1 in b:
ans += b[a[i] + 1]
if a[i] in b.keys():
b[a[i]] += 1
else:
b[a[i]] = 1
k = n - 1
for i in range(0, int(n / 2)):
ans += k... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR FUNC_CALL V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | from sys import *
n = int(stdin.readline())
a = list(map(int, stdin.read().split()))
freq = {}
ans = 0
psum = 0
for i in range(len(a)):
psum += a[i]
ans += (i + 1) * a[i] - psum
if a[i] in freq:
freq[a[i]] += 1
else:
freq[a[i]] = 1
if a[i] + 1 in freq:
ans -= (a[i] - (a[i] +... | 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(i) for i in input().split()]
sum = list()
sum.append(0)
cnt = dict()
for i in range(0, n):
cnt[a[i]] = 0
cnt[a[i] - 1] = 0
cnt[a[i] + 1] = 0
sum.append(a[i] + sum[i])
ans = 0
for i in range(0, n):
cur = 0
cur += i * a[i] - sum[i]
cur -= cnt[a[i] - 1]
cur += cnt[... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | cnt = {}
n = int(input())
tot_sum = 0
tot_cnt = 0
res = 0
for y in input().split():
y = int(y)
tot_sum += y
tot_cnt += 1
if y in cnt:
cnt[y] += 1
else:
cnt[y] = 1
tmp_sum = tot_sum
tmp_cnt = tot_cnt
for d in range(-1, 2):
x = y + d
if x in cnt:
... | ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | import sys
def main():
N = int(input())
A = list(map(int, input().split()))
cnt = dict()
pref = 0
ans = 0
for i in range(N):
v = A[i]
cnt[v - 1] = cnt.get(v - 1, 0)
cnt[v] = cnt.get(v, 0)
cnt[v + 1] = cnt.get(v + 1, 0)
add = pref - (v - 1) * cnt[v - 1] -... | IMPORT 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | import sys
mm = {}
sum = 0
ans = 0
line = sys.stdin.readline()
n = int(line)
line = sys.stdin.readline()
ls = line.split()
i = 0
for t in ls:
t = int(ls[i])
ans += t * i + sum
sum -= t
for j in range(-1, 2):
if t + j in mm:
ans += j * mm[t + j]
if t in mm:
mm[t] += 1
... | IMPORT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VA... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
v = list(map(int, input().split()))
mt = {}
sum = 0
ans = 0
for i in range(n - 1, -1, -1):
if not v[i] in mt:
mt[v[i]] = 0
if not v[i] + 1 in mt:
mt[v[i] + 1] = 0
if not v[i] - 1 in mt:
mt[v[i] - 1] = 0
cnt = n - i - 1 - mt[v[i]] - mt[v[i] + 1] - mt[v[i] - 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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBE... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
ans = 0
s = 0
mp = {}
for x in a:
for i in range(-1, 2):
mp[x + i] = 0
for i in range(1, n + 1):
x = a[i - 1]
s += x
mp[x] += 1
ans += i * x - s - (mp[x - 1] - mp[x + 1])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = input()
arr = list(map(int, input().split()))
mp = {}
ans = 0
sums = 0
count = 0
for i in range(int(n) - 1, -1, -1):
sums += arr[i]
if arr[i] in mp:
mp[arr[i]] += 1
else:
mp[arr[i]] = 1
count = count + 1
ans1 = 0
ans2 = 0
if arr[i] + 1 in mp:
ans1 = mp[arr[i] + 1]... | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
dic = {}
sl = list(map(int, input().split()))
summ = [(0) for i in range(n)]
summ[n - 1] = sl[n - 1]
for i in range(n - 2, -1, -1):
summ[i] = summ[i + 1] + sl[i]
for i in sl:
if i in dic:
dic[i] = dic[i] + 1
else:
dic[i] = 1
fuckdiff = 0
for i in range(0, n - 1):
fuckdif... | 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR F... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
arr = input()
arr = arr.split()
for i in range(n):
arr[i] = int(arr[i])
ans = 0
sm = arr[0]
m = {}
m[sm] = 1
for i in range(1, n):
a = 0
b = 0
c = 0
d = 0
x = arr[i]
if not x in m:
m[x] = 0
m[x] += 1
if x - 1 in m:
a = m[x - 1]
if x + 1 in m:
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASS... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(i) for i in input().split()]
b = []
c = {}
s = 0
for i in range(n):
if i == 0:
b.append(a[i])
else:
b.append(b[-1] + a[i])
try:
lower = c[a[i] - 1]
except KeyError:
lower = 0
try:
this = c[a[i]]
except ... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASS... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
nums = list(map(int, input().split()))
pref_sums = []
helper = []
all_numbers = {i: (0) for i in nums}
for i in nums:
all_numbers[i] += 1
cur = {}
for i in range(n):
c = nums[i]
if c not in cur.keys():
cur[c] = 1
else:
cur[c] += 1
lesser_all = 0
lesser_cur = 0
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split(" ")))
s = 0
ss = 0
m = n
mp = {}
while n > 0:
n -= 1
ss += a[n]
s += ss - (m - n) * a[n]
mp.setdefault(a[n] - 1, 0)
mp.setdefault(a[n] + 1, 0)
mp.setdefault(a[n], 0)
s += mp[a[n] - 1] - mp[a[n] + 1]
mp[a[n]] += 1
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FU... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
vs = list(map(int, input().split()))
dc = {}
for x in vs:
dc[x] = 0
for x in vs:
dc[x] += 1
ans = 0
for i in range(n):
ans += i * vs[i]
ans -= (n - 1 - i) * vs[i]
for i in range(n):
dc[vs[i]] -= 1
try:
ans -= dc[vs[i] + 1]
except:
pass
try:
ans +=... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = input().split(" ")
mp = {}
for i in range(len(a)):
a[i] = int(a[i])
mp[a[i]] = 0
for i in a:
mp[i] += 1
sum = []
sum.append(a[0])
for i in range(1, len(a)):
sum.append(sum[i - 1] + a[i])
ans = 0
for i in range(0, n - 1):
cur = sum[n - 1] - sum[i]
rang = n - 1 - i
mp[a[i]... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | import sys
input = sys.stdin.readline
n = int(input())
a = map(int, input().split())
d = {}
ans = sum = num = 0
for x in a:
t = x * num - sum
if x - 1 not in d:
d[x - 1] = 0
if x + 1 not in d:
d[x + 1] = 0
if x not in d:
d[x] = 0
t -= d[x - 1]
t += d[x + 1]
d[x] += 1... | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | def get_int_list(string):
ls = []
sum = ""
for s in string:
if s == " ":
ls.append(int(sum))
sum = ""
else:
sum += s
ls.append(int(sum))
return ls
def extras(d, num):
plus = minus = 0
if num + 1 in d:
plus = d[num + 1]
if num ... | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
cnt = {}
pref = 0
ans = 0
for i in range(n):
ans += i * a[i] - pref
t = 0
if a[i] - 1 in cnt:
ans += cnt[a[i] - 1] * (a[i] - 1)
t += cnt[a[i] - 1]
if a[i] + 1 in cnt:
ans += cnt[a[i] + 1] * (a[i] + 1)
t += cnt[a[i] + 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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUM... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
res = 0
cur = 0
d = {}
for j, i in enumerate(a):
if i - 1 in d:
res -= d[i - 1]
if i + 1 in d:
res += d[i + 1]
if i in d:
d[i] += 1
else:
d[i] = 1
res += i * j - cur
cur += i
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSI... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
nums = [int(i) for i in input().split()]
map = {nums[0]: 1}
result = 0
partial = nums[0]
for a in range(1, n):
if nums[a] in map.keys():
map[nums[a]] += 1
else:
map.update({nums[a]: 1})
result += a * nums[a] - partial
partial += nums[a]
new = 0
if nums[a] - 1 in ... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | import sys
mp = {}
n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
ans = 0
ele = 0
sel = 0
for x in A:
ans = ans + ele * x - sel
if x - 1 in mp:
ans = ans - mp[x - 1]
if x + 1 in mp:
ans = ans + mp[x + 1]
if x in mp:
mp[x] += 1
else:
mp... | IMPORT ASSIGN VAR DICT 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_O... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | maxN = 2 * 10**5
n = int(input())
bad = 0
ghabl = 0
a = list(map(int, input().split()))
for i in range(n):
if i:
bad += a[i] - a[0]
Sum = bad
for i in range(1, n):
bad -= a[i] - a[i - 1]
bad -= (n - 1 - i) * (a[i] - a[i - 1])
Sum += bad
maP = dict()
maP["pedram"] = 1
for i in range(n):
if a[... | ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VA... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | def list_input():
return list(map(int, input().split()))
def map_input():
return map(int, input().split())
def map_string():
return input().split()
ans = 0
d = {}
cnt = 0
n = int(input())
a = list_input()
for i in range(n):
tot = i
cur = cnt
if a[i] in d:
tot -= d[a[i]]
cur... | 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 FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN V... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
cnt = {}
ans = 0
allsum = 0
v = list(map(int, input().split()))
for i in range(n):
x = v[i]
ans += (i - cnt.get(x, 0) - cnt.get(x - 1, 0) - cnt.get(x + 1, 0)) * x - (
allsum
- cnt.get(x, 0) * x
- cnt.get(x - 1, 0) * (x - 1)
- cnt.get(x + 1, 0) * (x + 1)
)
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
res, sum = 0, a[0]
cnt = dict()
cnt[a[0]] = 1
for i in range(1, n):
res += a[i] * i - sum
sum += a[i]
if a[i] - 1 in cnt:
res -= cnt[a[i] - 1]
if a[i] + 1 in cnt:
res += cnt[a[i] + 1]
if a[i] not in cnt:
cnt[a[i]] = 1
e... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
dat = input().split()
cnt = n
sums = 0
mep = {}
for num in dat:
num = int(num)
sums += num
try:
mep[num] += 1
except KeyError:
mep[num] = 1
ans = 0
for num in dat:
num = int(num)
ans += sums - cnt * num
if num + 1 in mep:
ans -= mep[num + 1]
if nu... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR NUMB... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
s = input().split()
a = [int(i) for i in s]
m1 = {}
m2 = {}
sum = 0
def get(m, a):
if a in m:
return m[a]
else:
return 0
ans = 0
for i in range(n):
ans += i * a[i] - sum + get(m1, a[i]) - get(m2, a[i])
if not a[i] - 1 in m1:
m1[a[i] - 1] = 0
if not a[i] +... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = [int(x) for x in input().split()]
s = a[n - 1]
ans = 0
D = dict()
D[a[n - 1]] = 1
for i in range(n - 2, -1, -1):
x = a[i]
c = 0
d = 0
if x - 1 in D:
c = D[x - 1]
if x + 1 in D:
d = D[x + 1]
ans += s - ((n - 1 - i) * x - c + d)
s += x
if x in D:
... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
l = list(map(int, input().split()))
d = dict()
su = 0
ans = 0
for i in range(n):
if l[i] in d:
d[l[i]] += 1
else:
d.update({l[i]: 1})
c = 0
c1 = 0
j = l[i]
if j + 1 in d:
c = d[j + 1]
if j - 1 in d:
c1 = d[j - 1]
ans += i * l[i] - su + c -... | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | def almostDifference(nums):
ht = {}
result = 0
prefix_sum = 0
for i, n in enumerate(nums):
ht.setdefault(n, 0)
ht.setdefault(n - 1, 0)
ht.setdefault(n + 1, 0)
result -= prefix_sum
result += n * i
result -= ht[n - 1] * 1
result -= ht[n + 1] * -1
... | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | mp = dict()
n = int(input())
ans = 0
sum1 = 0
l = list(map(int, input().split()))
for i in range(0, n):
t = l[i]
if t - 1 not in mp:
mp[t - 1] = 0
if t + 1 not in mp:
mp[t + 1] = 0
if t not in mp:
mp[t] = 0
ans += i * t - sum1
ans -= mp[t - 1]
ans += mp[t + 1]
sum... | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_... |
Let's denote a function
$d(x, y) = \left\{\begin{array}{ll}{y - x,} & {\text{if}|x - y|> 1} \\{0,} & {\text{if}|x - y|\leq 1} \end{array} \right.$
You are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
-----Input-----
The ... | n = int(input())
a = list(map(int, input().split()))
bad = 0
for i in range(1, n):
bad += a[i] - a[0]
Sum = bad
for i in range(1, n):
bad -= (a[i] - a[i - 1]) * (n - i)
Sum += bad
maP = dict()
for i in range(n):
if a[i] in maP:
maP[a[i]] += 1
else:
maP[a[i]] = 1
for j in range(-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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUN... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.