description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
_n = sys.stdin.readline()
A = list(map(int, sys.stdin.readline().split()))
partial_sum_cache = set()
count = 0
last_sum = 0
for element in A:
partial_sum = element + last_sum
last_sum = partial_sum
if partial_sum == 0 or partial_sum in partial_sum_cache:
count += 1
partial_sum_cache = set()
last_sum = element
partial_sum_cache.add(last_sum)
print(count) | IMPORT ASSIGN 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 VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def main():
n = int(input())
c = 0
arr = [int(k) for k in input().split()]
m = {0}
s = 0
for i in range(n):
s += arr[i]
if s in m:
c += 1
m = {0}
s = arr[i]
m.add(arr[i])
m.add(s)
print(c)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | input()
li = list(map(int, input().split()))
s = {0}
count, sm = 0, 0
for i in li:
sm = sm + i
if sm in s:
s = {0}
count = count + 1
sm = i
s.add(sm)
print(count) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | for _ in range(1):
n = int(input())
arr = list(map(int, input().split()))
preSums = set()
preSums.add(0)
currSum = 0
total = 0
for i in range(n):
currSum += arr[i]
if currSum in preSums:
total += 1
preSums.clear()
currSum = arr[i]
preSums.add(currSum)
preSums.add(0)
else:
preSums.add(currSum)
print(total) | FOR VAR FUNC_CALL 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
arr = list(map(int, input().split()))
prevSum = set([0])
currSum = ans = 0
for i in range(n):
currSum += arr[i]
if currSum in prevSum:
ans += 1
currSum = arr[i]
prevSum.clear()
prevSum.add(arr[i])
prevSum.add(0)
else:
prevSum.add(currSum)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
arr = [int(x) for x in input().split()]
pre, ans = 0, 0
s = set()
for i in range(n):
if i == 0:
pre = arr[i]
else:
pre += arr[i]
if pre == 0:
ans += 1
pre = arr[i]
s = set()
s.add(pre)
elif pre in s:
ans += 1
pre = arr[i]
s = set()
s.add(pre)
else:
s.add(pre)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
arr = [int(i) for i in input().split()]
cnt = {(0): 1}
cnt[0] = 1
su = 0
res = 0
for i in range(n):
su += arr[i]
temp = -su
if su == 0 or su in cnt:
res += 1
cnt.clear()
cnt[arr[i]] = 1
su = arr[i]
else:
cnt[su] = 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
inline = sys.stdin.readline
def solution(arr):
count = 0
cursum = 0
presum = {(0): -1}
l0 = float("-inf")
for i, num in enumerate(arr):
cursum += num
if cursum in presum and l0 < presum[cursum]:
count += 1
l0 = i - 2
presum[cursum] = i
return count
_ = int(inline().strip())
arr = list(map(int, inline().strip().split()))
print(solution(arr)) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | (N,) = list(map(int, input().split()))
X = list(map(int, input().split()))
for i in range(1, N):
X[i] += X[i - 1]
d = set([0])
R = 0
for i in range(N):
if X[i] in d:
R += 1
d = set()
d.add(X[i])
if i > 0:
d.add(X[i - 1])
print(R) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
b = list(map(int, input().split()))
ans = 0
j = 0
d = dict()
s = 0
d[0] = 1
while j < n:
if s + b[j] in d.keys():
ans += 1
d = dict()
d[0] = 1
s = b[j]
d[s] = 1
else:
s += b[j]
d[s] = 1
j += 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | i = int(input())
d = {0}
s = ans = 0
for x in list(map(int, input().split())):
s += x
if s in d:
ans += 1
s = x
d = {0}
d.add(s)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = [*map(int, input().split())]
d = set()
ans = sm = 0
d.add(0)
for i in a:
sm += i
if sm in d:
ans += 1
d = set([0, i])
sm = i
else:
d.add(sm)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = list(map(int, input().split()))
s = 0
d = {}
ans = 0
for i in range(n):
s = s + a[i]
if s == 0:
ans += 1
s = a[i]
d = {}
d[s] = 1
else:
x = d.get(s, 0)
if x:
ans += 1
s = a[i]
d = {}
d[s] = 1
else:
d[s] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = input()
l = list(map(int, input().split()))
g = set()
g.add(0)
now = 0
delta = 1.0j
ans = 0
for i in l:
now += i
if now in g:
ans += 1
now += delta
g.add(now - i)
g.add(now)
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = list(map(int, input().split()))
freq = {}
freq[a[0]] = 1
Sum = a[0]
cnt = 0
for i in range(n - 1):
if Sum + a[i + 1] == 0 or freq.get(Sum + a[i + 1], 0) > 0:
Sum = a[i + 1]
cnt += 1
freq = {}
freq[Sum] = 1
else:
Sum += a[i + 1]
if freq.get(Sum, 0) == 0:
freq[Sum] = 1
else:
freq[Sum] += 1
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n, arr = int(input()), [int(i) for i in input().split()]
pre = [0] * (n + 5)
pre[1] = arr[0]
for i in range(n - 1):
pre[i + 2] = pre[i + 1] + arr[i + 1]
la = dict()
ans, last = 0, 0
for i in range(n + 1):
t = la.get(pre[i], -1)
if t >= last:
ans += 1
last = i - 1
la[pre[i]] = i
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | from sys import stdin
input = stdin.readline
def answer():
d = dict()
s = 0
d[s] = 1
ans = 0
for i in range(n):
s += a[i]
if s in d:
d = dict()
d[0] = 1
ans += 1
s = a[i]
d[s] = 1
return ans
for T in range(1):
n = int(input())
a = list(map(int, input().split()))
print(answer()) | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL 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 |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def solve():
inf = 1000000007
n = int(input())
arr = [int(v) for v in input().split()]
st = set([0])
s = 0
ans = 0
for i in range(n):
s += arr[i]
if s in st:
ans += 1
s = arr[i]
st = set([s, 0])
else:
st.add(s)
print(ans)
t = 1
for _ in range(t):
solve() | FUNC_DEF ASSIGN VAR NUMBER 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 LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = list(map(int, input().split()))
answer = 0
b = []
for i in a:
if len(b) == 0:
b.append(i)
else:
b.append(b[len(b) - 1] + i)
c = {0}
q = 0
for i in b:
if i in c:
c.clear()
c.add(q)
answer += 1
c.add(i)
q = i
print(answer) | 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 LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
arr = list(map(int, input().split()))
d = dict()
d[0] = None
summ = 0
count = 0
for i in range(n):
summ = summ + arr[i]
if summ in d:
count += 1
summ = arr[i]
d = {(0): None, arr[i]: None}
else:
d[summ] = None
print(count) | 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 NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER VAR VAR NONE NONE ASSIGN VAR VAR NONE EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
(*a,) = map(int, input().split())
h = {(0): 1}
s = 0
ans = 0
for i in range(n):
s += a[i]
if s in h:
ans += 1
s = a[i]
h = {(0): 1}
h[s] = i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
S = [0]
for a in A:
S.append(S[-1] + a)
ANS = 0
SET = set()
for i in range(n + 1):
if S[i] in SET:
ANS += 1
SET = {S[i], S[i - 1]}
else:
SET.add(S[i])
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = list(map(int, input().split()))
sett = set([0])
summ = 0
count = 0
i = 0
while i < n:
summ += a[i]
if summ in sett:
count += 1
sett.clear()
sett.add(0)
sett.add(a[i])
summ = a[i]
i += 1
else:
sett.add(summ)
i += 1
print(count) | 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 LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def solve(n, ar):
d = set()
curr = 0
ans = 0
d.add(0)
for i in range(n):
curr += ar[i]
if curr in d:
ans += 1
d = set()
d.add(0)
curr = ar[i]
d.add(curr)
print(ans)
n = int(input())
ar = list(map(int, input().split()))
solve(n, ar) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR 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 VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
n += 1
a = [int(i) for i in input().split()]
a = [0] + a
for i in range(1, n):
a[i] = a[i] + a[i - 1]
d = dict()
back = [-1] * n
for i in range(n):
if a[i] in d:
back[i] = d[a[i]]
d[a[i]] = i
i = 0
b = 0
cnt = 0
while i < n:
if back[i] - b >= 0:
i -= 1
b = i
cnt += 1
continue
i += 1
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | res = 0
summ = 0
my_set = {0}
n = int(input())
mas = list(map(int, input().split()))
for num in mas:
summ += num
if summ in my_set:
my_set = {0}
res += 1
summ = num
my_set.add(summ)
print(res) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | from sys import stdin
tt = 1
for loop in range(tt):
dic = {}
dic[0] = 1
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
now = 0
ans = 0
for i in a:
now += i
if now in dic:
ans += 1
dic = {}
dic[0] = 1
now = i
dic[now] = 1
print(ans) | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
myset = set([0])
sum = 0
ans = 0
arrays = map(int, input().split())
for ii in arrays:
sum += ii
if sum in myset:
ans = ans + 1
myset.clear()
myset.add(0)
myset.add(ii)
sum = ii
else:
myset.add(sum)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def findIndex(start, a, n):
st = set()
sm = 0
while start < n:
sm += a[start]
if sm == 0 or sm in st:
return start
start += 1
st.add(sm)
return -1
n = int(input())
a = list(map(int, input().split()))
k = 0
ind = 0
ans = 0
while ind != -1:
ind = findIndex(k, a, n)
if ind != -1:
k = ind
ans += 1
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
lst = list(map(int, input().split()))
pref = dict()
sum = lst[0]
pref[lst[0]] = 1
pref[0] = 1
res = 0
for r in range(1, n):
sum += lst[r]
if pref.get(sum, 0):
pref = dict()
pref[0] = 1
res += 1
sum = lst[r]
pref[sum] = 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | l = []
if 1 == 1:
n = int(input())
a = list(map(int, input().split()))
q = set()
sum = 0
count = 0
for i in range(n):
sum = sum + a[i]
if sum in q or sum == 0:
sum = a[i]
q.clear()
q.add(a[i])
count = count + 1
else:
q.add(sum)
l.append(count)
for i in l:
print(i) | ASSIGN VAR LIST IF 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
ch = input()
L = [int(i) for i in ch.split()]
for i in range(1, n):
L[i] += L[i - 1]
L = [0] + L
d = dict()
s = set()
s.add(0)
d[0] = 0
pos = -1
nb = 0
for i in range(1, n + 1):
if L[i] in s:
s = set()
s.add(L[i - 1])
nb += 1
s.add(L[i])
print(nb) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
n = int(input())
ar = list(map(int, input().split()))
li = []
su = 0
for i in range(n):
su += ar[i]
li.append(su)
se = {0}
ans = 0
shift = 0
for i in range(n):
if not li[i] - shift in se:
se.add(li[i] - shift)
else:
ans += 1
shift = li[i - 1]
se = {0, li[i] - shift}
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = list(map(int, input().split()))
p = 0
c = 0
d = {(0): 1}
q = 10**20
for j in range(n):
p = p + a[j]
if p in d:
c = c + 1
p = p - a[j]
p = p + q
d[p] = 1
p = p + a[j]
d[p] = 1
print(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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
s = input()
return list(s[: len(s) - 1])
def invr():
return map(int, input().split())
n = inp()
a = inlt()
cnt = 0
s = {0}
prefix = 0
for i in range(0, n):
prefix += a[i]
if prefix in s:
cnt += 1
s = {0, a[i]}
prefix = a[i]
else:
s.add(prefix)
print(cnt) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
done = {a[0]: 0, (0): 0}
s = a[0]
ans = 0
for i in range(1, n):
s += a[i]
if s in done:
done = {(0): 0, a[i]: 0}
ans += 1
s = a[i]
else:
done[s] = 0
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR DICT NUMBER VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = [int(i) for i in input().split()]
sum1 = a[0]
res = 0
t = 1
d = {}
d[0] = t
d[sum1] = t
for i in range(1, n):
sum1 += a[i]
if d.get(sum1) is not None and d[sum1] == t:
res += 1
sum1 = a[i]
t += 1
d[0] = t
d[sum1] = t
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR NONE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def main():
_, array = int(input()), list(map(int, input().split(" ")))
answer = 0
sumSpace = [0]
for elem in array:
sumSpace.append(sumSpace[-1] + elem)
sumSpace.pop(0)
verify = {0}
q = 0
for elem in sumSpace:
if elem in verify:
verify = {q}
answer += 1
verify.add(elem)
q = elem
print(answer)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | from itertools import accumulate
from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + "\n")
n = int(input())
a = list(map(int, input().split()))
a = accumulate(a)
pos = dict()
pos[0] = 0
e = 0
ans = 0
for i, ele in enumerate(a):
if ele in pos:
if pos[ele] + 1 >= e:
e = i
ans += 1
pos[ele] = i
print(ans) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | case = int(input())
s = {0}
cur = 0
ans = 0
lst = [int(x) for x in input().split(" ")]
for i in range(case):
cur += lst[i]
if cur in s:
ans += 1
s = {0}
cur = lst[i]
s.add(cur)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.buffer.readline
def solution():
n = int(input())
l = list(map(int, input().split()))
d = {}
d[0] = 1
ans = 0
pre = 0
for i in range(n):
pre += l[i]
if d.get(pre, 0) > 0:
ans += 1
pre = l[i]
d.clear()
d[0] = 1
d[l[i]] = 1
else:
d[pre] = 1
print(ans)
t = 1
for _ in range(t):
solution() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def readis():
return map(int, input().strip().split())
n = int(input())
xs = readis()
cumsums = {0}
count_inserts = 0
last = 0
for x in xs:
last += x
if last in cumsums:
count_inserts += 1
cumsums = {0}
last = x
cumsums.add(last)
print(count_inserts) | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def main():
n = int(input())
a = list(map(int, input().split()))
s = {0}
acc = 0
ans = 0
for x in a:
acc += x
if acc in s:
ans += 1
s = {0, x}
acc = x
else:
s.add(acc)
print(ans)
tn = 1
for _ in range(tn):
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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
from itertools import accumulate
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [[c for j in range(b)] for i in range(a)]
def list3d(a, b, c, d):
return [[[d for k in range(c)] for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [
[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)
]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
N = INT()
A = LIST()
acc = [0] + list(accumulate(A))
D = {}
ok = -1
ans = 0
for i in range(N + 1):
if acc[i] in D and D[acc[i]] > ok:
ok = i - 2
ans += 1
D[acc[i]] = i
print(ans) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = list(map(int, input().split()))
aset = set()
aset.add(0)
sum = 0
ans = 0
for i in range(0, n):
sum = sum + a[i]
if sum in aset:
ans = ans + 1
aset = set()
aset.add(0)
sum = a[i]
aset.add(sum)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | s = input()
s = input()
s1 = s.split()
l = [int(i) for i in s1]
cnt = 0
dp = [(0) for i in range(len(l))]
dp[0] = l[0]
d = {(0): True}
stack = []
for i in range(len(l)):
dp[i] = dp[i - 1] + l[i]
if dp[i] in d:
cnt = cnt + 1
dp[i] = l[i]
d = {(0): True}
d[dp[i]] = True
print(cnt) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
a = input().split()
s = [0]
for ai in a:
s.append(s[-1] + int(ai))
answer = 0
p = 0
ss = set()
for si in s:
if si in ss:
answer += 1
ss = {p, si}
else:
ss.add(si)
p = si
print(answer) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
A = list(map(int, input().split()))
d = {}
ans = 0
d[A[0]] = 1
s = A[0]
st = 0
for i in range(1, n):
s += A[i]
if s in d.keys() or s == 0:
ans += 1
s = A[i]
d = {}
d[s] = 1
else:
d[s] = 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 ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
arr = list(map(int, input().split()))
s = set([0])
out = 0
pre = 0
for i in range(n):
pre += arr[i]
if pre not in s:
s.add(pre)
else:
out += 1
s = set([0, arr[i]])
pre = arr[i]
print(out) | 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 LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
l = input().split()
li = [int(i) for i in l]
hashi = dict()
pref = 0
ans = 0
hashi[pref] = 1
for i in range(n):
pref += li[i]
if pref in hashi:
ans += 1
pref = li[i]
hashi = dict()
hashi[0] = 1
hashi[pref] = 1
else:
hashi[pref] = 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
segments = {0}
cnt, tmp = 0, 0
for i in range(n):
tmp += a[i]
if tmp in segments:
cnt += 1
segments = {0}
tmp = a[i]
segments.add(tmp)
print(cnt) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | from sys import stdin
stdin.readline
def mp():
return list(map(int, stdin.readline().strip().split()))
def it():
return int(stdin.readline().strip())
n = it()
l = mp()
s = set()
s.add(0)
ans = count = 0
for i in range(n):
ans += l[i]
if ans in s:
count += 1
ans = l[i]
s = set()
s.add(0)
s.add(ans)
print(count) | EXPR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | input()
prefix_sums = set()
prefix_sums_shift = 1000
result = 0
for x in map(int, input().split()):
if prefix_sums_shift - x in prefix_sums:
result += 1
prefix_sums = set()
prefix_sums_shift = 0
prefix_sums_shift -= x
prefix_sums.add(x + prefix_sums_shift)
print(result) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def nonZeroSegment(n, arr):
count = 0
map = {(0): 0}
sum = 0
for i in range(0, n):
sum += arr[i]
if sum in map:
count += 1
map = {}
map = {(0): 0, arr[i]: arr[i]}
sum = arr[i]
map[sum] = sum
print(count)
return
n = int(input())
arr = [int(i) for i in input().split()][:n]
nonZeroSegment(n, arr) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | input()
a = map(int, input().split())
p = set([0])
c = 0
ans = 0
for x in a:
c += x
if c in p:
ans += 1
p = set([0])
c = x
p.add(c)
print(ans) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | def solve(n, arr):
ans = 0
res = {0}
current = 0
for i in range(n):
current += arr[i]
if current in res:
ans += 1
res = {0}
current = arr[i]
res |= {current}
return ans
def main():
n = int(input())
arr = list(map(int, input().split(" ")))
ans = solve(n, arr)
print(ans)
return ans
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF 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 FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
A = [int(i) for i in input().split()]
cumm = [A[0]] * n
d = {A[0]: [0]}
count = 0
last_idx = -1
for i in range(1, n):
cumm[i] = cumm[i - 1] + A[i]
if cumm[i] == 0 and last_idx == -1:
count += 1
last_idx = i
elif cumm[i] in d:
l = d[cumm[i]]
if l[-1] >= last_idx - 1:
count += 1
last_idx = i
if cumm[i] not in d:
d[cumm[i]] = []
d[cumm[i]].append(i)
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR DICT VAR NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
def main():
n = int(input())
A = list(map(int, input().split()))
G = {}
s = A[0]
G[0] = 0
G[s] = 1
B = []
for i in range(1, n):
s = s + A[i]
if s in G:
B.append([G[s], i])
G[s] = i + 1
else:
G[s] = i + 1
B.sort()
ans = 0
xv, yv = 0, 0
flag = 1
for val in B:
flag = 0
x, y = val[0], val[1]
if x < yv:
xv = x
yv = min(yv, y)
else:
flag = 1
ans = ans + 1
xv, yv = x, y
print(ans)
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
arr = list(map(int, input().split()))
ans = 0
pref = [0]
prefset = {0}
for i in range(n):
now = pref[-1] + arr[i]
if now in prefset:
ans += 1
pref = [0, arr[i]]
prefset = {0, arr[i]}
else:
pref.append(now)
prefset.add(now)
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 LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n = int(input())
arr = list(map(int, input().split()))
occ = {(0): [-1]}
s = 0
ans = 0
MAX = 999999999999999999
ind = 0
while ind < len(arr):
val = arr[ind]
s += val
if s in occ:
ans += 1
s -= val
s += MAX
ind -= 1
else:
occ[s] = 1
ind += 1
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
s = set()
s.add(0)
res = 0
curr_sum = 0
for idx, el in enumerate(a):
curr_sum += el
if curr_sum in s:
res += 1
curr_sum = el
s = set()
s.add(0)
s.add(curr_sum)
print(res) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | int(input())
d = set({0})
s = 0
ans = 0
for i in list(map(int, input().split())):
s += i
if s in d:
ans += 1
d = set({0, i})
s = i
continue
d.add(s)
print(ans) | EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | n = int(input())
l = list(map(int, input().strip().split()))
ans = 0
lastInd = {}
curSum = 0
lastInd[0] = -1
for i in range(n):
curSum += l[i]
if curSum in lastInd:
ans += 1
lastInd = {}
lastInd[0] = i - 1
lastInd[l[i]] = i
curSum = l[i]
else:
lastInd[curSum] = i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Input-----
The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) β the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) β the description of Kolya's array.
-----Output-----
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.
-----Examples-----
Input
4
1 -5 3 2
Output
1
Input
5
4 -2 3 -9 2
Output
0
Input
9
-1 1 -1 1 -1 1 1 -1 -1
Output
6
Input
8
16 -5 -11 -15 10 5 4 -4
Output
3
-----Note-----
Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything. | _ = input()
numbers = [int(i) for i in input().split()]
seen_sum = {0}
tot = 0
num_zeros = 0
index = 0
while index < len(numbers):
tot += numbers[index]
if tot in seen_sum:
num_zeros += 1
tot = 0
seen_sum = {0}
else:
seen_sum.add(tot)
index += 1
print(num_zeros) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief. | from sys import stdin
lines = stdin.readlines()
n, s = map(int, lines[0].split())
counts = list(map(int, lines[1].split()))
totalwrong = 0
if counts[s - 1] > 0:
totalwrong += 1
counts[s - 1] = 0
maxlevel = max(counts)
levelcount = [0] * (max(counts) + 1)
for c in counts:
levelcount[c] += 1
curwrong = levelcount[0] - 1
levelcount[0] = 1
totalwrong += curwrong
curlevel = 0
while curlevel <= maxlevel:
lc = levelcount[curlevel]
if lc == 0:
if curwrong > 0:
curwrong -= 1
levelcount[curlevel] = 1
else:
levelcount[maxlevel] -= 1
levelcount[curlevel] = 1
totalwrong += 1
while levelcount[maxlevel] == 0:
maxlevel -= 1
curlevel += 1
print(totalwrong) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief. | [n, s] = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
mistakes = 0
mistakes += a[s - 1] is not 0
a[s - 1] = 0
numSuperiors = [0] * (2 * 100000 + 100)
for superiors in a:
numSuperiors[superiors] += 1
cachedMistakes = 0
while numSuperiors[0] != 1:
cachedMistakes += 1
numSuperiors[0] -= 1
rightIndex = len(numSuperiors) - 1
leftIndex = 0
while True:
while True:
if numSuperiors[leftIndex] == 0 and cachedMistakes != 0:
numSuperiors[leftIndex] += 1
cachedMistakes -= 1
mistakes += 1
if numSuperiors[leftIndex] == 0:
break
leftIndex += 1
while numSuperiors[rightIndex] == 0:
rightIndex -= 1
if leftIndex >= rightIndex:
break
numSuperiors[rightIndex] -= 1
cachedMistakes += 1
print(mistakes) | ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief. | n, s = map(int, input().split())
A = list(map(int, input().split()))
if A[s - 1] != 0:
per = 1
A[s - 1] = 0
else:
per = 0
A.sort()
maxs = max(A)
ans = [0] * (maxs + 1)
answer = maxs + 1
o = -1
for j in range(n):
if A[j] == 0:
o += 1
if ans[A[j]] == 0:
ans[A[j]] = 1
answer -= 1
an = per + max(o, answer)
for j in range(n - 2, -1, -1):
for t in range(A[j + 1] - 1, A[j] - 1, -1):
if ans[t] == 0:
answer -= 1
an = min(an, per + max(answer, o + n - j - 1))
print(an) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief. | n, s = [int(i) for i in input().split()]
s -= 1
n -= 1
d = 0
l = [int(i) for i in input().split()]
if l[s] != 0:
d += 1
l = l[:s] + l[s + 1 :]
for i in range(0, len(l)):
if l[i] == 0:
l[i] = n + 5
l.sort()
j = 0
i = 0
c = 0
while 1:
while i < len(l) and j == l[i]:
i += 1
if i >= len(l):
break
elif l[i] - j == 1:
j += 1
i += 1
continue
else:
l.pop()
d += 1
j += 1
print(d) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief. | f = lambda: map(int, input().split())
n, s = f()
c = [0] * n
t = list(f())
for i in t:
c[i] += 1
k = t[s - 1]
c[k] -= 1
d = c[0]
c += [d]
d += k > 0
i, j = 1, n
while i < j:
if c[i]:
i += 1
elif c[j]:
c[j] -= 1
i += 1
d += j < n
else:
j -= 1
print(d) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief. | n, root = map(int, input().split())
a = list(map(int, input().split()))
def push(d, x, val):
if x not in d:
d[x] = 0
d[x] += val
if d[x] == 0:
del d[x]
d = {}
for x in a:
push(d, x, 1)
min_ = 0
root -= 1
inf = 9999999
if a[root] != 0:
min_ += 1
push(d, a[root], -1)
push(d, 0, 1)
if 0 in d and d[0] > 1:
add = d[0] - 1
min_ += add
push(d, inf, add)
d[0] = 1
S = [[val, num] for val, num in sorted(d.items(), key=lambda x: x[0])]
cur = -1
i = 0
while i < len(S):
remain = S[i][0] - (cur + 1)
while remain > 0:
val, num = S[-1]
if val == S[i][0]:
if val != inf:
min_ += min(remain, num)
break
else:
add = min(num, remain)
remain -= add
if val != inf:
min_ += add
if num == add:
S.pop()
else:
S[-1][1] -= add
cur = S[i][0]
i += 1
print(min_) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
n = int(input())
uf = UnionFind(n * n)
grid = []
ans = []
edges = []
for i in range(n):
a = list(map(int, input().split()))
grid.append(a)
relations = []
for i in range(n):
for j in range(i, n):
if i == j:
ans.append(grid[i][j])
continue
relations.append((i, j, grid[i][j]))
relations.sort(key=lambda thing: thing[2])
rootfinder = [0] * (500 * 500)
for i in range(n):
rootfinder[i] = i
k = n
for p in range(len(relations)):
i, j, value = relations[p]
if uf.same(i, j):
continue
a = uf.find(i)
b = uf.find(j)
l = rootfinder[a]
m = rootfinder[b]
if ans[l] == value:
edges.append([m + 1, l + 1])
uf.union(a, b)
newroot = uf.find(a)
rootfinder[newroot] = l
elif ans[m] == value:
edges.append([l + 1, m + 1])
uf.union(a, b)
newroot = uf.find(a)
rootfinder[newroot] = m
else:
ans.append(value)
uf.union(a, b)
newroot = uf.find(a)
rootfinder[newroot] = k
edges.append([m + 1, k + 1])
edges.append([l + 1, k + 1])
k += 1
print(k)
print(*ans)
print(rootfinder[uf.find(0)] + 1)
for edge in edges:
print(*edge) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | n = 3
pairs = [[2, 5, 7], [5, 1, 7], [7, 7, 4]]
n = int(input())
pairs = [[None for _ in range(n)] for _ in range(n)]
for i in range(n):
pairs[i] = [int(x) for x in input().split(" ")]
salaries = [pairs[i][i] for i in range(n)]
report_sets = [[i] for i in range(n)]
done = [(False) for _ in range(n)]
boss = None
relationships = []
report_sets_to_indices = {}
while True:
index = None
for i, d in enumerate(done):
if not d:
index = i
break
if index is None:
break
done[index] = True
reports = report_sets[index]
salary = salaries[index]
direct_manager_salary = min([x for x in pairs[reports[0]] if x > salary])
direct_manager_reports = tuple(
x for x in range(n) if pairs[reports[0]][x] <= direct_manager_salary
)
if direct_manager_reports not in report_sets_to_indices:
salaries.append(direct_manager_salary)
report_sets.append(direct_manager_reports)
report_sets_to_indices[direct_manager_reports] = len(salaries)
if len(direct_manager_reports) == n:
boss = len(salaries)
done.append(True)
else:
done.append(False)
relationships.append((index + 1, report_sets_to_indices[direct_manager_reports]))
print(len(salaries))
print(" ".join([str(x) for x in salaries]))
print(boss)
for r in relationships:
print("{} {}".format(r[0], r[1])) | ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR DICT WHILE NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR NUMBER |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | import sys
input = sys.stdin.readline
class DSU:
def __init__(self, n):
self.uf = {i: i for i in range(n)}
def find(self, x):
self.uf.setdefault(x, x)
if self.uf[x] != x:
self.uf[x] = self.find(self.uf[x])
return self.uf[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
self.uf[px] = py
k = int(input())
A = [list(map(int, input().split())) for _ in range(k)]
B = [A[i][i] for i in range(k)]
C = sorted((A[i][j], i + 1, j + 1) for i in range(k) for j in range(i + 1, k))
ans = []
dsu = DSU(k)
for c, i, j in C:
pi, pj = dsu.find(i), dsu.find(j)
if pi == pj:
continue
if c > B[pi - 1] and c > B[pj - 1]:
B.append(c)
ans.append((pi, len(B)))
ans.append((pj, len(B)))
dsu.union(pi, len(B))
dsu.union(pj, len(B))
elif c == B[pi - 1]:
ans.append((pj, pi))
dsu.union(pj, pi)
else:
ans.append((pi, pj))
dsu.union(pi, pj)
print(len(B))
print(*B)
print(dsu.find(1))
for a in ans:
print(*a) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | def f(s):
global num, sal, super
rootnum = num
num += 1
groups = []
rootsal = 0
while len(s) > 0:
x = s.pop()
if rootsal == 0:
rootsal = max([a[x][i] for i in s])
sal[rootnum] = rootsal
sib = set([i for i in s if a[x][i] != rootsal])
sib.add(x)
rest = s.difference(sib)
if len(sib) == 1:
super[x + 1] = rootnum
else:
groups += [sib]
s = rest
for s in groups:
managernum = f(s)
super[managernum] = rootnum
return rootnum
n = int(input())
a = [[int(x) for x in input().split()] for _ in range(n)]
sal = {(x + 1): a[x][x] for x in range(n)}
super = {}
num = n + 1
head = f(set(range(n)))
print(num - 1)
print(" ".join([str(x) for x in sal.values()]))
print(head)
[print(f"{x} {super[x]}") for x in super] | FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR LIST VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR VAR VAR |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | from sys import stdin, stdout
def dogeforces(c_a, a_a):
global n
l_a = [i for i in range(n)]
return dfs(c_a, a_a, l_a)
def dfs(c_a, a_a, l_a):
global n
global d_a
if len(l_a) == 1:
return l_a[0]
root = -1
for l in l_a:
root = max(root, a_a[l_a[0]][l])
gp_a = []
for l in l_a:
gp = -1
for j in range(len(gp_a)):
g = gp_a[j][0]
if a_a[l][g] != root:
gp = j
break
if gp == -1:
gp = len(gp_a)
gp_a.append([])
gp_a[gp].append(l)
v = n
c_a.append(root)
n += 1
for gp in gp_a:
sr = dfs(c_a, a_a, gp)
d_a.append([sr, v])
return v
n = int(stdin.readline())
a_a = []
c_a = []
for i in range(n):
a_a.append(list(map(int, stdin.readline().split())))
c_a.append(a_a[i][i])
d_a = []
root = dogeforces(c_a, a_a)
stdout.write(str(n) + "\n")
stdout.write(" ".join(map(str, c_a)) + "\n")
stdout.write(str(root + 1) + "\n")
for d in d_a:
stdout.write(str(d[0] + 1) + " " + str(d[1] + 1) + "\n") | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | import sys
input = sys.stdin.readline
n = int(input())
low = []
lowsup = [0] * n
for i in range(n):
lowsup[i] = []
high = []
highsub = []
inp = []
for i in range(n):
inp.append(list(map(int, input().split())))
for i in range(n):
sal = []
for j in range(n):
sal.append([inp[i][j], j])
sal.sort()
low.append(sal[0][0])
alr = set()
for x in lowsup[i]:
alr.add(high[x])
isnew = True
for j in range(1, n):
if not isnew:
break
if sal[j][0] > sal[j - 1][0]:
if sal[j][0] in alr:
isnew = False
else:
isnew = True
high.append(sal[j][0])
lowsup[i].append(len(high) - 1)
lowsup[sal[j][1]].append(len(high) - 1)
highsub.append(i)
elif isnew:
lowsup[sal[j][1]].append(len(high) - 1)
print(n + len(high))
print(*(low + high))
print(n + high.index(max(high)) + 1)
for i in range(n):
ans = 0
sal = max(high) + 1
for x in lowsup[i]:
if high[x] < sal:
sal = high[x]
ans = x + n + 1
print(i + 1, ans)
for i in range(len(high)):
ans = 0
sal = max(high) + 1
for x in lowsup[highsub[i]]:
if high[x] > high[i] and high[x] < sal:
sal = high[x]
ans = x + n + 1
if ans:
print(i + n + 1, ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
val = [-1] * 10**6
par = [-1] * 10**6
for i in range(n):
val[i] = a[i][i]
edgetank = []
for i in range(n):
for j in range(i + 1, n):
edgetank.append((a[i][j], i, j))
edgetank.sort()
def findroot(x):
now = x
while par[now] >= 0:
now = par[now]
return now
newvertex = n
for w, u, v in edgetank:
u_root = findroot(u)
v_root = findroot(v)
if u_root == v_root:
continue
if val[u_root] == w:
par[v_root] = u_root
elif val[v_root] == w:
par[u_root] = v_root
else:
par[u_root] = newvertex
par[v_root] = newvertex
val[newvertex] = w
newvertex += 1
print(newvertex)
print(*val[:newvertex])
emp_sup = []
for i in range(newvertex):
if par[i] != -1:
emp_sup.append("{} {}".format(i + 1, par[i] + 1))
print(newvertex)
print(*emp_sup, sep="\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
The Dogeforces company has $k$ employees. Each employee, except for lower-level employees, has at least $2$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.
The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 500$) β the number of lower-level employees.
This is followed by $n$ lines, where $i$-th line contains $n$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,n}$ ($1 \le a_{i,j} \le 5000$) β salary of the common supervisor of employees with numbers $i$ and $j$. It is guaranteed that $a_{i,j} = a_{j,i}$. Note that $a_{i,i}$ is equal to the salary of the $i$-th employee.
-----Output-----
In the first line, print a single integer $k$ β the number of employees in the company.
In the second line, print $k$ integers $c_1, c_2, \dots, c_k$, where $c_i$ is the salary of the employee with the number $i$.
In the third line, print a single integer $r$ β the number of the employee who is the head of the company.
In the following $k-1$ lines, print two integers $v$ and $u$ ($1 \le v, u \le k$) β the number of the employee and his direct supervisor.
Note that the lower-level employees have numbers from $1$ to $n$, and for the rest of the employees, you have to assign numbers from $n+1$ to $k$. If there are several correct company structures, you can print any of them.
-----Examples-----
Input
3
2 5 7
5 1 7
7 7 4
Output
5
2 1 4 7 5
4
1 5
2 5
5 4
3 4
-----Note-----
One of the possible structures in the first example: | import sys
input = sys.stdin.readline
idx = 0
vis = None
a = None
e = None
def rec(c):
global idx
if len(c) == 1:
z = c[0]
vis[z] = -1
cost[z] = a[z][z]
return z
idx += 1
now = idx
z = 0
for i in c:
vis[i] = -1
for j in c:
if i != j:
z = max(z, a[i][j])
cost[now] = z
n = len(a)
cc = 0
w = []
for i in c:
if vis[i] < 0:
q = [i]
cc += 1
vis[i] = cc
qi = 0
while qi < len(q):
v = q[qi]
qi += 1
for j in c:
if v != j and a[v][j] < z and vis[j] < 0:
q.append(j)
vis[j] = cc
w.append(q)
for b in w:
e.append((rec(b), now))
return now
def solve():
global idx, vis, a, e, cost
n = int(input())
a = [None] * n
for i in range(n):
a[i] = list(map(int, input().split()))
idx = n - 1
e = []
vis = [-1] * len(a)
cost = [0] * (2 * n + 4)
root = rec(list(range(n)))
print(idx + 1)
print(*cost[: idx + 1])
print(root + 1)
for u, v in e:
print(u + 1, v + 1)
solve() | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | import sys
class Solution:
def minSteps(self, a, n, k):
cnt = 0
self.arr = sorted(a)
pre_sum = []
for ele in self.arr:
if not pre_sum:
curr = 0
else:
curr = pre_sum[-1]
pre_sum.append(curr + ele)
ans = sys.maxsize
for i, ele in enumerate(self.arr):
if i == 0:
drop_coins = 0
else:
drop_coins = pre_sum[i - 1]
mn = ele
index = self.get_index(mn + k)
if index != -1:
tot = pre_sum[-1] - pre_sum[index - 1]
expected = (mn + k) * (n - index)
drop_coins += tot - expected
ans = min(ans, drop_coins)
return ans
def get_index(self, key):
l = 0
u = len(self.arr) - 1
while l < u:
mid = (l + u) // 2
if self.arr[mid] <= key:
l = mid + 1
else:
u = mid
if self.arr[u] <= key:
return -1
return u
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, K = map(int, input().split())
A = list(map(int, input().split()))
ob = Solution()
print(ob.minSteps(A, N, K)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | import sys
class Solution:
def minSteps(self, a, n, k):
a.sort()
suma = [0] * n
suma[0] = a[0]
ans = sys.maxsize
for i in range(1, n):
suma[i] = suma[i - 1] + a[i]
for i in range(n):
res = 0
key = a[i] + k
num = self.bs(a, key, i + 1, n - 1)
if num != -1:
ind = n - 1 - (num - 1)
res = suma[n - 1] - suma[num - 1]
res = res - key * ind
if i > 0:
res += suma[i - 1]
ans = min(ans, res)
return ans
def bs(self, arr, k, l, h):
res = -1
while l <= h:
mid = (l + h) // 2
if arr[mid] <= k:
l = mid + 1
else:
res = mid
h = mid - 1
return res | IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def minSteps(self, a, n, k):
a = sorted(a)
p0 = 0
r0 = 0
p1 = len(a) - 1
r1 = 0
while a[p1] - k - a[p0] > 0:
r1 += a[p1] - k - a[p0]
p1 -= 1
p1 += 1
memo = [r0 + r1]
while p0 + 1 < len(a):
p0 += 1
r0 += a[p0 - 1]
ap = a[p1 - 1]
while p1 < len(a):
if k + a[p0] - a[p1] > 0:
r1 -= a[p1] - (k + a[p0 - 1])
p1 += 1
else:
break
r1 -= (len(a) - p1) * (a[p0] - a[p0 - 1])
memo.append(r0 + r1)
return min(memo) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | def upper_bound(A, L, R, K):
pos = R + 1
while L <= R:
M = (L + R) // 2
if A[M] > K:
pos = M
R = M - 1
else:
L = M + 1
return pos
class Solution:
def minSteps(self, a, N, K):
p = [0] * N
a.sort()
p[0] = a[0]
for i in range(1, N):
p[i] = p[i - 1] + a[i]
ans = (1 << 31) - 1
prev = 0
for i in range(N):
pos = upper_bound(a, i, N - 1, a[i] + K)
if i > 0 and a[i] != a[i - 1]:
prev = p[i - 1]
ans = min(ans, prev + p[N - 1] - p[pos - 1] - (N - pos) * (a[i] + K))
return ans | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL 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 ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def _nextGreaterThanX(self, arr, start_idx, x):
left, right = start_idx + 1, len(arr) - 1
ans = len(arr)
while left <= right:
mid = left + (right - left) // 2
if arr[mid] > x:
ans = min(ans, mid)
right = mid - 1
else:
left = mid + 1
return ans
def minSteps(self, A, N, K):
A.sort()
prefix_sum = [0] * N
_sum = 0
ans = float("inf")
if N == 1:
return 0
if A[-1] - A[0] <= K:
return 0
for idx, val in enumerate(A):
_sum += val
prefix_sum[idx] = _sum
for idx, val in enumerate(A):
greater_ele_idx = self._nextGreaterThanX(A, idx, val + K)
remove_coins = None
if greater_ele_idx == len(A):
remove_coins = prefix_sum[idx - 1] if idx > 0 else 0
else:
a = prefix_sum[len(A) - 1] - prefix_sum[greater_ele_idx - 1]
b = (len(A) - greater_ele_idx) * (val + K)
remove_coins = a - b + (prefix_sum[idx - 1] if idx > 0 else 0)
ans = min(ans, remove_coins)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NONE IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def minSteps(self, A, N, K):
A.sort()
ps = [0]
def bs(l, r, target):
while l < r:
m = (l + r) // 2
if A[m] >= target:
r = m
else:
l = m + 1
return l
for i in range(N):
ps.append(A[i] + ps[-1])
answer = float("inf")
for i in range(N):
small = A[i]
remove_all = ps[i]
idx = bs(i, N - 1, small + K + 1)
if A[idx] < small + K + 1:
idx = N
remove_ones = ps[-1] - ps[idx]
remove_ones -= (N - idx) * (small + K)
answer = min(answer, remove_all + remove_ones)
return answer | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | import sys
class Solution:
def findindex(self, low, high, minm, arr):
res = -1
while low <= high:
mid = (low + high) // 2
if arr[mid] > minm:
res = mid
high = mid - 1
else:
low = mid + 1
return res
def minSteps(self, arr, n, k):
arr.sort()
prefix_sum = [0] * n
prefix_sum[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
prefix_sum[i] = arr[i] + prefix_sum[i + 1]
sum1 = 0
ans = sys.maxsize
for i in range(n):
minm = arr[i] + k
index = self.findindex(i, n - 1, minm, arr)
if index == -1:
ans = min(ans, sum1)
continue
currsum = prefix_sum[index] - (n - index) * (arr[i] + k)
currsum += sum1
ans = min(ans, currsum)
sum1 += arr[i]
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, K = map(int, input().split())
A = list(map(int, input().split()))
ob = Solution()
print(ob.minSteps(A, N, K)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def binarysearch(self, arr, l, r, k):
if l <= r:
mid = (l + r) // 2
if arr[mid] > k:
return self.binarysearch(arr, l, mid - 1, k)
else:
return self.binarysearch(arr, mid + 1, r, k)
return l
def minSteps(self, A, N, K):
A.sort()
presum = [0] * N
presum[0] = A[0]
for i in range(1, N):
presum[i] = presum[i - 1] + A[i]
l = self.binarysearch(A, 0, N - 1, A[0] + K)
m1 = presum[-1] - presum[l - 1] - (N - l) * (A[0] + K)
m2 = 100000000
for i in range(1, N):
l = self.binarysearch(A, 0, N - 1, A[i] + K)
m2 = presum[i - 1] + (presum[-1] - presum[l - 1]) - (N - l) * (A[i] + K)
m1 = min(m1, m2)
return m1 | CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def binarySearch(self, A, N, ele):
start = 0
end = N - 1
res = -1
while start <= end:
mid = (start + end) // 2
if A[mid] > ele:
res = mid
end = mid - 1
else:
start = mid + 1
return res
def minSteps(self, A, N, K):
A.sort()
ans = 10000000000.0
prev = 0
prefix = []
for i in range(N):
if i == 0:
prefix.append(A[0])
else:
prefix.append(prefix[i - 1] + A[i])
for i in range(N):
index = self.binarySearch(A, N, A[i] + K)
if i > 0:
prev = prefix[i - 1]
if index < 0:
ans = min(ans, prev)
else:
ans = min(
ans,
prev + (prefix[-1] - prefix[index - 1] - (N - index) * (A[i] + K)),
)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def minSteps(self, arr, n, k):
f = [[0, 0, 0]]
arr = arr[:n]
arr.sort()
i = 0
while i < n:
f.append([arr[i], 1, f[-1][2] + arr[i]])
i += 1
while i < n and arr[i] == arr[i - 1]:
f[-1][1] += 1
f[-1][2] += arr[i]
i += 1
ans = f[-1][2]
for i in range(1, len(f)):
l = f[i][0]
r = min(f[-1][0], l + k)
s = f[i - 1][2]
j = len(f) - 1
while j > i and f[j][0] > r:
s += (f[j][0] - r) * f[j][1]
j -= 1
ans = min(ans, s)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def binary(self, a, k, x, low, high):
tar = x + k
ans = low - 1
while low <= high:
mid = (low + high) // 2
if a[mid] <= tar:
ans = mid
low = mid + 1
else:
high = mid - 1
return ans
def minSteps(self, a, n, k):
if n == 1:
return 0
a.sort()
pre = [0] * n
pre[0] = a[0]
for i in range(1, n):
pre[i] = pre[i - 1] + a[i]
total = pre[-1]
z = self.binary(a, k, a[0], 1, n - 1)
ans = min(total, total - pre[z] - (n - z - 1) * (k + a[0]))
for i in range(1, n):
z = self.binary(a, k, a[i], i + 1, n - 1)
ans = min(ans, total - pre[z] + pre[i - 1] - (n - z - 1) * (k + a[i]))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | def upper_bound(A, L, R, K):
pos = R + 1
while L <= R:
mid = (L + R) // 2
if A[mid] > K:
pos = mid
R = mid - 1
else:
L = mid + 1
return pos
class Solution:
def minSteps(self, A, N, K):
ans = sum(A)
prefix_sum = [(0) for i in range(0, N + 1)]
A.sort()
prefix_sum[0] = A[0]
for i in range(1, N):
prefix_sum[i] = prefix_sum[i - 1] + A[i]
last_remove = 0
for i in range(0, N):
if i > 0 and A[i] != A[i - 1]:
last_remove = prefix_sum[i - 1]
first_reduce = upper_bound(A, i, N - 1, A[i] + K)
ans = min(
ans,
last_remove
+ prefix_sum[N - 1]
- prefix_sum[first_reduce - 1]
- (N - first_reduce) * (A[i] + K),
)
return ans | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def binary(self, A, x, n):
l = 0
h = n - 1
ans = 0
while l <= h:
mid = (l + h) // 2
if A[mid] <= x:
l = mid + 1
else:
h = mid - 1
ans = mid
return ans
def minSteps(self, A, N, K):
A.sort()
l = [0]
l.append(A[0])
for i in range(1, N):
l.append(A[i] + l[-1])
prev = 0
ans = float("inf")
for i in range(N):
if A[i] + K >= A[-1]:
ans1 = N
else:
ans1 = self.binary(A, A[i] + K, N)
if i > 0 and A[i] != A[i - 1]:
prev = l[i]
ans = min(ans, prev + l[N] - l[ans1] - (N - ans1) * (A[i] + K))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def minSteps(self, A, N, K):
A.sort()
ps = [0] * N
res = 1 << 32 - 1
ps[0] = A[0]
for i in range(1, N):
ps[i] = ps[i - 1] + A[i]
def upperbound(l, con):
r = N - 1
pos = N
while l <= r:
m = (l + r) // 2
if A[m] > con:
pos = m
r = m - 1
else:
l = m + 1
return pos
prev = 0
for i in range(N):
SUM = 0
pos = upperbound(i, A[i] + K)
SUM += ps[N - 1] - ps[pos - 1] - (N - pos) * (A[i] + K)
if i > 0 and A[i] != A[i - 1]:
prev = ps[i - 1]
res = min(res, prev + SUM)
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def findIndex(self, A, i, N, p):
l = i + 1
h = N - 1
while l <= h:
m = (l + h) // 2
if A[m] <= p:
l = m + 1
else:
h = m - 1
return l
return N
def minSteps(self, A, N, K):
d = [0] * N
A.sort()
d[0] += A[0]
for i in range(1, N):
d[i] += d[i - 1] + A[i]
ans = 9999999999999999
prev = 0
c = 1
for i in range(N):
if i + 1 < N and A[i] == A[i + 1]:
c += 1
i += 1
continue
p = A[i] + K
idx = self.findIndex(A, i, N, p)
actualSum = d[N - 1] - d[idx - 1]
reduc = (N - idx) * p
actualSum -= reduc
ans = min(ans, actualSum + prev)
prev += A[i] * c
c = 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def minSteps(self, A, N, K):
A.sort()
presum = A.copy()
for i in range(1, N):
presum[i] += presum[i - 1]
res = sum(A)
upto = 0
def findpos(L, R, diff):
pos = R + 1
while L <= R:
mid = (L + R) // 2
if A[mid] > diff:
pos = mid
R = mid - 1
else:
L = mid + 1
return pos
for i in range(N):
pos = findpos(i, N - 1, A[i] + K)
if i > 0 and A[i] != A[i - 1]:
upto = presum[i - 1]
res = min(
res, upto + presum[N - 1] - presum[pos - 1] - (N - pos) * (A[i] + K)
)
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | def ub(A, l, r, key):
ans = r + 1
while l <= r:
mid = l + (r - l >> 1)
if A[mid] > key:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
class Solution:
def minSteps(self, A, N, K):
A.sort()
ans = 1000000000.0
pref = [0] * N
pref[0] = A[0]
for i in range(1, N):
pref[i] = pref[i - 1] + A[i]
prev = 0
for i in range(N):
key = A[i] + K
ix = ub(A, i, N - 1, key)
if i > 0 and A[i] != A[i - 1]:
prev = pref[i - 1]
ans = min(ans, prev + (pref[N - 1] - pref[ix - 1]) - key * (N - ix))
return ans | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR RETURN VAR |
There are N piles of coins each containing Ai (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most K.
Note: You can also remove a pile by removing all the coins of that pile.
Example 1:
Input:
N = 4, K = 0
arr[] = {2, 2, 2, 2}
Output:
0
Explanation:
For any two piles the difference in the
number of coins is <=0. So no need to
remove any coins.
Example 2:
Input:
N = 6, K = 3
arr[] = {1, 5, 1, 2, 5, 1}
Output :
2
Explanation:
If we remove one coin each from both
the piles containing 5 coins , then
for any two piles the absolute difference
in the number of coins is <=3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minSteps() which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{4}
1 β€ A[i] β€ 103 | class Solution:
def minSteps(self, a, n, k):
def upper_bound(low, high, target):
ans = high + 1
while low <= high:
mid = low + high >> 1
if a[mid] > target:
ans = mid
high = mid - 1
else:
low = mid + 1
return ans
a.sort()
pre = [0] * (n + 1)
pre[0] = a[0]
for i in range(1, n):
pre[i] = pre[i - 1] + a[i]
ans = float("inf")
for i in range(n):
idx = upper_bound(i, n - 1, a[i] + k)
ans = min(
ans, pre[i - 1] + (pre[n - 1] - pre[idx - 1]) - (n - idx) * (a[i] + k)
)
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, K = map(int, input().split())
A = list(map(int, input().split()))
ob = Solution()
print(ob.minSteps(A, N, K)) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain.
Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4.
------ Input ------
The first line of the input contains an integer T denoting the number of testcases.
The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively.
The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}.
------ Output ------
For each testcase, output the answer in a single line.
------ Constraints ------
1 β€ T β€ 10
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{5}
0 β€ S_{i} β€ 2 * 10^{5}
------ Subtasks ------
Subtask #1 (15 points): K=0.
Subtask #2 (85 points): Original Constraints.
----- Sample Input 1 ------
4
3 0
1 0 2
3 1
1 0 2
4 3
2 5 4 9
2 0
3 4
----- Sample Output 1 ------
3
4
6
0
----- explanation 1 ------
Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3.
Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set. | for _ in range(int(input())):
n, k = map(int, input().split())
arr = set(map(int, input().split()))
last = n + k
for i in range(n + k):
if i not in arr:
if k > 0:
k -= 1
else:
last = i
break
print(last) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain.
Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4.
------ Input ------
The first line of the input contains an integer T denoting the number of testcases.
The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively.
The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}.
------ Output ------
For each testcase, output the answer in a single line.
------ Constraints ------
1 β€ T β€ 10
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{5}
0 β€ S_{i} β€ 2 * 10^{5}
------ Subtasks ------
Subtask #1 (15 points): K=0.
Subtask #2 (85 points): Original Constraints.
----- Sample Input 1 ------
4
3 0
1 0 2
3 1
1 0 2
4 3
2 5 4 9
2 0
3 4
----- Sample Output 1 ------
3
4
6
0
----- explanation 1 ------
Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3.
Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set. | for _ in range(int(input())):
k = int(input().split()[-1])
values = set(map(int, input().split()))
value = 0
while True:
if value in values:
value += 1
elif k > 0:
k, value = k - 1, value + 1
else:
print(value)
break | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain.
Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4.
------ Input ------
The first line of the input contains an integer T denoting the number of testcases.
The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively.
The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}.
------ Output ------
For each testcase, output the answer in a single line.
------ Constraints ------
1 β€ T β€ 10
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{5}
0 β€ S_{i} β€ 2 * 10^{5}
------ Subtasks ------
Subtask #1 (15 points): K=0.
Subtask #2 (85 points): Original Constraints.
----- Sample Input 1 ------
4
3 0
1 0 2
3 1
1 0 2
4 3
2 5 4 9
2 0
3 4
----- Sample Output 1 ------
3
4
6
0
----- explanation 1 ------
Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3.
Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set. | for _ in range(int(input())):
n, k = map(int, input().split())
li = set(map(int, input().split()))
t = 0
mi = 0
while t != k + 1:
if mi in li:
mi += 1
else:
ans = mi
t += 1
mi += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain.
Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4.
------ Input ------
The first line of the input contains an integer T denoting the number of testcases.
The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively.
The second line contains N space separated integers denoting the multi-set S: S_{1}, S_{2} ,.... S_{N}.
------ Output ------
For each testcase, output the answer in a single line.
------ Constraints ------
1 β€ T β€ 10
1 β€ N β€ 10^{5}
0 β€ K β€ 10^{5}
0 β€ S_{i} β€ 2 * 10^{5}
------ Subtasks ------
Subtask #1 (15 points): K=0.
Subtask #2 (85 points): Original Constraints.
----- Sample Input 1 ------
4
3 0
1 0 2
3 1
1 0 2
4 3
2 5 4 9
2 0
3 4
----- Sample Output 1 ------
3
4
6
0
----- explanation 1 ------
Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3.
Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set. | for _ in range(int(input())):
l, k = map(int, input().split())
s1 = set(list(map(int, input().split())))
s2 = set(list(i for i in range(l + k + 1)))
s3 = s1.symmetric_difference(s2)
print(list(s3)[k]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.