description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
pre = a[0]
cnt = 0
ans = 0
for cur in a[1:]:
if pre < 2 * cur:
cnt += 1
else:
cnt = 0
if cnt >= k:
ans += 1
pre = cur
print(ans)
|
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 VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
import sys
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
def get_string():
return sys.stdin.readline().strip()
t = int(input())
for _ in range(t):
n, k = get_ints()
arr = get_list()
cnt = 0
l = 0
for i in range(1, n):
if 2 * arr[i] > arr[i - 1]:
if i - l == k:
cnt += 1
l += 1
else:
l = i
print(cnt)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = input().split(" ")
n, k = int(n), int(k)
arr = input().split(" ")
k += 1
temp = 1
ans = 0
for i in range(n):
arr[i] = int(arr[i])
if i != 0 and 2 * arr[i] > arr[i - 1]:
temp += 1
else:
temp = 1
ans += 1 if temp >= k else 0
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
n = int(input())
for i in range(n):
l = list(map(int, input().split()))
long = l[0]
equ = l[1]
s = list(map(int, input().split()))
s.append(0)
num = 0
tip = []
ans = 0
for j in range(long):
if s[j] < 2 * s[j + 1]:
num += 1
else:
tip.append(num)
num = 0
for p in tip:
if p >= equ:
ans += p - equ + 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [(a[i + 1] * 2 > a[i]) for i in range(n - 1)]
s = sum(a[:k])
count = int(s == k)
for i in range(n - k - 1):
s += a[i + k] - a[i]
if s == k:
count += 1
print(count)
|
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 BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for _ in range(t):
n, k = [int(x) for x in input().split()]
k += 1
a = [int(x) for x in input().split()]
broken = [-1] + [i for i in range(n - 1) if a[i] >= 2 * a[i + 1]] + [n - 1]
print(
sum(max(0, broken[i + 1] - broken[i] - k + 1) for i in range(len(broken) - 1))
)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for x in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
sub = 0
ans = [0] * n
for x in range(n - 1):
if l[x] < l[x + 1] * 2:
ans[x] = 1
s = 0
for x in range(k):
s += ans[x]
if s == k:
sub += 1
for y in range(k, n - 1):
s -= ans[y - k]
s += ans[y]
if s == k:
sub += 1
print(sub)
|
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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
lis = list(map(int, input().split()))
c = 0
v = 0
for i in range(n - 1):
if lis[i] >= 2 * lis[i + 1]:
c += max(v - k + 1, 0)
v = 0
else:
v += 1
c += max(v - k + 1, 0)
print(c)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
import sys
from sys import stdin, stdout
sys.setrecursionlimit(10**6)
I = stdin.readline
O = stdout.write
def bi(n):
return bin(n).replace("0b", "")
def solve():
n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
fin = [(1) for i in range(n)]
for i in range(1, n):
if 2 * arr[i] <= arr[i - 1]:
fin[i] = 0
for i in range(1, n):
fin[i] += fin[i - 1]
ans = 0
for i in range(n - k):
if fin[i + k] - fin[i] == k:
ans += 1
print(ans)
for tc in range(int(input())):
solve()
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF ASSIGN VAR 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
testcase = int(input())
for _ in range(testcase):
n, k = list(map(int, input().split()))
lists = list(map(int, input().split()))
left = 0
count = 0
for right in range(1, len(lists)):
if lists[right] * 2 <= lists[right - 1]:
left = right
if right - left >= k:
count += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def solve():
n, k = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
ct = 1
for i in range(1, n):
if 2 * l[i] > l[i - 1]:
ct += 1
else:
ct = 1
if ct > k:
ans += 1
print(ans)
for t in range(int(input())):
solve()
|
FUNC_DEF 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def sol():
n, k = map(int, input().split())
l = list(map(int, input().split()))
tl = [0] * n
tl[0] = 1
ans = 0
for i in range(1, n):
if 2 * l[i] > l[i - 1]:
tl[i] = tl[i - 1] + 1
else:
tl[i] = 1
for i in tl:
if i > k:
ans += 1
print(ans)
for t in range(int(input())):
sol()
|
FUNC_DEF 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 LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
import sys
input = sys.stdin.readline
def solve():
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
cnt = 0
for j in range(k):
cnt += a[j] >= a[j + 1] * 2
for i in range(n - k):
ans += cnt == 0
cnt -= a[i] >= a[i + 1] * 2
if i + 1 + k < n:
cnt += a[i + k] >= a[i + k + 1] * 2
return ans
for _ in range(int(input())):
print(solve())
|
IMPORT ASSIGN VAR VAR FUNC_DEF 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
import itertools
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.append(0)
cnt = 0
res = 0
for i in range(n):
if a[i] < 2 * a[i + 1]:
cnt += 1
else:
if cnt >= k:
res += cnt - k + 1
cnt = 0
print(res)
|
IMPORT 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * (n - 1)
for i in range(n - 1):
b[i] = 1 if a[i] < 2 * a[i + 1] else 0
ans = 0
c = sum(b[:k])
if c == k:
ans += 1
for i in range(n - k - 1):
c -= b[i]
c += b[i + k]
if c == k:
ans += 1
print(ans)
|
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for case in range(t):
n, k = map(int, input().split(" "))
arr = list(map(int, input().split(" ")))
consecutive = 0
count = 0
for j in range(n - 1):
if arr[j] < 2 * arr[j + 1]:
if consecutive < k:
consecutive += 1
if consecutive == k:
count += 1
else:
consecutive = 0
print(count)
|
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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
a = int(input())
for y in range(a):
b, c = map(int, input().split())
d = list(map(int, input().split()))
l = 0
f = 1
o = 0
for y in range(1, b - c):
if 2 * d[y] <= d[y - 1]:
l = y
if f - (c + 1) + 1 > 0:
o += f - (c + 1) + 1
f = 1
else:
f += 1
k = b - c - l + 1
for y in range(b - c, b):
if 2 * d[y] > d[y - 1]:
k += 1
else:
break
j = 0
if k - (c + 1) > 0:
j += k - (c + 1)
print(j + o)
|
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for t in range(int(input())):
n, k = map(int, input().split())
cnt = 0
temp = 0
last = 0
i = 1
for a in map(int, input().split()):
if a * 2 > last:
temp += 1
if i == n:
cnt += max(0, temp - k)
else:
cnt += max(0, temp - k)
temp = 1
last = a
i += 1
print(cnt)
|
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
import itertools
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
print(
sum(
max(sum(1 for _ in g) + 1 - k, 0)
for t, g in itertools.groupby(i < j + j for i, j in zip(a, a[1:]))
if t
)
)
|
IMPORT 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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr_bool = [True]
for i in range(1, n):
if arr[i - 1] < 2 * arr[i]:
arr_bool.append(True)
else:
arr_bool.append(False)
arr_bool.append(False)
i = 0
count = 0
start = 0
for i, bool in enumerate(arr_bool):
if not bool:
count += max(i - start - k, 0)
start = i
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def read_arr():
return list(map(int, input().split(" ")))
_t = 1
_t = int(input())
for _i in range(_t):
n, k = read_arr()
A = read_arr()
B = [0] * n
ans, cur = 0, 0
for i in range(1, n):
if A[i] * 2 > A[i - 1]:
B[i] = 1
for b in B:
if b == 1:
cur += 1
if cur >= k:
ans += 1
else:
cur = 0
print(ans)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for i in range(int(input())):
a, b = map(int, input().split())
lis = list(map(int, input().split()))
ans = 0
c = 1
an = ""
for i in range(a - 1):
if lis[i] < 2 * lis[i + 1]:
c += 1
else:
if c - b > 0:
ans += c - b
c = 1
if c - b > 0:
ans += c - b
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 ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def sort_2(n, k, l):
c = 1
i = 1
ans = 0
while i < n:
if 2 * l[i] > l[i - 1]:
c += 1
else:
c = 1
if c >= k + 1:
ans += 1
i += 1
return ans
for i in range(int(input())):
[n, k] = list(map(int, input().split()))
l = list(map(int, input().split()))
print(sort_2(n, k, l))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for _ in range(t):
n, k = [int(i) for i in input().strip().split()]
a = [int(i) for i in input().strip().split()]
i = 0
j = 1
ans = 0
while j < n:
while j < n and a[j] / a[j - 1] > 0.5:
j += 1
if j - i >= k + 1:
ans += j - i - k
i = j
j = i + 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [(2 * a[i] > a[i - 1]) for i in range(1, n)]
colT = sum(b[i] for i in range(k))
ans = 0
if colT == k:
ans += 1
for i in range(1, n - k):
colT += b[i + k - 1] - b[i - 1]
if colT == k:
ans += 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 BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def solve():
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
count = 1
k += 1
for i in range(1, n):
if arr[i] * 2 > arr[i - 1]:
count += 1
else:
if count >= k:
ans += count - k + 1
count = 1
if count >= k:
ans += count - k + 1
print(ans)
for T in range(int(input())):
solve()
|
FUNC_DEF 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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
b = ""
for i in range(n - 1):
if l[i] < 2 * l[i + 1]:
b += "1"
else:
b += "0"
x = b.split("0")
ans = 0
for i in x:
temp = len(i)
if k > temp:
continue
elif k == temp:
ans += 1
else:
ans += temp - k + 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 STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def solve():
n, k = map(int, input().split())
a = map(int, input().split())
a = list(a)
res = 0
t = 1
i = 1
while i < n:
while i < n and a[i - 1] < a[i] * 2:
i = i + 1
t = t + 1
if t >= k + 1:
res = res + t - k
t = 1
i = i + 1
print(res)
T = int(input())
for i in range(T):
solve()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
p = lambda: map(int, input().split())
def solve():
n, k = p()
a = [*p()]
b = [(a[i] < 2 * a[i + 1]) for i in range(n - 1)]
cnt = 0
s = 0
for i in range(k):
s += b[i]
i = 0
j = k
if s == k:
cnt += 1
while j < n - 1:
s += b[j]
s -= b[i]
if s == k:
cnt += 1
i += 1
j += 1
print(cnt)
t = int(input())
for _ in range(t):
solve()
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
x = int(input())
def cal(n, k, arr):
ans = 0
t = 0
for i in range(1, n):
if arr[i] * 2 > arr[i - 1]:
if i == n - 1:
ans += max(0, i - t - k + 1)
else:
ans += max(0, i - t - k)
t = i
return ans
for jj in range(x):
n, k = [int(i) for i in input().split(" ")]
arr = [int(i) for i in input().split(" ")]
print(cal(n, k, arr))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def shift(s):
for i in range(1, len(s) + 1):
if s == s[i:] + s[:i]:
return i
for _ in range(int(input())):
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
cur = 1
ans = 0
for i in range(1, n):
if a[i - 1] < 2 * a[i]:
cur += 1
else:
cur = 1
if cur > k:
ans += 1
print(ans)
|
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
while t:
n, k = map(int, input().split())
l = list(map(int, input().split()))
le = 1
ans = 0
for i in range(n - 1):
if 2 * l[i + 1] > l[i]:
le += 1
else:
le = 1
if le >= k + 1:
ans += 1
print(ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
s = []
for i in range(n - 1):
if a[i] < 2 * a[i + 1]:
s.append(1)
else:
s.append(0)
cnt = 0
now = sum(s[:k])
if now == k:
cnt += 1
for i in range(k, len(s)):
now -= s[i - k]
now += s[i]
if now == k:
cnt += 1
print(cnt)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
l = []
for i in range(1, n):
if 2 * arr[i] > arr[i - 1]:
l.append(1)
else:
l.append(0)
cs = sum(l[:k])
ans = 0
if cs == k:
ans += 1
for i in range(k, n - 1):
cs += l[i]
cs -= l[i - k]
if cs == k:
ans += 1
print(ans)
|
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 LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
n = int(input())
while n > 0:
x, y = map(int, input().split())
l1 = list(map(int, input().strip().split()))
l2 = l1
l2 = [(x * 2) for x in l2]
ans = 0
k = 0
for i in range(0, x - 1):
if l1[i] < l2[i + 1]:
k += 1
else:
k = 0
if k >= y:
ans += 1
print(ans)
n -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for j in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
cur = 1
for i in range(n - 1):
if cur < k:
if a[i] < a[i + 1] * 2:
cur += 1
else:
cur = 1
elif a[i] < a[i + 1] * 2:
ans += 1
else:
cur = 1
print(ans)
|
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for i in range(t):
[n, k] = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
length = 0
ans = 0
for i in range(n):
if i == n - 1:
if length >= k:
ans += length + 1 - k
break
if arr[i] < 2 * arr[i + 1]:
length += 1
else:
if length >= k:
ans += length + 1 - k
length = 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
tc = int(input())
for _ in range(tc):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
l = []
cnt1 = 1
for i in range(len(a) - 1):
if a[i] < 2 * a[i + 1]:
cnt1 += 1
elif a[i] >= 2 * a[i + 1]:
l.append(cnt1)
cnt1 = 1
l.append(cnt1)
cnt = 0
for i in l:
if i >= k + 1:
cnt += i - (k + 1) + 1
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) — the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for i in range(t):
n, k = [int(v) for v in input().split()]
w = [int(v) for v in input().split()]
z = []
for j in range(1, n):
if w[j] * 2 > w[j - 1]:
z.append(1)
else:
z.append(0)
res = 0
c = 0
for j in range(n - 1):
if z[j] == 1:
c += 1
else:
if c >= k:
res += c - k + 1
c = 0
if c >= k:
res += c - k + 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN 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 LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
pref = {(0): -1}
result = best = math.inf
best_till = [math.inf] * len(arr)
for i, curr in enumerate(itertools.accumulate(arr)):
diff = curr - target
if diff in pref:
left = pref[diff]
length = i - left
result = min(result, best_till[left] + length)
best = min(best, length)
pref[curr] = i
best_till[i] = best
return result if result < math.inf else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
record = collections.defaultdict()
record[0] = 0
l1 = l2 = float("Inf")
tmp_sum = 0
res = float("Inf")
dp = [float("Inf") for _ in range(len(arr) + 1)]
for i in range(len(arr)):
tmp_sum += arr[i]
if tmp_sum - target in record:
dp[i + 1] = i - record[tmp_sum - target] + 1
res = min(res, dp[i + 1] + dp[record[tmp_sum - target]])
record[tmp_sum] = i + 1
dp[i + 1] = min(dp[i + 1], dp[i])
return res if res < float("Inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
arr_len = len(arr)
dp = [sys.maxsize] * arr_len
left = right = 0
sum_tmp = 0
length, ans_len = sys.maxsize, sys.maxsize
while right < arr_len:
sum_tmp += arr[right]
while sum_tmp > target:
sum_tmp -= arr[left]
left += 1
if sum_tmp == target:
if left > 0 and dp[left - 1] != sys.maxsize:
length = min(length, dp[left - 1] + right - left + 1)
ans_len = min(ans_len, right - left + 1)
dp[right] = ans_len
right += 1
return length if length != sys.maxsize else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
result = float("inf")
leftmin = self.getMinLen(arr, target, True)
rightmin = self.getMinLen(arr, target, False)
for i in range(0, len(arr) - 1):
result = min(result, leftmin[i] + rightmin[i + 1])
return result if result != float("inf") else -1
def getMinLen(self, arr, target, l2r):
if not l2r:
arr = arr[::-1]
memo = {(0): 0}
result = [float("inf")] * len(arr)
curSum = 0
for i, num in enumerate(arr):
if i > 0:
result[i] = result[i - 1]
curSum += num
if curSum - target in memo:
curLen = i - memo[curSum - target] + 1
result[i] = min(result[i], curLen)
memo[curSum] = i + 1
if not l2r:
result = result[::-1]
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR FUNC_DEF IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER RETURN VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
dp = [len(arr) + 1] * len(arr)
table = {(0): -1}
s = 0
min_sum = len(arr) + 1
for i in range(len(arr)):
s += arr[i]
if i > 0:
dp[i] = dp[i - 1]
if s - target in table:
length = i - table[s - target]
dp[i] = min(dp[i], length)
if length <= i:
min_sum = min(min_sum, length + dp[i - length])
table[s] = i
if min_sum == len(arr) + 1:
return -1
else:
return min_sum
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
H = {(0): -1}
total = 0
M = len(arr) + 1
for i, a in enumerate(arr):
total += a
H[total] = i
result = M
minL = M
total = 0
for i, a in enumerate(arr):
total += a
if total - target in H:
minL = min(minL, i - H[total - target])
if total + target in H and minL < M:
result = min(result, minL + H[total + target] - i)
return result if result < M else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
sums = {(0): -1}
prefix = 0
dp = [math.inf for _ in range(len(arr) + 1)]
ans = math.inf
for idx, num in enumerate(arr):
prefix += num
dp[idx + 1] = min(dp[idx + 1], dp[idx])
if prefix - target in sums:
ans = min(
ans, idx - sums[prefix - target] + dp[sums[prefix - target] + 1]
)
dp[idx + 1] = min(dp[idx + 1], idx - sums[prefix - target])
sums[prefix] = idx
return ans if ans != math.inf else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
run = [0, arr[0]]
h = {(0): 0, arr[0]: 1}
prefix = [len(arr)]
for i in range(1, len(arr)):
if run[i] - target in h:
prefix.append(min(prefix[-1], i - h[run[i] - target]))
else:
prefix.append(prefix[-1])
run.append(run[-1] + arr[i])
h[run[-1]] = i + 1
suffix = [len(arr) for i in range(len(arr))]
if arr[-1] == target:
suffix[-1] = 1
for i in range(len(arr) - 2, -1, -1):
if run[i] + target in h:
suffix[i] = min(suffix[i + 1], h[run[i] + target] - i)
else:
suffix[i] = suffix[i + 1]
res = len(arr) + 1
for i in range(len(arr)):
if not (prefix[i] == len(arr) or suffix[i] == len(arr)):
res = min(res, prefix[i] + suffix[i])
if res == len(arr) + 1:
return -1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR DICT NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
p = [0]
m = {(0): -1}
minLen = [float("inf")]
cur = 0
ans = best = float("inf")
for i, a in enumerate(arr):
cur += a
p.append(cur)
if cur - target in m:
ans = min(ans, i - m[cur - target] + minLen[m[cur - target] + 1])
best = min(i - m[cur - target], best)
minLen.append(best)
m[cur] = i
return ans if ans != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
def prefix_sum(arr):
lookup = {}
dp = [float("inf")] * len(arr)
cumsum = 0
for i, num in enumerate(arr):
cumsum += num
if cumsum == target:
dp[i] = i - 0 + 1
elif cumsum - target in lookup:
dp[i] = i - lookup[cumsum - target]
lookup[cumsum] = i
dp[i] = min(dp[i - 1], dp[i])
return dp
prefix = prefix_sum(arr)
suffix = prefix_sum(arr[::-1])[::-1]
ans = float("inf")
for i in range(1, len(arr)):
ans = min(ans, prefix[i - 1] + suffix[i])
return ans if ans != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
INF = int(1000000000.0)
class Solution:
def shortest_subarrays(self, a, target):
cum = [0]
for x in a:
cum.append(cum[-1] + x)
hist = {(0): 0}
ret = [INF]
for i in range(1, len(cum)):
ci = cum[i]
begin = hist.get(ci - target, -1)
if begin >= 0:
ret.append(min(i - begin, ret[-1]))
else:
ret.append(ret[-1])
hist[ci] = i
return ret
def minSumOfLengths(self, arr: List[int], target: int) -> int:
forward = self.shortest_subarrays(arr, target)
backward = self.shortest_subarrays(reversed(arr), target)
ret = INF
for i in range(1, len(arr) - 1 + 1):
ret = min(ret, forward[i] + backward[len(arr) - i])
if ret >= INF:
return -1
return ret
|
ASSIGN VAR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
def get_dp(arr):
dp = []
ptr = 0
total = 0
min_val = float("inf")
for i in range(n):
total += arr[i]
while ptr < i and total > target:
total -= arr[ptr]
ptr += 1
if total == target:
min_val = min(min_val, i - ptr + 1)
dp.append(min_val)
return dp
dp_left = get_dp(arr)
dp_right = get_dp(arr[::-1])[::-1]
min_val = float("inf")
for i in range(n - 1):
min_val = min(min_val, dp_left[i] + dp_right[i + 1])
return min_val if min_val != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr, target):
result = inf = 2**31 - 1
i = window = 0
premin = [inf] * len(arr)
for j in range(len(arr)):
window += arr[j]
while window > target:
window -= arr[i]
i += 1
if window == target:
curr = j - i + 1
if result > curr + premin[i - 1]:
result = curr + premin[i - 1]
premin[j] = curr if curr < premin[j - 1] else premin[j - 1]
else:
premin[j] = premin[j - 1]
return result if result < inf else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
mp = {}
sum_list = 0
mp[0] = -1
for i, val in enumerate(arr):
sum_list += val
mp[sum_list] = i
res = float("inf")
lvalue = float("inf")
sum_list = 0
for i, val in enumerate(arr):
sum_list += val
if sum_list - target in mp:
lvalue = min(lvalue, i - mp[sum_list - target])
if sum_list + target in mp and lvalue != float("inf"):
res = min(res, lvalue + mp[sum_list + target] - i)
return -1 if res == float("inf") else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
l = len(arr)
left, right, pre = [l] * l, [l] * l, {(0): -1}
p = 0
for i in range(l):
p += arr[i]
if p - target in pre:
prev = pre[p - target]
left[i] = i - prev
if prev >= 0:
right[prev] = min(right[prev], i - prev)
pre[p] = i
for i in range(1, l):
left[i] = min(left[i], left[i - 1])
for i in range(l - 2, -1, -1):
right[i] = min(right[i], right[i + 1])
ans = l + 1
ans = min(
left[i] + right[i] if left[i] < l and right[i] < l else l + 1
for i in range(l)
)
return ans if ans < l + 1 else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST VAR VAR BIN_OP LIST VAR VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
left = [float("inf")] * n
memo = {(0): -1}
current = 0
for k in range(n):
if k > 0:
left[k] = left[k - 1]
current += arr[k]
if current - target in memo:
left[k] = min(left[k], k - memo[current - target])
memo[current] = k
right = [float("inf")] * n
memo = {(0): n}
current = 0
for k in range(n - 1, -1, -1):
if k < n - 1:
right[k] = right[k + 1]
current += arr[k]
if current - target in memo:
right[k] = min(right[k], memo[current - target] - k)
memo[current] = k
ans = float("inf")
for k in range(n - 1):
ans = min(ans, left[k] + right[k + 1])
return ans if ans != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR DICT NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def getMinArr(self, arr, target, right=False):
pre_sum = 0
res = [float("inf") for i in range(len(arr))]
sum_dict = {(0): 0}
if right:
arr = arr[::-1]
for i, num in enumerate(arr):
pre_sum += num
if i > 0:
res[i] = res[i - 1]
if pre_sum - target in sum_dict:
cur_len = i - sum_dict[pre_sum - target] + 1
res[i] = min(res[i], cur_len)
sum_dict[pre_sum] = i + 1
if right:
res.reverse()
return res
def minSumOfLengths(self, arr: List[int], target: int) -> int:
left_min = self.getMinArr(arr, target)
right_min = self.getMinArr(arr, target, True)
min_len = float("inf")
for i in range(len(arr) - 1):
min_len = min(min_len, left_min[i] + right_min[i + 1])
if min_len == float("inf"):
return -1
return min_len
|
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
su = 0
s = 0
min_len = float("inf")
min_lens = [float("inf") for _ in range(n)]
ans = float("inf")
for e in range(n):
su += arr[e]
while su > target:
su -= arr[s]
s += 1
if su == target:
cur_len = e - s + 1
if s > 0 and min_lens[s - 1] != float("inf"):
ans = min(ans, cur_len + min_lens[s - 1])
min_len = min(min_len, cur_len)
min_lens[e] = min_len
return -1 if ans >= float("inf") else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
if not arr:
return -1
prefixes = [0] * len(arr)
suffixes = [0] * len(arr)
left = right = 0
curr = 0
min_len = float("inf")
while right < len(arr):
curr += arr[right]
while curr >= target and left <= right:
if curr == target:
l = right - left + 1
if l < min_len:
min_len = l
curr -= arr[left]
left += 1
prefixes[right] = min_len
right += 1
left = right = len(arr) - 1
curr = 0
min_len = float("inf")
while left >= 0:
curr += arr[left]
while curr >= target and left <= right:
if curr == target:
l = right - left + 1
if l < min_len:
min_len = l
curr -= arr[right]
right -= 1
suffixes[left] = min_len
left -= 1
min_len = float("inf")
for i in range(len(arr) - 1):
s = prefixes[i] + suffixes[i + 1]
if s < min_len:
min_len = s
return min_len if min_len != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
res = n + 1
pre = {}
pre[0] = -1
dp = [float("inf")] * n
p = 0
for i, a in enumerate(arr):
p += a
dp[i] = dp[i - 1]
if p - target in pre:
cur = i - pre[p - target]
if pre[p - target] >= 0 and dp[pre[p - target]] != float("inf"):
res = min(res, cur + dp[pre[p - target]])
dp[i] = min(i - pre[p - target], dp[i - 1])
pre[p] = i
return -1 if res == n + 1 else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
start, ans, sum = 0, float("inf"), 0
best = [float("inf")] * len(arr)
best_so_far = float("inf")
for i in range(len(arr)):
sum += arr[i]
while sum > target:
sum -= arr[start]
start += 1
if sum == target:
if start > 0 and best[start - 1] != float("inf"):
ans = min(ans, best[start - 1] + i - start + 1)
best_so_far = min(best_so_far, i - start + 1)
best[i] = best_so_far
return -1 if ans == float("inf") else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix_sum2_idx = defaultdict(int)
prefix_sum2_idx[0] = -1
prefix_sum = 0
n = len(arr)
for i in range(n):
prefix_sum += arr[i]
prefix_sum2_idx[prefix_sum] = i
prefix_sum = 0
l_min_len = float("inf")
min_len = float("inf")
for i in range(n):
prefix_sum += arr[i]
if prefix_sum - target in prefix_sum2_idx:
l_min_len = min(l_min_len, i - prefix_sum2_idx[prefix_sum - target])
if prefix_sum + target in prefix_sum2_idx:
min_len = min(
min_len, prefix_sum2_idx[prefix_sum + target] - i + l_min_len
)
return -1 if min_len == float("inf") else min_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
best_so_far = [float("inf") for i in range(len(arr) + 1)]
prefix_sum = 0
prefix_sum_to_idx = {(0): -1}
res = float("inf")
for i, v in enumerate(arr):
prefix_sum += v
best_so_far[i + 1] = best_so_far[i]
prev_sum = prefix_sum - target
if prev_sum in prefix_sum_to_idx:
res = min(
res,
best_so_far[prefix_sum_to_idx[prev_sum] + 1]
+ i
- prefix_sum_to_idx[prev_sum],
)
best_so_far[i + 1] = min(
best_so_far[i], i - prefix_sum_to_idx[prev_sum]
)
prefix_sum_to_idx[prefix_sum] = i
return -1 if res == float("inf") else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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 ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
lim = len(arr)
pref = [arr[0]]
d = dict()
e = dict()
d[arr[0]] = [0]
e[arr[0]] = 1
for i in range(1, lim):
pref.append(pref[-1] + arr[i])
try:
d[pref[-1]].append(i)
e[pref[i]] += 1
except:
d[pref[-1]] = [i]
e[pref[i]] = 1
A = [lim] * lim
for i in range(0, lim):
val = target + pref[i] - arr[i]
if val in d:
l = 0
h = e[val] - 1
mn = lim + 1
while l <= h:
m = (l + h) // 2
if d[val][m] >= i:
if d[val][m] < mn:
mn = d[val][m]
h = m - 1
else:
l = m + 1
if mn != lim + 1:
A[i] = mn - i + 1
if arr[i] == target:
A[i] = 1
pmn = lim
p = [lim] * lim
s = [lim] * lim
for i in range(0, lim):
if i + A[i] < lim and p[i + A[i]] > A[i]:
p[i + A[i]] = A[i]
smn = lim
if A[-1] < lim:
smn = A[-1]
for i in range(lim - 1, -1, -1):
if A[i] < smn:
smn = A[i]
s[i] = smn
mn = lim + 1
for i in range(0, lim):
if p[i] + s[i] < mn:
mn = p[i] + s[i]
if mn >= lim + 1:
return -1
return mn
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengthsOld(self, arr: List[int], target: int) -> int:
n = len(arr)
if n == 1:
return -1
i = 0
j = 0
sum_ = arr[i]
interval = []
while i < n and j < n:
if sum_ == target:
interval.append([i, j, j - i + 1])
sum_ -= arr[i]
i += 1
j += 1
if j < n:
sum_ += arr[j]
elif sum_ < target:
if j < n - 1:
sum_ += arr[j + 1]
j += 1
else:
sum_ -= arr[i]
i += 1
if len(interval) == 0:
return -1
interval.sort(key=lambda x: x[2])
interval_val_sorted = [x for x in interval]
interval.sort(key=lambda x: x[0])
print(interval_val_sorted)
print(interval)
i_, j_, l_ = interval[0][0], interval[0][1], interval[0][2]
found = False
for i in range(1, len(interval), 1):
i_1, j_1, l_1 = interval[i][0], interval[i][1], interval[i][2]
if i_1 >= i_ and i_1 <= j_:
continue
if i_ >= i_1 and i_ <= j_1:
continue
found = True
break
if found == False:
return -1
return l_ + l_1
def findCeil(self, x, starts):
s = 0
e = len(starts) - 1
res = -1
while s <= e:
mid = s + (e - s) // 2
if starts[mid] > x:
res = mid
e = mid - 1
else:
s = mid + 1
return res
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
i = 0
j = 0
overlaps = list()
sum_ = 0
j = 0
while i < n and j < n:
sum_ += arr[j]
while j < n - 1 and sum_ < target:
j += 1
sum_ += arr[j]
if sum_ == target:
overlaps.append([i, j])
sum_ -= arr[i]
sum_ -= arr[j]
i += 1
if sum_ == target:
if i <= n - 1:
overlaps.append([i, j - 1])
starting = [x for [x, y] in overlaps]
length = [(y - x + 1) for [x, y] in overlaps]
l_o = len(length)
if l_o == 0:
return -1
min_arr = [length[-1]]
s = 1
min_ = length[-1]
for i in range(l_o - 2, -1, -1):
min_ = min(length[i], min_)
min_arr.append(min_)
min_length = sys.maxsize
min_arr = list(reversed(min_arr))
for overlap in overlaps:
s, e = overlap[0], overlap[1]
indi = self.findCeil(e, starting)
if indi == -1:
continue
min_length = min(min_length, e - s + 1 + min_arr[indi])
if min_length == sys.maxsize:
return -1
return min_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN 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 FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
i, window, result = 0, 0, float("inf")
premin = [float("inf")] * len(arr)
for j, num in enumerate(arr):
window += num
while window > target:
window -= arr[i]
i += 1
if window == target:
curr = j - i + 1
result = min(result, curr + premin[i - 1])
premin[j] = min(curr, premin[j - 1])
else:
premin[j] = premin[j - 1]
return result if result < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
preSum = {(0): -1}
n = len(arr)
curSum = 0
minLen = float("inf")
lsize = float("inf")
for i in range(n):
curSum += arr[i]
preSum[curSum] = i
curSum = 0
for i in range(n):
curSum += arr[i]
if curSum - target in preSum:
lsize = min(lsize, i - preSum[curSum - target])
if curSum + target in preSum and lsize != float("inf"):
minLen = min(minLen, preSum[curSum + target] - i + lsize)
return minLen if minLen != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
left_min = self.getMinArr(arr, target, True)
right_min = self.getMinArr(arr, target, False)
result = sys.maxsize
for i in range(0, len(arr) - 1):
if max(left_min[i], right_min[i + 1]) != sys.maxsize:
result = min(left_min[i] + right_min[i + 1], result)
return result if result < sys.maxsize else -1
def getMinArr(self, arr, target, right):
result = [sys.maxsize] * len(arr)
current_sum = 0
num_map = {(0): 0}
nums = arr if right else arr[::-1]
for i, num in enumerate(nums):
if i > 0:
result[i] = result[i - 1]
current_sum += num
if current_sum - target in num_map:
current_len = i - num_map[current_sum - target] + 1
result[i] = min(result[i], current_len)
num_map[current_sum] = i + 1
if not right:
result.reverse()
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR RETURN VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
accuArr = []
y = 0
for x in arr:
y += x
accuArr.append(y)
bestTill = [float("inf")] * len(arr)
bestTillNow = float("inf")
sum2Pos = dict()
sum2Pos[0] = -1
res = float("inf")
for i in range(len(accuArr)):
currAccu = accuArr[i]
preSum = currAccu - target
if preSum in sum2Pos:
preEnd = sum2Pos[preSum]
currLength = i - preEnd
if preEnd != -1:
res = min(res, currLength + bestTill[preEnd])
bestTillNow = min(bestTillNow, currLength)
sum2Pos[currAccu] = i
bestTill[i] = bestTillNow
return res if res != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
dp = [float("inf")] * n
sums, res = 0, float("inf")
sum_record = {(0): -1}
for i, num in enumerate(arr):
sums += num
dp[i] = dp[i - 1]
if sums - target in sum_record:
cur_len = i - sum_record[sums - target]
if i - cur_len >= 0:
res = min(res, cur_len + dp[i - cur_len])
dp[i] = min(dp[i - 1], cur_len)
sum_record[sums] = i
return res if res != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
l = r = 0
s = arr[l]
res = []
while r < len(arr):
if s == target:
ln = r - l + 1
res.append((ln, l, r))
if s <= target:
r += 1
if r < len(arr):
s += arr[r]
else:
l += 1
s -= arr[l - 1]
if l > r:
r += 1
if r < len(arr):
s += arr[r]
msl = [math.inf] * len(arr)
msr = [math.inf] * len(arr)
mi = math.inf
resi = -1
for i in range(len(arr)):
if resi + 1 < len(res) and res[resi + 1][2] == i:
resi += 1
mi = min(mi, res[resi][0])
msl[i] = mi
mi = math.inf
resi = len(res)
for i in range(len(arr) - 1, -1, -1):
if resi - 1 >= 0 and res[resi - 1][1] == i:
resi -= 1
mi = min(mi, res[resi][0])
msr[i] = mi
mn = math.inf
for i in range(len(arr) - 1):
mn = min(mn, msl[i] + msr[i + 1])
if mn == math.inf:
return -1
return mn
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
prefixSum = [0] * n
targetSum = [sys.maxsize] * n
beforeSum = [sys.maxsize] * n
afterSum = [sys.maxsize] * n
sumDict = {(0): -1}
total = 0
for i in range(n):
total += arr[i]
prefixSum[i] = total
if total - target in sumDict:
index = sumDict[total - target]
targetSum[i] = i - index
sumDict[total] = i
minValue = sys.maxsize
for i in range(n):
minValue = min(targetSum[i], minValue)
beforeSum[i] = minValue
minValue = sys.maxsize
for i in reversed(list(range(1, n))):
startIndex = i - targetSum[i]
if startIndex >= 0:
afterSum[startIndex] = min(afterSum[startIndex], targetSum[i])
minValue = sys.maxsize
for i in range(len(arr)):
minValue = min(minValue, beforeSum[i] + afterSum[i])
if minValue == sys.maxsize:
minValue = -1
return minValue
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
import sys
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
table = {(0): -1}
cursum = 0
dp1 = [sys.maxsize for i in range(len(arr))]
for i in range(len(arr)):
if i > 0:
dp1[i] = dp1[i - 1]
cursum += arr[i]
if cursum - target in list(table.keys()):
dp1[i] = min(dp1[i], i - table[cursum - target])
table[cursum] = i
table = {(0): len(arr)}
cursum = 0
dp2 = [sys.maxsize for i in range(len(arr))]
for i in range(len(arr) - 1, -1, -1):
if i < len(arr) - 1:
dp2[i] = dp2[i + 1]
cursum += arr[i]
if cursum - target in list(table.keys()):
dp2[i] = min(dp2[i], table[cursum - target] - i)
table[cursum] = i
res = sys.maxsize
for i in range(len(dp1) - 1):
if dp1[i] != sys.maxsize and dp2[i + 1] != sys.maxsize:
res = min(res, dp1[i] + dp2[i + 1])
if res == sys.maxsize:
return -1
else:
return res
|
IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
mapsum = {}
mapsum[0] = -1
totsum = 0
best_till = [math.inf] * len(arr)
best = math.inf
end = -1
res = float("inf")
for i in range(len(arr)):
totsum += arr[i]
if totsum - target in mapsum:
end = mapsum[totsum - target]
if end > -1:
res = min(res, i - end + best_till[end])
best = min(best, i - end)
best_till[i] = best
mapsum[totsum] = i
if res < math.inf:
return res
return res if res < math.inf else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
prefix = [float("INF")] * (n + 1)
suffix = [float("INF")] * (n + 1)
tmp = 0
start = 0
for i in range(n):
if arr[i] == target:
prefix[i + 1] = 1
elif tmp + arr[i] < target:
tmp += arr[i]
else:
tmp += arr[i]
while tmp > target and start < i:
tmp -= arr[start]
start += 1
if tmp == target:
prefix[i + 1] = i - start + 1
prefix[i + 1] = min(prefix[i + 1], prefix[i])
tmp = 0
start = -1
for i in range(-1, -n - 1, -1):
if arr[i] == target:
suffix[i] = 1
elif tmp + arr[i] < target:
tmp += arr[i]
else:
tmp += arr[i]
while tmp > target and start > i:
tmp -= arr[start]
start -= 1
if tmp == target:
suffix[i] = start - i + 1
suffix[i] = min(suffix[i], suffix[i + 1])
min_val = float("INF")
for i in range(n):
min_val = min(min_val, prefix[i] + suffix[i + 1])
return min_val if min_val != float("INF") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
left = self.shortestTails(arr, target, False)
right = self.shortestTails(arr, target, True)
mn = float("inf")
for i in range(len(arr) - 1):
if left[i][0] < float("inf") and right[i + 1][0] < float("inf"):
mn = min(mn, left[i][0] + right[i + 1][0])
return mn if mn < float("inf") else -1
def shortestTails(self, arr, target, reverse):
end = len(arr) - 1 if reverse else 0
prefixSumMap = {(0): len(arr) if reverse else -1}
s = 0
shortestAt = [None] * len(arr)
shortest = float("inf"), -1, -1
for _ in range(len(arr)):
s += arr[end]
diff = s - target
if diff in prefixSumMap:
start = prefixSumMap[diff]
if abs(end - start) < shortest[0]:
shortest = abs(end - start), start, end
shortestAt[end] = shortest
prefixSumMap[s] = end
end = end - 1 if reverse else end + 1
return shortestAt
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
minLen = float("inf")
lsize = float("inf")
preSum = {(0): -1}
curSum = 0
for i, val in enumerate(arr):
curSum += val
preSum[curSum] = i
curSum = 0
for i, val in enumerate(arr):
curSum += val
if curSum - target in preSum:
lsize = min(lsize, i - preSum[curSum - target])
if curSum + target in preSum and lsize != float("inf"):
minLen = min(minLen, preSum[curSum + target] - i + lsize)
return minLen if minLen != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
for i in range(1, len(arr)):
arr[i] += arr[i - 1]
index = {(0): -1}
best_till = [float("inf")] * len(arr)
res = float("inf")
best = float("inf")
for i, val in enumerate(arr):
if val - target in index:
start_of_window = index[val - target]
length = i - start_of_window
res = min(res, best_till[start_of_window + 1] + length)
best = min(best, length)
if i + 1 < len(best_till):
best_till[i + 1] = best
index[val] = i
return -1 if res == float("inf") else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = {(0): -1}
res, best, dp = sys.maxsize, sys.maxsize, [sys.maxsize] * len(arr)
for i, acc in enumerate(itertools.accumulate(arr)):
if acc - target in prefix:
end = prefix[acc - target]
if end > -1:
res = min(res, i - end + dp[end])
best = min(best, i - end)
dp[i] = best
prefix[acc] = i
return -1 if res == sys.maxsize else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
left, right = 0, 0
valid_subarray_lengths = [(0) for _ in range(len(arr))]
cum_sum = 0
for i in range(len(arr)):
cum_sum += arr[i]
right = i
while cum_sum > target:
cum_sum -= arr[left]
left += 1
if cum_sum == target:
valid_subarray_lengths[i] = right - left + 1
prefix_min_lengths = [None for _ in range(len(valid_subarray_lengths))]
prefix_min = float("inf")
for i in range(len(valid_subarray_lengths)):
prefix_min = min(
prefix_min,
(
valid_subarray_lengths[i]
if valid_subarray_lengths[i]
else float("inf")
),
)
prefix_min_lengths[i] = prefix_min
res = float("inf")
for i in range(len(valid_subarray_lengths)):
if (
valid_subarray_lengths[i] != 0
and i - valid_subarray_lengths[i] >= 0
and prefix_min_lengths[i - valid_subarray_lengths[i]] > 0
):
res = min(
res,
valid_subarray_lengths[i]
+ prefix_min_lengths[i - valid_subarray_lengths[i]],
)
return res if res != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
dp = [float("inf") for _ in range(n + 1)]
ans = float("inf")
start = 0
curr_sum = 0
for end in range(n):
curr_sum += arr[end]
while start < end and curr_sum > target:
curr_sum -= arr[start]
start += 1
if curr_sum == target:
ans = min(ans, end - start + 1 + dp[start - 1])
dp[end] = min(dp[end - 1], end - start + 1)
else:
dp[end] = dp[end - 1]
return ans if ans != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n_arr = len(arr)
if n_arr == 0:
return -1
def get_minlen_ending_before(array: List[int]) -> List[int]:
n_array = len(array)
minlen_array = [float("inf")] * n_array
curr_sum = array[0]
i = 0
j = 0
min_len = float("inf")
while i < n_array and j < n_array:
increment_i = False
increment_j = False
if curr_sum == target:
min_len = min(min_len, j - i + 1)
curr_sum -= array[i]
increment_i = True
elif curr_sum < target:
increment_j = True
if j < n_array - 1:
curr_sum += array[j + 1]
elif curr_sum > target:
curr_sum -= array[i]
increment_i = True
if j < n_array - 1:
minlen_array[j + 1] = min_len
if increment_i:
i += 1
if increment_j:
j += 1
if j < i and i < n_array:
j = i
curr_sum = array[i]
return minlen_array
prefix = get_minlen_ending_before(arr)
postfix = get_minlen_ending_before(arr[::-1] + [0])[1:][::-1]
min_sum = float("inf")
for i in range(n_arr):
min_sum = min(min_sum, prefix[i] + postfix[i])
return min_sum if min_sum < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr, target):
result = inf = 2**31 - 1
i = window = count = 0
preMin = [(-1, inf)]
for j, num in enumerate(arr):
window += num
while window > target:
window -= arr[i]
i += 1
if window == target:
curr = j - i + 1
n = 0
for index, length in preMin[::-1]:
if index <= i - 1:
n = length
break
if result > curr + n:
result = curr + n
if curr < preMin[-1][-1]:
preMin.append((j, curr))
return result if result < inf else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
prefix_sum = list(accumulate(arr))
suffix_sum = list(accumulate(arr[::-1]))
def helper(prefix_sum, n, target):
dict_x = {(0): -1}
l_1 = [(n + 1) for _ in range(n)]
for i in range(n):
t1 = prefix_sum[i] - target
x = n + 1
if dict_x.get(t1, "x") != "x":
x = i - dict_x[t1]
l_1[i] = min(x, l_1[i - 1] if i > 0 else n + 1)
dict_x[prefix_sum[i]] = i
return l_1
l = helper(prefix_sum, n, target)
r = helper(suffix_sum, n, target)[::-1]
ans = sys.maxsize
for i in range(n - 1):
ans = min(ans, l[i] + r[i + 1])
print(ans)
return ans if ans <= n else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = []
for n in arr:
if len(prefix) == 0:
prefix.append(n)
else:
prefix.append(prefix[-1] + n)
indexMap = {}
indexMap[0] = -1
best = output = float("inf")
best_till = [float("inf")] * len(arr)
for i, p in enumerate(prefix):
if p - target in indexMap:
output = min(
output, i - indexMap[p - target] + best_till[indexMap[p - target]]
)
best = min(best, i - indexMap[p - target])
best_till[i] = best
indexMap[p] = i
return -1 if output == float("inf") else output
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
ptr1, ptr2 = 0, 0
s = arr[0]
m = [-1] * len(arr)
ans = -1
global_min = -1
while ptr2 < len(arr):
if global_min > 0:
m[ptr2] = global_min
if s == target:
m[ptr2] = (
min(global_min, ptr2 - ptr1 + 1)
if global_min > 0
else ptr2 - ptr1 + 1
)
global_min = m[ptr2]
if ptr1 - 1 >= 0 and m[ptr1 - 1] > 0:
ans = (
min(ans, m[ptr1 - 1] + ptr2 - ptr1 + 1)
if ans > 0
else m[ptr1 - 1] + ptr2 - ptr1 + 1
)
ptr1 += 1
ptr2 += 1
if ptr2 < len(arr):
s = s - arr[ptr1 - 1] + arr[ptr2]
elif s > target:
ptr1 += 1
s = s - arr[ptr1 - 1]
if ptr1 > ptr2 and ptr2 + 1 < len(arr):
ptr2 += 1
s = s + arr[ptr2]
else:
ptr2 += 1
if ptr2 < len(arr):
s = s + arr[ptr2]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
if len(arr) < 2:
return -1
n = len(arr)
curSum = 0
prefixSum = {(0): -1}
dp = [float("inf")] * n
ans = float("inf")
for i in range(n):
curSum += arr[i]
if curSum - target in prefixSum:
j = prefixSum[curSum - target] + 1
ans = min(ans, i - j + 1 + dp[j - 1])
dp[i] = min(dp[i - 1], i - j + 1)
else:
dp[i] = dp[i - 1]
prefixSum[curSum] = i
return ans if ans < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
res = [float("inf")] * len(arr)
ans = float("inf")
currMin = float("inf")
i = 0
s = 0
for j in range(len(arr)):
s += arr[j]
while s > target:
s -= arr[i]
i += 1
if s == target:
currMin = min(currMin, j - i + 1)
ans = min(ans, j - i + 1 + res[i - 1])
res[j] = currMin
return ans if ans < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
LARGE_NUMBER = 1000000000
if not arr:
return -1
psum = [0] * (len(arr) + 1)
sum2i = {}
for i in range(len(arr)):
psum[i + 1] = psum[i] + arr[i]
res = LARGE_NUMBER
dp = [LARGE_NUMBER] * (len(arr) + 1)
for i, x in enumerate(psum):
assert x not in sum2i
sum2i[x] = i
if i > 0:
dp[i] = dp[i - 1]
if psum[i] - target in sum2i:
j = sum2i[psum[i] - target]
dp[i] = min(dp[i], i - j)
if dp[j] != LARGE_NUMBER:
res = min(res, dp[j] + i - j)
return res if res != LARGE_NUMBER else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
len_to_start = collections.defaultdict(list)
start, end = 0, 1
tmp_sum = arr[0]
earliest_end, latest_start = len(arr), 0
while start < len(arr):
while end < len(arr) and tmp_sum < target:
tmp_sum += arr[end]
end += 1
if tmp_sum == target:
len_to_start[end - start].append(start)
earliest_end = min(earliest_end, end)
latest_start = max(latest_start, start)
tmp_sum -= arr[start]
start += 1
if earliest_end > latest_start:
return -1
for l, starts in list(len_to_start.items()):
if len(starts) > 2:
len_to_start[l] = [starts[0], starts[-1]]
shortest = len(arr)
for l1 in len_to_start:
for s1 in len_to_start[l1]:
for l2 in len_to_start:
for s2 in len_to_start[l2]:
if l1 + l2 >= shortest:
continue
if s1 < s2 and s1 + l1 <= s2:
shortest = l1 + l2
if s2 < s1 and s2 + l2 <= s1:
shortest = l1 + l2
return shortest
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR FOR VAR VAR FOR VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
INF = len(arr) + 1
best_idx = [INF] * len(arr)
best = INF
csum = 0
left = 0
for right in range(len(arr)):
csum += arr[right]
while csum > target and left <= right:
csum -= arr[left]
left += 1
if csum == target:
best = min(best, best_idx[left - 1] + right - left + 1)
best_idx[right] = min(best_idx[right - 1], right - left + 1)
else:
best_idx[right] = best_idx[right - 1]
if INF == best:
return -1
return best
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
left = list(itertools.accumulate(arr))
right = list(itertools.accumulate(arr[::-1]))
right = right[::-1]
rans = [500000] * len(arr)
lans = [500000] * len(arr)
i, j, n = 0, 0, len(arr)
while i < n and j < n:
sm = left[j] - (left[i - 1] if i else 0)
while sm > target and i <= j:
i += 1
sm = left[j] - (left[i - 1] if i else 0)
if sm == target:
lans[j] = min(lans[j], j - i + 1)
j += 1
i, j = n - 1, n - 1
while i > -1:
sm = right[i] - (right[j + 1] if j + 1 < n else 0)
while sm > target and j >= i:
j -= 1
sm = right[i] - (right[j + 1] if j + 1 < n else 0)
if sm == target:
rans[i] = min(rans[i], j - i + 1)
i -= 1
ans = math.inf
mv = 999999999
for i in range(n):
mv = min(mv, lans[i])
lans[i] = mv
mv = 212498174
for i in range(n - 1, -1, -1):
mv = min(mv, rans[i])
rans[i] = mv
for i in range(n - 1):
ans = min(ans, lans[i] + rans[i + 1])
return ans if ans < 500000 else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = {(0): -1}
best_till = [math.inf] * len(arr)
ans = best = math.inf
for i in range(1, len(arr)):
arr[i] += arr[i - 1]
for i, cur in enumerate(arr):
if cur - target in prefix:
end = prefix[cur - target]
if end > -1:
ans = min(ans, i - end + best_till[end])
best = min(best, i - end)
best_till[i] = best
prefix[cur] = i
return -1 if ans == math.inf else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
left_sub_array = [len(arr)] * len(arr)
sub_sum, next_index = 0, 0
for index in range(len(arr)):
while sub_sum < target and next_index < len(arr):
sub_sum += arr[next_index]
if sub_sum > target:
sub_sum -= arr[next_index]
break
else:
if sub_sum == target:
left_sub_array[next_index] = next_index - index + 1
next_index += 1
sub_sum -= arr[index]
left_sub_array[index] = min(
left_sub_array[index], left_sub_array[index - 1]
)
r_arr = arr[::-1]
right_sub_array = [len(r_arr)] * len(r_arr)
sub_sum, next_index = 0, 0
for index in range(len(r_arr)):
while sub_sum < target and next_index < len(r_arr):
sub_sum += r_arr[next_index]
if sub_sum > target:
sub_sum -= r_arr[next_index]
break
else:
if sub_sum == target:
right_sub_array[next_index] = next_index - index + 1
next_index += 1
sub_sum -= r_arr[index]
right_sub_array[index] = min(
right_sub_array[index], right_sub_array[index - 1]
)
right_sub_array = right_sub_array[::-1]
min_length = 2 * len(arr)
for index, val in enumerate(left_sub_array[:-1]):
min_length = min(min_length, val + right_sub_array[index + 1])
if min_length > len(arr):
return -1
else:
return min_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
sP = 0
fP = 0
intervalHeap = []
sumSoFar = 0
while fP < len(arr):
sumSoFar += arr[fP]
if sumSoFar >= target:
if sumSoFar == target:
heapq.heappush(intervalHeap, (fP - sP + 1, sP, fP))
while sumSoFar >= target:
sumSoFar -= arr[sP]
sP += 1
if sumSoFar == target:
heapq.heappush(intervalHeap, (fP - sP + 1, sP, fP))
fP += 1
if len(intervalHeap) > 1:
first = heapq.heappop(intervalHeap)
second = heapq.heappop(intervalHeap)
if first[2] < second[1] or second[2] < first[1]:
return first[0] + second[0]
else:
while intervalHeap:
third = heapq.heappop(intervalHeap)
if third and first[2] < third[1] or third[2] < first[1]:
return first[0] + third[0]
elif third and second[2] < third[1] or third[2] < second[1]:
return second[0] + third[0]
return -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
sumArr = []
sumMap = {}
sumMap[0] = -1
for i in range(len(arr)):
sumArr.append(arr[i] + (sumArr[i - 1] if i - 1 > -1 else 0))
sumMap[sumArr[i]] = i
res = math.inf
dp = [math.inf for i in range(len(arr))]
for i in range(0, len(arr)):
if sumMap.get(sumArr[i] - target) != None:
dp[i] = min(
i - sumMap[sumArr[i] - target], dp[i - 1] if i > 0 else math.inf
)
else:
dp[i] = dp[i - 1] if i > 0 else math.inf
print(dp)
for i in range(len(arr) - 1, -1, -1):
if sumMap.get(sumArr[i] - target) != None:
j = sumMap.get(sumArr[i] - target)
print(j)
l1 = i - j
if j > -1 and dp[j] != math.inf:
res = min(res, l1 + dp[j])
return -1 if res == math.inf else res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
N = len(arr)
prefix = [N + 1] * N
suffix = [N + 1] * N
presum = dict()
total = 0
presum[0] = -1
for i in range(N):
total += arr[i]
presum[total] = i
prefix[0] = N + 1
total = 0
for i in range(0, N):
total += arr[i]
t = N + 1
xx = total - target
if xx in presum:
t = i - presum[xx]
prefix[i] = min(prefix[i - 1], t)
sufsum = dict()
total = 0
sufsum[0] = N
for i in range(N - 1, -1, -1):
total += arr[i]
sufsum[total] = i
total = 0
if arr[-1] == target:
suffix[-1] = 1
total = arr[-1]
for i in range(N - 2, -1, -1):
total += arr[i]
t = N + 1
xx = total - target
if xx in sufsum:
t = sufsum[xx] - i
suffix[i] = min(suffix[i + 1], t)
rtv = N + 1
for i in range(N - 1):
cur = prefix[i] + suffix[i + 1]
rtv = min(rtv, cur)
if rtv > N:
return -1
return rtv
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr, target):
result = inf = 2**31 - 1
i = window = count = 0
preMin = deque([(-1, inf)])
for j, num in enumerate(arr):
window += num
while window > target:
window -= arr[i]
i += 1
while len(preMin) >= 2 and preMin[1][0] <= i - 1:
preMin.popleft()
if window == target:
curr = j - i + 1
n = preMin[0][1]
if result > curr + n:
result = curr + n
if curr < preMin[-1][-1]:
preMin.append((j, curr))
if curr == 1:
count += 1
if count == 2:
return 2
return result if result < inf else -1
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR NUMBER
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
if not arr:
return 0
min_at_curr = [float("inf")] * len(arr)
start = end = 0
currSum = 0
minSum = float("inf")
while end < len(arr):
currSum += arr[end]
while start <= end and currSum > target:
currSum -= arr[start]
start += 1
if currSum == target:
minSum = min(minSum, min_at_curr[start - 1] + (end - start) + 1)
min_at_curr[end] = min(min_at_curr[end - 1], end - start + 1)
else:
min_at_curr[end] = min_at_curr[end - 1]
end += 1
return minSum if minSum != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
def get_min(arr):
min_left = [float("inf")] * n
window = 0
l = 0
for r in range(n):
window += arr[r]
while window > target:
window -= arr[l]
l += 1
if window == target:
min_left[r] = r - l + 1
if r > 0:
min_left[r] = min(min_left[r], min_left[r - 1])
return min_left
min_left = get_min(arr)
min_right = get_min(arr[::-1])[::-1]
best = float("inf")
for i in range(1, n):
best = min(best, min_left[i - 1] + min_right[i])
return best if best < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
nums = [(0) for i in range(len(arr))]
nums[0] = arr[0]
for i in range(1, len(arr)):
nums[i] = nums[i - 1] + arr[i]
best = [float("inf") for i in range(len(arr))]
ans = float("inf")
size = float("inf")
l = 0
r = 0
window = 0
while l <= r and r < len(arr):
window += arr[r]
while l <= r and window > target:
window -= arr[l]
l += 1
if window == target:
ans = min(ans, r - l + 1 + best[l - 1])
best[r] = min(best[r - 1], r - l + 1)
else:
best[r] = best[r - 1]
r += 1
if ans == float("inf"):
return -1
else:
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.