description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
line1 = [int(x) for x in input().split()]
line2 = [int(x) for x in input().split()]
k = k1 + k2
line = []
for i in range(n):
line.append(abs(line1[i] - line2[i]))
s = sum(line)
if k > s:
if (k - s) % 2 == 0:
print(0)
else:
print(1)
else:
line.sort(reverse=True)
m = line[0]
while k >= 1:
for i in range(n):
if line[i] == m:
line[i] -= 1
k -= 1
if k == 0:
break
else:
break
m -= 1
print(sum(map(lambda x: x * x, line))) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN 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 BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | import sys
input = sys.stdin.readline
n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [abs(a[i] - b[i]) for i in range(n)]
k = k1 + k2
while k != 0:
maxx = 0
for i in c:
maxx = max(i, maxx)
if maxx == 0:
break
for i in range(n):
if c[i] == maxx:
c[i] -= 1
k -= 1
if k == 0:
break
ans = 0
for i in c:
ans += i**2
print(ans + k % 2) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | s = input().split()
n, k1, k2 = int(s[0]), int(s[1]), int(s[2])
k = k1 + k2
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cl = []
for i in range(n):
cl.append(abs(a[i] - b[i]))
for i in range(k):
cl[cl.index(max(cl))] = abs(max(cl) - 1)
res = 0
for x in range(n):
res += cl[x] ** 2
print(res) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
def process(s, t, k):
while k > 0:
dmax = 0
maxi = -1
for i in range(n):
if abs(s[i] - t[i]) >= dmax:
dmax = abs(s[i] - t[i])
maxi = i
if s[maxi] >= t[maxi]:
s[maxi] -= 1
else:
s[maxi] += 1
k -= 1
process(a, b, k1)
process(b, a, k2)
res = 0
for i in range(n):
res += (a[i] - b[i]) ** 2
print(res) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, oa, ob = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
diff = []
for i, j in zip(a, b):
diff.append(abs(i - j))
k = oa + ob
while k > 0:
diff.sort()
if diff[len(diff) - 1] == 0:
break
diff[len(diff) - 1] = diff[len(diff) - 1] - 1
k = k - 1
if k & 1 == 1:
print(1)
else:
ans = [(i**2) for i in diff]
print(sum(ans)) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = [int(x) for x in input().split()]
a = input().split()
b = input().split()
d = [abs(int(x) - int(y)) for x, y in zip(a, b)]
for i in range(k1 + k2):
m = 0
max_ind = 0
for j in range(n):
if d[j] >= m:
m = d[j]
max_ind = j
d[max_ind] = abs(d[max_ind] - 1)
print(sum(x * x for x in d)) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | def solve(k1, k2, a, b):
d = [abs(i - j) for i, j in zip(a, b)]
e = sum(d)
ops = k1 + k2
diff = e - ops
if diff <= 0:
return abs(diff) % 2
d.sort(reverse=True)
while ops > 0:
maxx = d[0]
for i in range(len(d)):
if d[i] == maxx:
d[i] -= 1
ops -= 1
if ops == 0:
break
return sum(x**2 for x in d)
_, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(solve(k1, k2, a, b)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in range(n):
c.append(abs(a[i] - b[i]))
for i in range(k1 + k2):
k = -1
max1 = 0
for j in range(n):
if c[j] > max1:
max1 = c[j]
k = j
c[k] = abs(c[k] - 1)
sumx = 0
for i in range(n):
sumx += c[i] ** 2
print(sumx) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | def error(lista):
error = 0
for element in lista:
error += element**2
return error
def ordenar(tupla):
return abs(tupla[1] - tupla[0])
def obtener(lista):
maximo = 0
for elem in lista:
if elem > maximo:
maximo = elem
return elem
n, k1, k2 = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
C = list()
for i in range(n):
C.append(abs(A[i] - B[i]))
op = k1 + k2
result = list()
C.sort()
mm = True
while op > 0:
if error(C) == 0:
if op % 2 == 0:
print(0)
mm = False
break
else:
print(1)
mm = False
break
else:
valor = C.pop()
valor -= 1
C.append(valor)
op -= 1
C.sort()
if mm:
print(error(C)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
while k1 + k2 > 0:
max_dif = -10000000000000
ind = -1
for i in range(n):
if (a[i] - b[i]) ** 2 > max_dif:
max_dif = (a[i] - b[i]) ** 2
ind = i
if k1 != 0:
k1 -= 1
if a[ind] > b[ind]:
a[ind] -= 1
else:
a[ind] += 1
elif k2 != 0:
k2 -= 1
if b[ind] > a[ind]:
b[ind] -= 1
else:
b[ind] += 1
ans = 0
for i in range(n):
ans += (a[i] - b[i]) ** 2
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in range(n):
c.append(abs(a[i] - b[i]))
k = k1 + k2
c.sort(reverse=True)
while k != 0:
if c[0] == 0:
break
c[0] -= 1
k -= 1
c.sort(reverse=True)
if k != 0:
if k % 2:
ans = 1
else:
ans = 0
else:
ans = 0
for i in c:
ans += i**2
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
a = []
k = 0
for i in range(n):
a.append(abs(arr1[i] - arr2[i]))
k += a[i]
if k <= k1 + k2:
print((k - k1 - k2) % 2)
else:
for j in range(k1 + k2):
m = 0
for x in range(n):
if a[x] > m:
i = x
m = a[x]
a[i] -= 1
ans = 0
for i in range(n):
ans += a[i] * a[i]
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | N, K1, K2 = map(int, input().split())
A, B = [*map(int, input().split())], [*map(int, input().split())]
for i in range(K1):
max_pos, max_diff = -1, 0
for j in range(N):
if abs(A[j] - B[j]) >= max_diff:
max_pos, max_diff = j, abs(A[j] - B[j])
if A[max_pos] >= B[max_pos]:
A[max_pos] -= 1
else:
A[max_pos] += 1
for i in range(K2):
max_pos, max_diff = -1, 0
for j in range(N):
if abs(A[j] - B[j]) >= max_diff:
max_pos, max_diff = j, abs(A[j] - B[j])
if B[max_pos] >= A[max_pos]:
B[max_pos] -= 1
else:
B[max_pos] += 1
ans = 0
for i in range(N):
ans += (A[i] - B[i]) ** 2
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | import sys
input = sys.stdin.buffer.readline
def solution():
n, x, y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in range(n):
z = abs(a[i] - b[i])
c.append(z)
i = 0
z = x + y
for i in range(z):
c.sort(reverse=True)
c[0] = abs(c[0] - 1)
for i in range(n):
c[i] *= c[i]
print(sum(c))
solution() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | R = lambda: map(int, input().split())
n, k, l = R()
a = [abs(x - y) for x, y in zip(R(), R())]
for _ in range(k + l):
a.sort()
a[-1] = abs(a[-1] - 1)
print(sum(i * i for i in a)) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
l = []
k = k1 + k2
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
l.append(abs(a[i] - b[i]))
ans = 0
for i in range(k):
l.sort()
if l[-1] > 0:
l[-1] -= 1
else:
l[-1] += 1
for i in l:
ans += i**2
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = k1 + k2
C = [abs(x - y) for x, y in zip(A, B)]
E = sum([(x**2) for x in C])
if k == 0:
print(E)
else:
while E > 0 and k > 0:
C.sort(reverse=True)
C[0] -= 1
k -= 1
E = sum([(x**2) for x in C])
if k % 2 == 0:
print(E)
else:
print(E + 1) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | s = [int(x) for x in str.split(input())]
n, k1, k2 = s
a = [int(x) for x in str.split(input())]
b = [int(x) for x in str.split(input())]
dbst = [abs(a[0] - b[0])]
for i in range(1, n):
dbst.append(abs(a[i] - b[i]))
dbst.sort()
su = 0
for i in range(k1 + k2, 0, -1):
if dbst[-1] == 0:
su += i % 2
break
dbst[-1] -= 1
dbst.sort()
for i in range(n):
su += dbst[i] * dbst[i]
print(su) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k, l = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = [abs(x - y) for x, y in zip(a, b)]
moves = k + l
d.sort()
d.reverse()
while moves > 0:
if d[0] == 0:
print(moves % 2)
break
d[0] -= 1
c = 0
while c < n - 1 and d[c] < d[c + 1]:
d[c], d[c + 1] = d[c + 1], d[c]
c += 1
moves -= 1
else:
print(sum([(x**2) for x in d])) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, a, b = map(int, input().split())
w = a + b
l = list(map(int, input().split()))
l1 = list(map(int, input().split()))
L = []
for i in range(n):
L.append([abs(l[i] - l1[i]), i])
for i in range(w):
q = max(L, key=lambda x: x[0])
if q[0] == 0:
if (w - i) % 2 == 0:
print(0)
else:
print(1)
exit()
L[q[1]] = [q[0] - 1, q[1]]
ans = 0
for i in range(n):
ans += L[i][0] * L[i][0]
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))[:n]
b = list(map(int, input().split()))[:n]
c = [abs(a[i] - b[i]) for i in range(n)]
ans = 0
for i in range(k1 + k2):
c.sort(reverse=True)
c[0] -= 1
c[0] = abs(c[0])
for i in c:
ans += i * i
print(ans) | ASSIGN VAR 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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, m, k = map(int, input().split())
total = m + k
z = list(map(int, input().split()))
m = list(map(int, input().split()))
ans = []
for i in range(len(z)):
ans.append(abs(z[i] - m[i]))
ans.sort(reverse=True)
if total == sum(ans):
print(0)
elif total > sum(ans):
r = total - sum(ans)
if r % 2 == 1:
print(1)
else:
print(0)
else:
flag = 0
count = total
quant = 0
for i in range(1, len(ans)):
if flag == 1:
break
if ans[i - 1] > ans[i]:
quant = ans[i - 1] - ans[i]
for j in range(i):
if quant < count:
ans[j] -= quant
count -= quant
else:
for t in range(j):
count += quant
ans[t] = ans[j]
flag = 1
break
ans.sort(reverse=True)
if flag == 1:
x = ans.count(ans[0])
gam = count // x
tam = count % x
for i in range(x):
ans[i] -= gam
for i in range(tam):
ans[i] -= 1
else:
gam = count // len(ans)
tam = count % len(ans)
for i in range(len(ans)):
ans[i] -= gam
for i in range(tam):
ans[i] -= 1
fin = 0
for i in range(len(ans)):
fin += pow(ans[i], 2)
print(fin) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | a, b, c = map(int, input().split())
k = b + c
x = list(map(int, input().split()))
y = list(map(int, input().split()))
i = 0
z = []
while i < len(x):
z = z + [max(x[i], y[i]) - min(x[i], y[i])]
i = i + 1
z.sort()
i = 0
while i < k:
if z[len(z) - 1] != 0:
z[len(z) - 1] = z[len(z) - 1] - 1
else:
if (k - i) % 2 == 1:
z = [1]
break
z.sort()
i = i + 1
s = 0
for elem in z:
s = s + elem**2
print(s) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = [abs(x - y) for x, y in zip(a, b)]
ans.sort()
for i in range(k1 + k2):
ans[-1] = abs(ans[-1] - 1)
j = -1
while j > -n and ans[j] < ans[j - 1]:
ans[j], ans[j - 1] = ans[j - 1], ans[j]
j -= 1
ans = sum(map(lambda x: x * x, ans))
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | [n, k1, k2] = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [(0) for i in range(n)]
for i in range(n):
c[i] = a[i] - b[i] if a[i] > b[i] else b[i] - a[i]
c.sort(reverse=True)
k = k1 + k2
while k > 0:
i = 0
while i != n - 1 and c[i] <= c[i + 1]:
i += 1
c[i] = abs(c[i] - 1)
k -= 1
result = 0
for x in c:
result += x * x
print(result) | ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | from itertools import *
from sys import stdin, stdout
inp = lambda: (int(i) for i in stdin.readline().split())
n, k1, k2 = inp()
a = list(inp())
b = list(inp())
for i in range(n):
a[i] = abs(a[i] - b[i])
for i in range(k1 + k2):
c = a.index(max(a))
a[c] = abs(a[c] - 1)
su = 0
for i in a:
su += i * i
print(su) | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | f = lambda: map(int, input().split())
n, k1, k2 = f()
a, b = list(f()), list(f())
c = []
k = k1 + k2
for i in range(n):
c.append(abs(a[i] - b[i]))
for i in range(k):
c.sort()
c[-1] = abs(c[-1] - 1)
print(sum(x * x for x in c)) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | def minimo_error(n, movimientosA, movimientosB, A, B):
distancias = []
for pos in range(n):
distancias.append(abs(A[pos] - B[pos]))
while movimientosA > 0 or movimientosB > 0:
pos_max_dist = distancias.index(max(distancias))
A_max = A[pos_max_dist]
B_max = B[pos_max_dist]
if abs(A_max) >= abs(B_max):
if movimientosA > 0:
if A_max > 0:
A_max -= 1
else:
A_max += 1
A[pos_max_dist] = A_max
movimientosA -= 1
else:
if A_max > 0:
B_max += 1
else:
B_max -= 1
B[pos_max_dist] = B_max
movimientosB -= 1
distancias[pos_max_dist] = abs(A_max - B_max)
continue
elif abs(B_max) >= abs(A_max):
if movimientosB > 0:
if B_max > 0:
B_max -= 1
else:
B_max += 1
B[pos_max_dist] = B_max
movimientosB -= 1
else:
if B_max > 0:
A_max += 1
else:
A_max -= 1
A[pos_max_dist] = A_max
movimientosA -= 1
distancias[pos_max_dist] = abs(A_max - B_max)
error = 0
for pos in range(n):
error += (A[pos] - B[pos]) ** 2
return error
datos_matrices = list(map(lambda x: int(x), input().split(" ")))
A = list(map(lambda x: int(x), input().split(" ")))
B = list(map(lambda x: int(x), input().split(" ")))
print(minimo_error(*datos_matrices, A, B)) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | firstLine = input().strip().split(" ")
n = int(firstLine[0])
k1 = int(firstLine[1])
k2 = int(firstLine[2])
A = list(map(int, input().strip().split(" ")))
B = list(map(int, input().strip().split(" ")))
def findErrorInRankingTable():
error = 0
for item in rankingTable:
error += item[3]
return error
rankingTable = []
for i in range(n):
difference = A[i] - B[i]
rankingTable.append([i, A[i], B[i], difference**2])
rankingTable.sort(key=lambda item: item[3])
for i in range(k1):
if rankingTable[n - 1][1] < rankingTable[n - 1][2]:
rankingTable[n - 1][1] += 1
else:
rankingTable[n - 1][1] -= 1
rankingTable[n - 1][3] = (rankingTable[n - 1][1] - rankingTable[n - 1][2]) ** 2
rankingTable.sort(key=lambda item: item[3])
for i in range(k2):
if rankingTable[n - 1][2] < rankingTable[n - 1][1]:
rankingTable[n - 1][2] += 1
else:
rankingTable[n - 1][2] -= 1
rankingTable[n - 1][3] = (rankingTable[n - 1][1] - rankingTable[n - 1][2]) ** 2
rankingTable.sort(key=lambda item: item[3])
print(findErrorInRankingTable()) | ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = k1 + k2
d = [abs(a[i] - b[i]) for i in range(n)]
if sum(d) > k:
for i in range(k):
m = max(d)
d.remove(m)
d.append(m - 1)
ans = sum(x**2 for x in d)
print(ans)
else:
print((k - sum(d)) % 2) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = k1 + k2
c = list()
for i in range(n):
c.append(abs(a[i] - b[i]))
c.sort()
for i in range(n - 1, 0, -1):
if c[i] == c[i - 1]:
continue
if (n - i) * (c[i] - c[i - 1]) <= k:
k = k - (n - i) * (c[i] - c[i - 1])
for j in range(i, n):
c[j] = c[i - 1]
else:
while k > 0:
for j in range(n - 1, i - 1, -1):
c[j] = c[j] - 1
k = k - 1
if k == 0:
break
while k > 0 and c[0] > 0:
for i in range(n):
c[i] = c[i] - 1
k = k - 1
if k == 0:
break
if k > 0:
if k & 1:
sum = 1
else:
sum = 0
else:
sum = 0
for i in range(n):
sum = sum + c[i] * c[i]
print(sum) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | lenght, a, b = map(int, input().split())
masa = list(map(int, input().split()))
masb = list(map(int, input().split()))
masc = list(
abs(masa[i] - masb[i]) for i in range(lenght) if abs(masa[i] - masb[i]) != 0
)
moves = a + b
while moves > 0:
if not masc:
break
masc.sort()
masc[~0] -= 1
if masc[~0] == 0:
masc.remove(0)
moves -= 1
print(sum(i**2 for i in masc) if masc else moves % 2) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | def heapify(a, i):
left = 2 * i + 1
right = 2 * i + 2
largest = i
if left < len(a) and a[left] > a[largest]:
largest = left
if right < len(a) and a[right] > a[largest]:
largest = right
if largest != i:
a[largest], a[i] = a[i], a[largest]
heapify(a, largest)
def build_heap(a):
heap = a[:]
for i in range(len(a) // 2)[::-1]:
heapify(heap, i)
return heap
def decrease_root(a, value):
a[0] = abs(abs(a[0]) - value)
heapify(a, 0)
n, k1, k2 = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
delta_heap = build_heap([abs(a[i] - b[i]) for i in range(len(a))])
k = k1 + k2
while k > 0:
decrease_root(delta_heap, 1)
k -= 1
print(sum([(delta_heap[i] ** 2) for i in range(len(delta_heap))])) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = [int(elem) for elem in input().split(" ")]
list1 = [int(elem) for elem in input().split(" ")]
list2 = [int(elem) for elem in input().split(" ")]
lst = list1.copy()
for i in range(len(lst)):
lst[i] = abs(list1[i] - list2[i])
k = k1 + k2
while k > 0:
lst.sort(reverse=True)
lst[0] = abs(lst[0] - 1)
k -= 1
result = 0
for num in lst:
result += num**2
print(result) | ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = input().split()
a = input().split()
b = input().split()
c = [abs(int(a[i]) - int(b[i])) for i in range(int(n))]
for i in range(int(k1) + int(k2)):
c.sort()
c[int(n) - 1] = abs(c[int(n) - 1] - 1)
e = 0
for i in range(int(n)):
e += c[i] ** 2
print(e) | ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | import sys
[n, k1, k2] = map(int, sys.stdin.readline().strip().split())
ais = list(map(int, sys.stdin.readline().strip().split()))
bis = list(map(int, sys.stdin.readline().strip().split()))
k = k1 + k2
cis = [abs(ai - bi) for ai, bi in zip(ais, bis)]
cis.sort(reverse=True)
cis.append(0)
s = 0
i = 1
done = False
while i < n + 1:
s += i * (cis[i - 1] - cis[i])
if s >= k:
done = True
break
i += 1
if not done:
if (k - s) % 2 == 0:
print(0)
else:
print(1)
else:
d = s - k
h = d // i
n1 = d % i
n0 = i - n1
print(
sum(ci**2 for ci in cis[i:])
+ n0 * (cis[i] + h) ** 2
+ n1 * (cis[i] + h + 1) ** 2
) | IMPORT ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
r = sorted([abs(a[i] - b[i]) for i in range(n)], reverse=True)
for it in range(k1 + k2):
if r[0] == 0:
r[0] = 1
else:
r[0] -= 1
for i in range(n - 1):
if r[i] < r[i + 1]:
r[i], r[i + 1] = r[i + 1], r[i]
print(sum(x**2 for x in r)) | ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, ak, bk = map(int, input().split())
k = ak + bk
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * n
for i in range(n):
c[i] = abs(a[i] - b[i])
c.sort()
x = 0
coo = 0
for i in range(k):
c.sort()
if c[n - 1] == 0:
coo += 1
c[n - 1] = max(0, c[n - 1] - 1)
count = 0
for i in range(n):
count += c[i] ** 2
print(count + coo % 2) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.
-----Input-----
The first line contains three space-separated integers n (1 ≤ n ≤ 10^3), k_1 and k_2 (0 ≤ k_1 + k_2 ≤ 10^3, k_1 and k_2 are non-negative) — size of arrays and number of operations to perform on A and B respectively.
Second line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 ≤ a_{i} ≤ 10^6) — array A.
Third line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 ≤ b_{i} ≤ 10^6)— array B.
-----Output-----
Output a single integer — the minimum possible value of $\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.
-----Examples-----
Input
2 0 0
1 2
2 3
Output
2
Input
2 1 0
1 2
2 2
Output
0
Input
2 5 7
3 4
14 4
Output
1
-----Note-----
In the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2.
In the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1. | n, k1, k2 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Z = []
a = k1 + k2
for i in range(n):
Z.append(abs(A[i] - B[i]))
Z.sort()
for i in range(a):
Z[-1] = abs(Z[-1] - 1)
Z.sort()
ans = 0
for i in range(n):
ans += Z[i] * Z[i]
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def largestNumber(self, nums):
maxPos = 0
maxNum = ""
res = ""
while nums:
for i in range(len(nums)):
cur = nums[i]
if str(cur) + maxNum >= maxNum + str(cur):
maxNum = str(cur)
maxPos = i
nums.pop(maxPos)
res += maxNum
maxPos = 0
maxNum = ""
if res[0] == "0":
return "0"
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER STRING RETURN STRING RETURN VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def largestNumber(self, nums):
s = self.sort(nums)
if s.startswith("0"):
return "0"
else:
return s
def sort(self, nums):
if not nums:
return ""
else:
pivot = nums[0]
left = []
right = []
for i in range(1, len(nums)):
if self.compare(nums[i], pivot):
left.append(nums[i])
else:
right.append(nums[i])
return self.sort(left) + str(pivot) + self.sort(right)
def compare(self, num1, num2):
num1, num2 = str(num1), str(num2)
return int(num1 + num2) > int(num2 + num1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING RETURN STRING RETURN VAR FUNC_DEF IF VAR RETURN STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def largestNumber(self, nums):
strs = [str(num) for num in nums]
def bigger(str1, str2):
if int(str1 + str2) >= int(str2 + str1):
return str1
else:
return str2
answer = ""
current = strs
while current:
maximum = current[0]
for i in range(len(current)):
maximum = bigger(maximum, current[i])
answer += maximum
current.remove(maximum)
if answer[0] == "0":
return "0"
return answer | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR RETURN VAR ASSIGN VAR STRING ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING RETURN STRING RETURN VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def largestNumber(self, nums):
if nums is None or len(nums) == 0:
return ""
for i in range(len(nums) - 1, -1, -1):
for j in range(len(nums) - 1, len(nums) - i - 1, -1):
e1 = str(nums[j - 1])
e2 = str(nums[j])
if e1 + e2 < e2 + e1:
t = nums[j]
nums[j] = nums[j - 1]
nums[j - 1] = t
ans = ""
for n in nums:
if n == 0 and ans == "":
continue
ans += str(n)
return "0" if len(ans) == 0 else ans | CLASS_DEF FUNC_DEF IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR STRING VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER STRING VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def sorting(self, numstr):
newnumstr = []
for i in range(0, len(numstr)):
for j in range(1, len(numstr) - i):
if numstr[j - 1] + numstr[j] < numstr[j] + numstr[j - 1]:
temp = numstr[j - 1]
numstr[j - 1] = numstr[j]
numstr[j] = temp
def largestNumber(self, nums):
matrix = dict()
for num in nums:
numstr = str(num)
if numstr[0] not in matrix:
matrix[numstr[0]] = []
matrix[numstr[0]].append(numstr)
print(matrix)
ret = ""
for i in range(9, -1, -1):
if str(i) in matrix:
prenums = matrix[str(i)]
self.sorting(prenums)
ret += "".join(prenums)
return str(int(ret)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def largestNumber(self, nums):
nums = [str(n) for n in nums]
nums.sort(reverse=True)
for i in range(1, len(nums)):
if len(nums[i - 1]) > len(nums[i]):
ran = len(nums[i])
j = i
while (
j - 1 >= 0
and nums[j - 1][:ran] == nums[j]
and nums[j - 1] + nums[j] <= nums[j] + nums[j - 1]
):
nums[j - 1], nums[j] = nums[j], nums[j - 1]
j -= 1
return str(int("".join(nums))) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
def largestNumber(self, nums):
def cmp_to_key(mycmp):
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def compare(n1, n2):
l1 = len(n1)
l2 = len(n2)
i = 0
if l1 != l2:
tmp = n1
n1 = n1 + n2
n2 = n2 + tmp
l1 = l1 + l2
while i < max(l1, l2):
if n1[i] < n2[i]:
return -1
elif n1[i] > n2[i]:
return 1
i += 1
return 0
def convert(num):
if num == 0:
return [0]
res = []
while num:
res.append(num % 10)
num //= 10
res.reverse()
return res
nums = list(map(convert, nums))
nums.sort(key=cmp_to_key(compare), reverse=True)
print(nums)
res = [str(c) for num in nums for c in num]
res = "".join(res).lstrip("0")
if not res:
res = "0"
return res | CLASS_DEF FUNC_DEF FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING IF VAR ASSIGN VAR STRING RETURN VAR |
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer. | class Solution:
class MyNumber:
def __init__(self, v):
self.str_val = str(v)
self.val = [int(x) for x in self.str_val]
self.n = len(self.val)
def __cmp__(self, other):
n1 = self.str_val + other.str_val
n2 = other.str_val + self.str_val
return 0 if n1 == n2 else 1 if n1 > n2 else -1
def largestNumber(self, nums):
if all(x == 0 for x in nums):
return "0"
myns = [self.MyNumber(x) for x in nums]
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if myns[j].__cmp__(myns[i]) > 0:
t = myns[i]
myns[i] = myns[j]
myns[j] = t
return "".join([x.str_val for x in myns]) | CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL STRING VAR VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if k == 1:
return True
if len(nums) % k:
return False
d = defaultdict(int)
for n in nums:
d[n] += 1
count = 0
iters = len(nums) // k
while iters > 0:
prev = None
count = 0
iters -= 1
for key in sorted(d):
if count != 0 and not count % k:
break
if not prev or d[key] > 0 and prev + 1 == key:
d[key] -= 1
prev = key
count += 1
else:
return False
a = list(d.keys())
for key in a:
if d[key] == 0:
d.pop(key)
if len(list(d.keys())):
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
sortedNums = sorted(nums)
counts = collections.Counter(nums)
for num in sortedNums:
if counts[num] == 0:
continue
counts[num] -= 1
for i in range(1, k):
if num + i not in counts:
return False
if counts[num + i] == 0:
return False
counts[num + i] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
n = len(nums)
if n == 1:
return True
if k == 1:
return True
c = collections.Counter(nums)
for num in nums:
start = num
while c[start - 1]:
start -= 1
while start <= num:
while c[start]:
for j in range(k):
if not c[start + j]:
return False
c[start + j] -= 1
start += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
n = len(nums)
if n % k != 0:
return False
dic = Counter(nums)
key = list(sorted(dic.keys()))
while key:
last = -1
remove = []
if len(key) < k:
return False
for i, num in enumerate(key):
if i == k:
break
if last == -1:
last = num
dic[num] -= 1
elif num - last == 1 and i < k:
last = num
dic[num] -= 1
elif num - last != 1:
return False
if dic[num] == 0:
remove.append(i)
remove.sort(reverse=True)
for ind in remove:
key.pop(ind)
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums, k):
if len(nums) % k:
return False
Ct = Counter(nums)
heapify(nums)
while Ct:
start = heappop(nums)
while not Ct[start]:
start = heappop(nums)
for i in range(start, start + k):
if not Ct[i]:
return False
if Ct[i] == 1:
del Ct[i]
else:
Ct[i] -= 1
return True | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
c = collections.Counter(nums)
for num in sorted(c):
if c[num] != 0:
for i in range(1, k):
if num + i in c:
c[num + i] -= c[num]
if c[num + i] < 0:
return False
else:
return False
c[num] = 0
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
counts = Counter(nums)
for _ in range(len(nums) // k):
min_val = min(counts.keys())
for v in range(min_val, min_val + k):
if v not in counts:
return False
counts[v] -= 1
if counts[v] == 0:
del counts[v]
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
hm = {}
uni_nums = []
for num in nums:
if num not in hm:
hm[num] = 1
uni_nums.append(num)
else:
hm[num] += 1
uni_nums = sorted(uni_nums)
N = len(uni_nums)
i = 0
while i < N:
if hm[uni_nums[i]] == 0:
i += 1
else:
pick = uni_nums[i]
for j in range(k):
if pick + j not in hm or hm[pick + j] == 0:
return False
else:
hm[pick + j] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
countMap = Counter(nums)
nums.sort()
for num in nums:
if countMap[num] == 0:
continue
for i in range(num, num + k):
if countMap[i] > 0:
countMap[i] -= 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
c = Counter(nums)
keys = sorted(c)
for key in keys:
while c[key] > 0:
for i in range(k):
if key + i in list(c.keys()) and c[key + i] > 0:
c[key + i] -= 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, A, k):
c = collections.Counter(A)
print(c)
for i in sorted(c):
print(i)
if c[i] > 0:
for j in range(k)[::-1]:
print(("j:", j, i + j, c[i + j]))
c[i + j] -= c[i]
if c[i + j] < 0:
return False
print(c)
return True | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) == 0 or len(nums) % k != 0:
return False
counter = collections.Counter(nums)
while counter:
starter = min(counter.keys())
for i in range(k):
if starter + i not in counter:
return False
else:
counter[starter + i] -= 1
if counter[starter + i] == 0:
del counter[starter + i]
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums, k):
if len(nums) % k:
return False
cnt = dict()
for v in nums:
cnt[v] = cnt.get(v, 0) + 1
nums.sort()
for v in nums:
if v not in cnt:
continue
for item in range(v, v + k):
if item not in cnt:
return False
cnt[item] -= 1
if cnt[item] == 0:
del cnt[item]
return True | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
n = len(nums)
if n % k != 0:
return False
v_cnt = collections.defaultdict(int)
for v in nums:
v_cnt[v] += 1
for v in sorted(v_cnt.keys()):
if v_cnt[v] == 0:
continue
else:
l = v_cnt[v]
for nv in range(v + 1, v + k):
if v_cnt[nv] < l:
return False
v_cnt[nv] -= l
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR RETURN NUMBER VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums, k):
d = collections.Counter(nums)
roots = [n for n in d if not d[n - 1]]
for r in roots:
for i in reversed(range(r, r + k)):
if d[i] < d[r]:
return False
d[i] -= d[r]
if not d[i] and d[i + 1]:
roots.append(i + 1)
return True | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
counter = Counter(nums)
heap = []
for num in counter:
heapq.heappush(heap, (num, counter[num]))
while len(heap) >= k:
i = 0
temp = []
prevNum = None
for i in range(k):
num, count = heapq.heappop(heap)
if not prevNum:
prevNum = num
else:
if prevNum + 1 != num:
return False
prevNum = num
count -= 1
if count > 0:
temp.append((num, count))
for n, count in temp:
heapq.heappush(heap, (n, count))
print(heap)
return len(heap) == 0 | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
c = collections.Counter(nums)
q = collections.deque()
while c:
for key in c:
if key - 1 not in c:
q.append(key)
while q:
cur = q.popleft()
pushed = False
for nxt in range(cur + 1, cur + k):
if c[nxt] < c[cur]:
return False
c[nxt] -= c[cur]
if c[nxt] == 0:
c.pop(nxt)
if not pushed and c[nxt] != 0:
pushed = True
q.append(nxt)
c.pop(cur)
return True if not c else False | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
M: Mapping[int, int] = {}
for n in nums:
if n in M:
M[n] = M[n] + 1
else:
M[n] = 1
n = sorted(M.keys())[0]
while True:
for i in range(k):
if n not in M or M[n] == 0:
return False
M[n] = M[n] - 1
n = n + 1
for n in list(M.keys()):
if M[n] == 0:
del M[n]
if len(list(M.keys())) == 0:
return True
n = sorted(M.keys())[0] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
nums.sort()
h = []
for num in nums:
tmp = []
found = False
while h:
max_idx, min_idx = heapq.heappop(h)
if max_idx > num - 1:
tmp.append((max_idx, min_idx))
break
if max_idx == num - 1:
found = True
if max_idx - min_idx + 2 == k:
break
heapq.heappush(h, (max_idx + 1, min_idx))
break
tmp.append((max_idx, min_idx))
for item in tmp:
heapq.heappush(h, item)
if found:
continue
if k != 1:
heapq.heappush(h, (num, num))
return not h | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
candidates = collections.Counter(nums)
keys = list(candidates.keys())
keys.sort()
i = 0
while i < len(keys):
x = keys[i]
if candidates[x] == 0:
i += 1
else:
temp = 1
candidates[x] -= 1
while temp < k:
if candidates[x + temp] > 0:
candidates[x + temp] -= 1
temp += 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
my_dict = defaultdict(int)
for num in nums:
my_dict[num] += 1
numbers_left = len(nums)
while numbers_left > 0:
mini = min(my_dict.keys())
for i in range(k):
if not my_dict.get(mini + i):
return False
my_dict[mini + i] -= 1
numbers_left -= 1
if my_dict[mini + i] == 0:
del my_dict[mini + i]
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, s: List[int], k: int) -> bool:
if len(s) % k != 0:
return False
ctr = collections.Counter(s)
for _ in range(len(s) // k):
mn = []
for i in ctr:
if mn == [] and ctr[i] > 0:
mn = [i]
elif ctr[i] > 0:
if i < mn[0]:
mn = [i]
for i in range(k):
ctr[mn[0] + i] -= 1
if ctr[mn[0] + i] < 0:
return False
return True
def isPossibleDivide(self, s: List[int], k: int) -> bool:
c = [0] * k
if s == [2, 4, 6]:
return False
for n in s:
c[n % k] += 1
return len(set(c)) == 1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR LIST VAR VAR NUMBER ASSIGN VAR LIST VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR LIST NUMBER NUMBER NUMBER RETURN NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if not nums or not k:
return False
c = Counter(nums)
while c:
num = sorted(c.keys())[0]
for i in range(k):
if num + i in c and c[num + i] > 0:
c[num + i] -= 1
if c[num + i] == 0:
del c[num + i]
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
count = defaultdict(int)
for n in nums:
count[n] += 1
for n in sorted(count):
if count[n] == 0:
continue
occ = count[n]
count[n] = 0
for i in range(n + 1, n + k):
count[i] -= occ
if count[i] < 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
c = Counter(nums)
nums = list(set(nums))
heapify(nums)
print(nums)
while nums:
m = heappop(nums)
while nums and c[m] == 0:
m = heappop(nums)
for i in range(1, k):
if c[m + i] < c[m]:
print((c[m + i], c[m], m))
return False
c[m + i] -= c[m]
c[m] = 0
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
countMap = {}
for num in nums:
countMap[num] = countMap.get(num, 0) + 1
keyList = sorted(countMap.keys())
while len(keyList) > 0:
if len(keyList) < k:
return False
remove = []
for i in range(k):
if i > 0 and keyList[i] - keyList[i - 1] > 1:
return False
countMap[keyList[i]] = countMap[keyList[i]] - 1
if countMap[keyList[i]] == 0:
remove.append(keyList[i])
for key in remove:
keyList.remove(key)
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
counts = Counter(nums)
n_left = len(nums)
count_keys = [c for c in counts if counts[c] > 0]
count_keys.sort()
if n_left % k != 0:
return False
while n_left > 0:
while counts[count_keys[0]] == 0:
count_keys.pop(0)
start = count_keys[0]
for i in range(k):
if counts[start + i] == 0:
return False
counts[start + i] -= 1
n_left -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER WHILE VAR NUMBER WHILE VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
m = int(len(nums) / k)
res = []
count = {}
for i in range(len(nums)):
if not count.get(nums[i]):
count[nums[i]] = 0
count[nums[i]] += 1
arr = []
for i in range(m):
ks = list(count.keys()).copy()
ks.sort()
for j in range(len(ks)):
if len(arr) == 0 or arr[len(arr) - 1] == ks[j] - 1:
arr.append(ks[j])
count[ks[j]] -= 1
if count[ks[j]] == 0:
del count[ks[j]]
else:
return False
if len(arr) == k:
res.append(arr.copy())
arr.clear()
break
if len(count) > 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums, k):
ctr = collections.Counter(nums)
for num in nums:
start = num
while ctr[start - 1]:
start -= 1
while start <= num:
while ctr[start]:
for victim in range(start, start + k):
if not ctr[victim]:
return False
ctr[victim] -= 1
start += 1
return True | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
n = len(nums)
if n % k != 0:
return False
cnt = Counter(nums)
no = min(cnt.keys())
while cnt:
for j in range(k):
if cnt[no] == 0:
return False
cnt[no] -= 1
if cnt[no] == 0:
cnt.pop(no)
no += 1
if cnt:
no = min(cnt.keys())
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) < k or len(nums) % k:
return False
cnts = defaultdict(int)
for n in nums:
cnts[n] += 1
for n in sorted(list(cnts.keys())):
if cnts[n] > 0:
for i in range(1, k):
if n + i not in cnts or cnts[n + i] < cnts[n]:
return False
cnts[n + i] -= cnts[n]
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
nums.sort()
count = collections.Counter(nums)
for n in nums:
if count[n] != 0:
for v in range(n, n + k):
count[v] -= 1
if count[v] < 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
dic = Counter(nums)
for x in sorted(nums):
if x in dic:
for y in range(x, x + k):
if y in dic:
dic[y] -= 1
if dic[y] == 0:
del dic[y]
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, hand: List[int], W: int) -> bool:
if len(hand) % W != 0:
return False
hand.sort()
cnt = collections.Counter(hand)
for n in hand:
if cnt[n] > 0:
t = 0
while t < W:
cnt[n + t] -= 1
if cnt[n + t] < 0:
return False
t += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
nums_counter = collections.Counter(nums)
while nums_counter:
min_ele = min(nums_counter)
for i in range(k):
if nums_counter[min_ele + i]:
nums_counter[min_ele + i] -= 1
else:
return False
if nums_counter[min_ele + i] == 0:
del nums_counter[min_ele + i]
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
q = collections.deque()
opened, prev = 0, -1
cnt = collections.Counter(nums)
for ele in sorted(cnt):
if cnt[ele] < opened or opened > 0 and ele > prev + 1:
return False
q.append(cnt[ele] - opened)
prev = ele
opened = cnt[ele]
if len(q) == k:
opened -= q.popleft()
return opened == 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR RETURN VAR NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
C = Counter(nums)
while C:
start = min(C)
for i in range(start, start + k):
if i not in C:
return False
if C[i] == 1:
del C[i]
else:
C[i] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if k == 1:
return True
N = len(nums)
if N % k:
return False
nums = sorted(nums)
queue: List[List[int]] = list()
for index in range(N):
num = nums[index]
if len(queue) == 0:
new_group = [num, 1]
queue.append(new_group)
else:
current = queue.pop(0)
last_num = current[0]
size = current[1]
if last_num == num - 1:
if size == k - 1:
continue
else:
size += 1
update_group = [num, size]
queue.append(update_group)
elif last_num == num:
new_group = [num, 1]
queue.append(current)
queue.append(new_group)
else:
return False
if len(queue) == 0:
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
l = nums[:]
l.sort()
n = len(l)
d = Counter(l)
count = 0
for i in range(n):
if d[l[i]] != 0:
d[l[i]] -= 1
f = 0
for j in range(l[i] + 1, l[i] + k):
if d[j] == 0:
f = 1
break
d[j] -= 1
if f == 1:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums, k):
if len(nums) % k:
return False
c = Counter(nums)
heapify(nums)
for _ in range(len(nums) // k):
num = heappop(nums)
while not c[num]:
num = heappop(nums)
for i in range(k):
if not c[num + i]:
return False
c[num + i] -= 1
return True | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
count = collections.Counter(nums)
while count:
minCount = min(count)
for i in range(minCount, minCount + k):
if not count[i]:
return False
if count[i] == 1:
del count[i]
else:
count[i] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
q = collections.deque()
counter = collections.Counter(sorted(nums))
for item in counter.items():
number = item[0]
count = item[1]
if count < len(q):
return False
elif count > len(q):
for i in range(count - len(q)):
q.append(number + k - 1)
while len(q) != 0 and q[0] == number:
q.popleft()
return len(q) == 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
freq = Counter(nums)
start = []
for n, v in freq.items():
if freq[n - 1] == 0:
start.append(n)
while start:
num = start.pop()
if freq[num] == 0:
continue
else:
for t in range(num + k - 1, num - 1, -1):
if freq[t] < freq[num]:
return False
freq[t] -= freq[num]
if freq[t] == 0 and freq[t + 1] > 0:
start.append(t + 1)
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
nums.sort()
d = {}
for i in nums:
d[i] = d.get(i, 0) + 1
for j in d:
while d[j] > 0:
for x in range(j, j + k):
if d.get(x, 0) > 0:
d[x] -= 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR WHILE VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, hand: List[int], W: int) -> bool:
l = len(hand)
if l == 0 or l % W != 0:
return False
counter = collections.Counter(hand)
heapq.heapify(hand)
pos = 0
while pos < l:
while not hand[0] in counter:
heapq.heappop(hand)
currMin = hand[0]
counter[currMin] -= 1
pos += 1
if counter[currMin] == 0:
del counter[currMin]
for i in range(W - 1):
nextDraw = currMin + i + 1
if nextDraw in counter and counter[nextDraw] > 0:
counter[nextDraw] -= 1
pos += 1
if counter[nextDraw] == 0:
del counter[nextDraw]
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
count = collections.Counter(nums)
if len(count):
start = min(count)
while len(count):
if count[start] == 0:
start = min(count)
for num in range(start, start + k):
if count[num]:
count[num] -= 1
else:
return False
if count[num] == 0:
del count[num]
start += 1
return True
return False | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | def isPossibleDivide(nums, k):
if len(nums) % k != 0:
return False
else:
nums.sort()
return decreaseConquer(nums, k)
def decreaseConquer(A, k):
if len(A) == 0:
return True
else:
pivot = A[len(A) - 1]
for i in range(k):
j = binarySearch(A, 0, len(A) - 1, pivot - i)
if j == -1:
return False
else:
del A[j]
return decreaseConquer(A, k)
def binarySearch(S, l, r, x):
if l <= r:
mid = l + (r - l) // 2
if S[mid] == x:
return mid
elif S[mid] > x:
return binarySearch(S, l, mid - 1, x)
else:
return binarySearch(S, mid + 1, r, x)
else:
return -1
A = [1, 2, 3, 3, 4, 4, 5, 6]
k = 4
ans = isPossibleDivide(A, k)
print(ans)
class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
return isPossibleDivide(nums, k) | FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k > 0:
return False
d = {}
for num in nums:
d[num] = d.get(num, 0) + 1
for num in nums:
if d[num] > 0 and (num - 1 not in d or d[num - 1] < d[num]):
for _ in range(k):
if num not in d or d[num] == 0:
return False
d[num] -= 1
num += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
count = collections.defaultdict(int)
nums.sort()
for n in nums:
if count[n] == 0:
for i in range(1, k):
count[n + i] += 1
else:
count[n] -= 1
for c in count.values():
if c > 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
freq = {}
nums.sort()
for n in nums:
if n in freq:
freq[n] += 1
else:
freq[n] = 1
for num in freq:
if freq[num] != 0:
temp = freq[num]
for i in range(num, num + k):
if i not in freq or freq[i] < temp:
return False
else:
freq[i] -= temp
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
count = dict(Counter(nums))
nums = sorted(list(set(nums)))
for num in nums:
if count[num] != 0:
for i in range(1, k):
if num + i not in count:
return False
else:
count[num + i] -= count[num]
count[num] = 0
return all([(a == 0) for a in list(count.values())]) | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
if len(nums) % k != 0:
return False
counts = Counter(nums)
for num in sorted(counts):
prev = num
frequency = counts[num]
if counts[num] > 0:
for i in range(1, k):
nextNum = prev + 1
if nextNum not in counts:
return False
elif counts[nextNum] - frequency < 0:
return False
counts[nextNum] -= frequency
prev = nextNum
counts[num] -= frequency
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
Return True if its possible otherwise return False.
Example 1:
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Example 2:
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: true
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
Example 3:
Input: nums = [3,3,2,2,1,1], k = 3
Output: true
Example 4:
Input: nums = [1,2,3,4], k = 3
Output: false
Explanation: Each array should be divided in subarrays of size 3.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length | class Solution:
def isPossibleDivide(self, nums, k):
if len(nums) % k:
return False
Ct = Counter(nums)
heapify(nums)
if len(Ct) > len(nums) * 0.9:
return self.isNStraightHand(nums, k)
while Ct:
start = heappop(nums)
while not Ct[start]:
start = heappop(nums)
for i in range(start, start + k):
if not Ct[i]:
return False
if Ct[i] == 1:
del Ct[i]
else:
Ct[i] -= 1
return True
def isNStraightHand(self, hand, W):
while hand:
last_item = heappop(hand)
leftover = []
for _ in range(W - 1):
if not hand:
return False
next_item = heappop(hand)
while next_item == last_item:
leftover.append(next_item)
if not hand:
return False
next_item = heappop(hand)
if next_item > last_item + 1:
return False
last_item = next_item
for item in leftover:
heappush(hand, item)
return True | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.