description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
---|---|---|
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
be = 1
n = collections.Counter(text)
maxl = 0
i = 1
re = []
now = text[0]
while i < len(text):
if now == text[i]:
be += 1
if now != text[i] or i == len(text) - 1:
res = be
if len(re) >= 2:
print(re)
com, lencom = re.pop(0)
if re[-1][1] == 1 and com == now:
res = lencom + be
if res < n[now]:
res += 1
maxl = max(maxl, res)
re.append((now, be))
now = text[i]
be = 1
i += 1
return maxl | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
inuse = collections.defaultdict(int)
left = collections.defaultdict(int)
MOVE_TO_THE_NEXT_CHAR = 1
REPLACE = 0
LEAVE_AS_IT_IS = -1
res, i, n = 1, 0, 0
for ch in text:
left[ch] += 1
for j, ch in enumerate(text):
inuse[ch] += 1
left[ch] -= 1
n = max(inuse[ch], n)
action = j - i - n
char_exists = False
if action >= MOVE_TO_THE_NEXT_CHAR:
inuse[text[i]] -= 1
left[text[i]] += 1
i += 1
elif action == REPLACE:
char_exists = left[ch] > 0
elif action == LEAVE_AS_IT_IS:
char_exists = True
if char_exists and j - i >= res:
res = j - i + 1
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
letters = {}
for i, char in enumerate(text):
if char in letters:
letters[char].append(i)
else:
letters[char] = [i]
if len(letters) == 1:
return len(text)
ans = 0
for letter in letters:
cur = 0
prev = 0
discarded = False
maxSoFar = 0
arr = letters[letter]
for j, pos in enumerate(arr):
if not j:
cur = 1
elif pos - arr[j - 1] == 1:
cur += 1
else:
if not discarded and prev:
discarded = True
elif not discarded and pos - arr[j - 1] > 2:
discarded = True
if prev + cur > maxSoFar:
maxSoFar = prev + cur
if pos - arr[j - 1] == 2:
prev = cur
cur = 1
else:
prev = 0
cur = 1
print(prev + cur)
if prev + cur > maxSoFar:
maxSoFar = prev + cur
if discarded:
maxSoFar += 1
if maxSoFar > ans:
ans = maxSoFar
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
count = Counter(text)
i = 0
diff = 1
sol = 0
while i < len(text):
c = text[i]
j = i + 1
found = False
while j < len(text):
if text[j] == c:
j += 1
else:
if found:
if count[c] >= j - i:
sol = max(sol, j - i)
else:
sol = max(sol, j - i - 1)
break
found = True
diff = j
j += 1
if j == len(text):
if count[c] > j - i and not found:
sol = max(sol, j - i + 1)
if count[c] >= j - i:
sol = max(sol, j - i)
else:
sol = max(sol, j - i - 1)
break
i = diff
return sol | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
max_char = ""
clist = [1] * len(text)
one_max = 1
compress_list = []
for i in range(1, len(text)):
if text[i] == text[i - 1]:
clist[i] = clist[i - 1] + 1
else:
clist[i] = 1
compress_list.append([text[i - 1], clist[i - 1]])
if clist[i] > one_max:
one_max = clist[i]
max_char = text[i]
compress_list.append([text[-1], clist[-1]])
print(compress_list)
two_count = []
two_max = 0
for i in range(2, len(compress_list)):
if compress_list[i - 1][1] > 1:
continue
if compress_list[i][0] != compress_list[i - 2][0]:
continue
if compress_list[i][1] + compress_list[i - 2][1] + 1 < two_max:
continue
c = compress_list[i][0]
swaped = False
for j in range(len(compress_list)):
if j >= i - 2 and j <= i:
continue
if compress_list[j][0] == c:
len2 = compress_list[i][1] + compress_list[i - 2][1] + 1
two_count.append(len2)
if len2 > two_max:
two_max = len2
swaped = True
if swaped:
continue
len2 = compress_list[i][1] + compress_list[i - 2][1]
two_count.append(len2)
if len2 > two_max:
two_max = len2
if one_max >= two_max:
char_count = 0
for i in range(len(compress_list)):
if compress_list[i][0] == max_char:
char_count += 1
if char_count > 1:
one_max += 1
return max(one_max, two_max) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
if not text:
return 0
compact = [[text[0], 1]]
max_ = 1
for i in range(1, len(text)):
if text[i] == compact[-1][0]:
compact[-1][1] += 1
else:
compact.append([text[i], 1])
max_ = max(max_, compact[-1][1])
last_idx = defaultdict(int)
for i in range(len(compact)):
last_idx[compact[i][0]] += compact[i][1]
for i in range(len(compact) - 2):
if (
compact[i][0] == compact[i + 2][0]
and compact[i + 1][1] == 1
and compact[i][1] + compact[i + 2][1] < last_idx[compact[i][0]]
):
max_ = max(max_, compact[i][1] + compact[i + 2][1] + 1)
if compact[i][0] == compact[i + 2][0] and compact[i + 1][1] == 1:
max_ = max(max_, compact[i][1] + compact[i + 2][1])
for i in range(len(compact)):
if last_idx[compact[i][0]] > compact[i][1]:
max_ = max(max_, compact[i][1] + 1)
return max_ | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
def get_possible(idx, cur, step, swap):
if idx >= len(positions):
return step + swap
if positions[idx] - 1 == cur:
return get_possible(idx + 1, positions[idx], step + 1, swap)
elif not swap:
return step
else:
return max(
step + 1,
get_possible(idx, cur + 1, step + 1, False),
get_possible(idx, positions[idx] - 1, 1, False),
get_possible(idx + 1, positions[idx], 1, True),
)
letters, ans = defaultdict(list), 0
for i, c in enumerate(text):
letters[c].append(i)
for _, positions in letters.items():
if len(positions) < 3:
ans = max(ans, len(positions))
continue
x = min(get_possible(1, positions[0], 1, True), len(positions))
ans = max(x, ans)
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
n = len(text)
win = collections.Counter()
def valid_win(win):
if len(win) > 2:
return False
if len(win) == 1:
return True
return not all(n >= 2 for n in win.values())
max_c_idx = {}
min_c_idx = {}
for i, c in enumerate(text):
if c not in max_c_idx:
max_c_idx[c] = i
elif i > max_c_idx[c]:
max_c_idx[c] = i
if c not in min_c_idx:
min_c_idx[c] = i
left, right = 0, 0
ans = 0
while right < n:
c = text[right]
right += 1
win[c] += 1
while not valid_win(win) and left < right:
d = text[left]
left += 1
win[d] -= 1
if win[d] == 0:
win.pop(d)
if len(win) == 1:
ans = max(ans, right - left)
else:
k1, k2 = win.keys()
if win[k1] == 1 and (max_c_idx[k2] >= right or min_c_idx[k2] < left):
ans = max(ans, right - left)
elif win[k2] == 1 and (max_c_idx[k1] >= right or min_c_idx[k1] < left):
ans = max(ans, right - left)
else:
ans = max(ans, right - left - 1)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
c = Counter(text)
window = Counter()
i = 0
ans = 1
for j in range(len(text)):
c[text[j]] -= 1
window[text[j]] += 1
if (
len(window) == 1
or len(window) == 2
and min(window.values()) == 1
and c[sorted(window, key=window.get)[1]] > 0
):
ans = max(ans, j - i + 1)
else:
c[text[i]] += 1
window[text[i]] -= 1
if window[text[i]] == 0:
del window[text[i]]
i += 1
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
d = {}
f = {}
for i in text:
d[i] = []
f[i] = f.get(i, 0) + 1
for i in list(d.keys()):
s = 0
same = 0
diff = 0
for j in text:
if j == i:
s = 1
if j == i and s == 1:
if diff:
d[i].append(-diff)
same += 1
diff = 0
else:
if same:
d[i].append(same)
diff += 1
same = 0
if same:
d[i].append(same)
if diff:
d[i].append(-diff)
l = 0
print(d)
for i in list(d.keys()):
for j, v in enumerate(d[i]):
if v > 0:
l = max(l, v)
if j + 2 < len(d[i]) and d[i][j + 1] == -1:
if d[i][j + 2] + d[i][j] < f[i]:
l = max(l, d[i][j + 2] + d[i][j] + 1)
else:
l = max(l, d[i][j + 2] + d[i][j])
elif d[i][j] < f[i]:
l = max(l, d[i][j] + 1)
return l | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
n = len(text)
ans = 1
mp = {}
for i in text:
if i in mp:
mp[i] += 1
else:
mp[i] = 1
for i in mp:
first = 0
second = 0
if i in mp:
for j in range(0, n):
if text[j] == i:
first += 1
if first + second < mp[i]:
ans = max(ans, first + second + 1)
else:
ans = max(ans, first + second)
else:
if (
j != 0
and j != n - 1
and text[j - 1] == i
and text[j + 1] == i
):
second = first
else:
second = 0
first = 0
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
lst, lookup = list(), dict()
ptr = 0
while ptr < len(text):
curr, count = text[ptr], 0
while ptr < len(text) and curr == text[ptr]:
count += 1
ptr += 1
lookup[curr] = lookup.get(curr, 0) + count
lst.append((curr, count))
res = 0
for i in range(len(lst)):
if 0 < i < len(lst) - 1:
if lst[i - 1][0] == lst[i + 1][0] and lst[i][1] == 1:
curlen = lst[i - 1][1] + lst[i + 1][1]
if lookup[lst[i - 1][0]] > curlen:
curlen += 1
res = max(res, curlen)
continue
curlen = lst[i][1]
if lookup[lst[i][0]] > curlen:
curlen += 1
res = max(res, curlen)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
ln = len(text)
if ln == 1:
return 1
mx = -1
start = 0
while start < ln:
print(start, ",", end="")
i = start + 1
inx = -1
while i < ln and (text[start] == text[i] or inx == -1):
if text[start] != text[i]:
inx = i
i += 1
print(i, ",", end="")
if inx != -1 and (text[start] in text[i:] or text[start] in text[:start]):
mx = max(mx, i - start)
elif inx == -1 and (text[start] in text[i:] or text[start] in text[:start]):
mx = max(mx, i - start + 1)
elif inx == -1:
mx = max(mx, i - start)
else:
mx = max(mx, i - start - 1)
if inx == -1:
start = i
else:
start = inx
print(mx)
return mx | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
hashmap = {}
wordlist = []
l, r, ans = 0, 0, 0
while r < len(text):
while r < len(text) and text[l] == text[r]:
r += 1
wordlist.append([text[l], r - l])
if text[l] not in hashmap:
hashmap[text[l]] = r - l
else:
hashmap[text[l]] += r - l
l = r
for ch, count in wordlist:
ans = max(ans, min(count + 1, hashmap[ch]))
for i in range(1, len(wordlist) - 1):
if wordlist[i - 1][0] == wordlist[i + 1][0] and wordlist[i][1] == 1:
ans = max(
ans,
min(
wordlist[i - 1][1] + wordlist[i + 1][1] + 1,
hashmap[wordlist[i + 1][0]],
),
)
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
text_cnt = collections.Counter(text)
d = collections.Counter()
l, r = 0, 0
maxcnt = 0
res = 0
N = len(text)
for r, ch in enumerate(text):
d[ch] += 1
maxcnt = max(maxcnt, d[ch])
while r - l + 1 > maxcnt + 1:
d[text[l]] -= 1
maxcnt = max(d.values())
l += 1
res = max(res, min(r - l + 1, text_cnt[ch]))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
for k, g in itertools.groupby(text):
print(list(g))
G = [[k, len(list(g))] for k, g in itertools.groupby(text)]
c = collections.Counter(text)
res = max(min(n + 1, c[k]) for k, n in G)
for i in range(1, len(G) - 1):
if G[i - 1][0] == G[i + 1][0] and G[i][1] == 1:
res = max(min(G[i - 1][1] + G[i + 1][1] + 1, c[G[i - 1][0]]), res)
return res | CLASS_DEF FUNC_DEF VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Solution:
def maxRepOpt1(self, text: str) -> int:
tcounter = Counter(text)
res = 0
counter = Counter()
i = 0
for j in range(len(text)):
counter[text[j]] += 1
while j - i + 1 - max(counter.values()) > 1:
counter[text[i]] -= 1
i += 1
curr_max = max(counter.values())
c = [k for k in counter if counter[k] == curr_max][0]
res = max(res, min(j - i + 1, tcounter[c]))
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000
text consist of lowercase English characters only. | class Unionfind:
def __init__(self, n):
self.par = [-1] * n
self.rank = [1] * n
def root(self, x):
r = x
while not self.par[r] < 0:
r = self.par[r]
t = x
while t != r:
tmp = t
t = self.par[t]
self.par[tmp] = r
return r
def unite(self, x, y):
rx = self.root(x)
ry = self.root(y)
if rx == ry:
return
if self.rank[rx] <= self.rank[ry]:
self.par[ry] += self.par[rx]
self.par[rx] = ry
if self.rank[rx] == self.rank[ry]:
self.rank[ry] += 1
else:
self.par[rx] += self.par[ry]
self.par[ry] = rx
def is_same(self, x, y):
return self.root(x) == self.root(y)
def count(self, x):
return -self.par[self.root(x)]
class Solution:
def maxRepOpt1(self, text: str) -> int:
n = len(text)
cnt = Counter(list(text))
uf = Unionfind(n)
for i in range(n - 1):
if text[i] == text[i + 1]:
uf.unite(i, i + 1)
ans = 0
for i in range(n):
ans = max(ans, uf.count(i) + (1 if uf.count(i) < cnt[text[i]] else 0))
for i in range(n - 2):
if text[i] == text[i + 2] and text[i] != text[i + 1]:
total = uf.count(i) + uf.count(i + 2)
ans = max(ans, total + (1 if total < cnt[text[i]] else 0))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR VAR |
Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.
As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.
-----Input-----
- First line of input contains an integer T denoting the number of test cases.
- For each test case, there are two lines.
- First line contains two space separated integers n, k as defined in the problem.
- Next line contains string s of size n.
-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.
-----Constraints-----
Subtask #1: 20 points
- 1 β€ T β€ 100, 1 β€ n β€ 20, 1 β€ k β€ n
Subtask #2: 35 points
- 1 β€ T β€ 102, 1 β€ n β€ 103, 1 β€ k β€ n
Subtask #3: 45 points
- 1 β€ T β€ 105, 1 β€ n β€ 105, 1 β€ k β€ n
- Sum of n over all the test cases is β€ 106
-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010
-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character). | def is_prime(n):
if n == 2 or n == 3:
return True
if n < 2 or n % 2 == 0:
return False
if n < 9:
return True
if n % 3 == 0:
return False
r = int(n**0.5)
f = 5
while f <= r:
if n % f == 0:
return False
if n % (f + 2) == 0:
return False
f += 6
return True
def decomp(n):
return [n] if n < 10 else decomp(n // 10) + [n % 10]
def compose(lst):
return (
0 if len(lst) < 1 else lst[len(lst) - 1] + compose(lst[0 : len(lst) - 1]) * 10
)
n_test = int(input())
def find_next_chunk(bin_str, start_pos, k):
len_str = len(bin_str)
if start_pos >= len_str:
return -1, -1
cur_pos = start_pos
cur_val = bin_str[start_pos]
while cur_pos < len_str:
if bin_str[cur_pos] != cur_val:
if cur_pos - start_pos >= k:
return start_pos, cur_pos - 1
else:
start_pos = cur_pos
cur_val = bin_str[cur_pos]
cur_pos += 1
if cur_pos - start_pos >= k:
return start_pos, cur_pos - 1
else:
return -1, -1
def fill_chunk(bin_str, start_pos, end_pos, k):
count = 1
list_bin_str = list(bin_str)
if bin_str[start_pos] == "0":
val = "1"
else:
val = "0"
while end_pos - start_pos - k * count + 1 > 0:
list_bin_str[start_pos + k * count - 1] = val
count += 1
if end_pos - start_pos - k * count + 1 == 0:
list_bin_str[start_pos + k * (count - 1) + 1] = val
count += 1
return count - 1, "".join(list_bin_str)
def fill_chunk_zero_one(bin_str):
list_bin_str = list(bin_str)
len_str = len(bin_str)
one_first_count = 0
zero_first_count = 0
for k in range(len_str):
if k % 2 == 0:
if list_bin_str[k] == "0":
one_first_count += 1
else:
zero_first_count += 1
elif list_bin_str[k] == "1":
one_first_count += 1
else:
zero_first_count += 1
if one_first_count <= zero_first_count:
for k in range(len_str):
if k % 2 == 0:
list_bin_str[k] = "1"
else:
list_bin_str[k] = "0"
return one_first_count, "".join(list_bin_str)
else:
for k in range(len_str):
if k % 2 == 0:
list_bin_str[k] = "0"
else:
list_bin_str[k] = "1"
return zero_first_count, "".join(list_bin_str)
for t in range(n_test):
n, k = list(map(int, input().split()))
bin_str = input()
bin_str = bin_str[0:n]
n_cnt = 0
if k == 1:
n_cnt, bin_str = fill_chunk_zero_one(bin_str)
else:
start_pos = 0
while True:
start_pos, end_pos = find_next_chunk(bin_str, start_pos, k + 1)
if end_pos == -1:
break
count, bin_str = fill_chunk(bin_str, start_pos, end_pos, k + 1)
start_pos = end_pos + 1
n_cnt += count
print(n_cnt)
print(bin_str) | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF RETURN VAR NUMBER LIST VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING WHILE BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING RETURN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING RETURN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.
As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.
-----Input-----
- First line of input contains an integer T denoting the number of test cases.
- For each test case, there are two lines.
- First line contains two space separated integers n, k as defined in the problem.
- Next line contains string s of size n.
-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.
-----Constraints-----
Subtask #1: 20 points
- 1 β€ T β€ 100, 1 β€ n β€ 20, 1 β€ k β€ n
Subtask #2: 35 points
- 1 β€ T β€ 102, 1 β€ n β€ 103, 1 β€ k β€ n
Subtask #3: 45 points
- 1 β€ T β€ 105, 1 β€ n β€ 105, 1 β€ k β€ n
- Sum of n over all the test cases is β€ 106
-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010
-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character). | def solve(l, k):
s = [x for x in l]
n = len(s)
r = 0
if k == 1:
s0 = s[:]
r0 = r1 = 0
for i, c in enumerate(s):
if i % 2:
if c == "0":
s0[i] = "1"
r0 += 1
elif c == "1":
s0[i] = "0"
r0 += 1
s1 = s[:]
for i, c in enumerate(s):
if i % 2 == 0:
if c == "0":
s1[i] = "1"
r1 += 1
elif c == "1":
s1[i] = "0"
r1 += 1
if r0 < r1:
r = r0
s = s0
else:
r = r1
s = s1
else:
i = j = 0
while j < n:
while j < n and s[i] == s[j]:
j += 1
if j - i > k:
for l in range(i + k, j, k + 1):
if l == j - 1:
l -= 1
s[l] = "0" if s[l] == "1" else "1"
r += 1
i = j
print(r)
print("".join(s))
for t in range(int(input())):
n, k = [int(x) for x in input().split()]
solve(input(), k) | FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR STRING STRING STRING VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.
As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.
-----Input-----
- First line of input contains an integer T denoting the number of test cases.
- For each test case, there are two lines.
- First line contains two space separated integers n, k as defined in the problem.
- Next line contains string s of size n.
-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.
-----Constraints-----
Subtask #1: 20 points
- 1 β€ T β€ 100, 1 β€ n β€ 20, 1 β€ k β€ n
Subtask #2: 35 points
- 1 β€ T β€ 102, 1 β€ n β€ 103, 1 β€ k β€ n
Subtask #3: 45 points
- 1 β€ T β€ 105, 1 β€ n β€ 105, 1 β€ k β€ n
- Sum of n over all the test cases is β€ 106
-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010
-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character). | T = int(input())
for i in range(T):
n, k = list(map(int, input().split()))
s = list(map(int, input()))
if k == 1:
s_0 = {}
s_1 = {}
c0 = 0
c1 = 0
for j in range(n):
if j % 2 == 0:
s_1[j] = 1
s_0[j] = 0
else:
s_1[j] = 0
s_0[j] = 1
for j in range(n):
if s_0[j] - s[j] != 0:
c0 += 1
if s_1[j] - s[j] != 0:
c1 += 1
if c0 < c1:
print(c0)
p_s = ""
for j in range(n):
p_s += str(s_0[j])
print(p_s)
else:
print(c1)
p_s = ""
for j in range(n):
p_s += str(s_1[j])
print(p_s)
else:
count = 1
c = s[0]
flips = 0
for j in range(1, n):
if s[j] == c:
count += 1
if count > k:
if j + 1 < n and s[j] == s[j + 1]:
if s[j] == 1:
s[j] = 0
else:
s[j] = 1
elif s[j - 1] == 1:
s[j - 1] = 0
else:
s[j - 1] = 1
flips += 1
count = 1
elif s[j] != c:
count = 1
c = s[j]
print(flips)
p_s = ""
for j in range(n):
p_s += str(s[j])
print(p_s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.
As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.
-----Input-----
- First line of input contains an integer T denoting the number of test cases.
- For each test case, there are two lines.
- First line contains two space separated integers n, k as defined in the problem.
- Next line contains string s of size n.
-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.
-----Constraints-----
Subtask #1: 20 points
- 1 β€ T β€ 100, 1 β€ n β€ 20, 1 β€ k β€ n
Subtask #2: 35 points
- 1 β€ T β€ 102, 1 β€ n β€ 103, 1 β€ k β€ n
Subtask #3: 45 points
- 1 β€ T β€ 105, 1 β€ n β€ 105, 1 β€ k β€ n
- Sum of n over all the test cases is β€ 106
-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010
-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character). | def r():
return list(map(int, input().split()))
def process(stack):
if len(stack) < k + 1:
ans.extend(stack)
return
if len(stack) % (k + 1) == 0:
for i in range(k, len(stack) - 1, k + 1):
stack[i] = (stack[i] + 1) % 2
stack[-2] = (stack[-2] + 1) % 2
else:
for i in range(k, len(stack), k + 1):
stack[i] = (stack[i] + 1) % 2
ans.extend(stack)
for i in range(eval(input())):
n, k = r()
arr = list(map(int, list(input())))
if k == 1:
s = []
c = 0
for i in range(n):
s.append((c + 1) % 2)
c += 1
s1 = []
c = 1
for i in range(n):
s1.append((c + 1) % 2)
c += 1
ans1, ans2 = 0, 0
for i in range(n):
if arr[i] != s1[i]:
ans2 += 1
if arr[i] != s[i]:
ans1 += 1
s1 = list(map(str, s1))
s = list(map(str, s))
if ans1 < ans2:
print(ans1)
print("".join(s))
else:
print(ans2)
print("".join(s1))
else:
ans = []
c = 1
count = 0
stack = [arr[0]]
for i in range(1, n):
if arr[i] == arr[i - 1]:
stack.append(arr[i])
else:
process(stack)
stack = [arr[i]]
process(stack)
count = 0
for i in range(n):
if ans[i] != arr[i]:
count += 1
print(count)
ans = list(map(str, ans))
print("".join(ans)) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.
As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.
-----Input-----
- First line of input contains an integer T denoting the number of test cases.
- For each test case, there are two lines.
- First line contains two space separated integers n, k as defined in the problem.
- Next line contains string s of size n.
-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.
-----Constraints-----
Subtask #1: 20 points
- 1 β€ T β€ 100, 1 β€ n β€ 20, 1 β€ k β€ n
Subtask #2: 35 points
- 1 β€ T β€ 102, 1 β€ n β€ 103, 1 β€ k β€ n
Subtask #3: 45 points
- 1 β€ T β€ 105, 1 β€ n β€ 105, 1 β€ k β€ n
- Sum of n over all the test cases is β€ 106
-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010
-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character). | t = int(input())
for i in range(t):
n, k = map(int, input().split())
a = input()
if k == 1:
cnt = 0
str1 = ""
str2 = ""
while cnt < n:
str1 += "0"
str2 += "1"
cnt += 1
if cnt == n:
break
str1 += "1"
str2 += "0"
cnt += 1
flip1 = 0
flip2 = 0
for j in range(n):
if a[j] != str1[j]:
flip1 += 1
if a[j] != str2[j]:
flip2 += 1
if flip1 < flip2:
print(flip1)
print(str1)
else:
print(flip2)
print(str2)
else:
flip = 0
con = 1
past = a[0]
for j in range(1, n, 1):
if a[j] == past:
con += 1
else:
con = 1
if con > k:
idx = j
if j + 1 < n and a[j] != a[j + 1]:
idx = j - 1
temp = list(a)
if temp[idx] == "1":
temp[idx] = "0"
else:
temp[idx] = "1"
a = "".join(temp)
con = 1
flip += 1
past = a[j]
print(flip)
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string.
As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.
-----Input-----
- First line of input contains an integer T denoting the number of test cases.
- For each test case, there are two lines.
- First line contains two space separated integers n, k as defined in the problem.
- Next line contains string s of size n.
-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.
-----Constraints-----
Subtask #1: 20 points
- 1 β€ T β€ 100, 1 β€ n β€ 20, 1 β€ k β€ n
Subtask #2: 35 points
- 1 β€ T β€ 102, 1 β€ n β€ 103, 1 β€ k β€ n
Subtask #3: 45 points
- 1 β€ T β€ 105, 1 β€ n β€ 105, 1 β€ k β€ n
- Sum of n over all the test cases is β€ 106
-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001
Output:
1
10
0
11
2
1010
-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character). | t = int(input())
for i in range(t):
x = input().split()
n = int(x[0])
k = int(x[1])
y = [int(i) for i in input()]
z = [(1 - i) for i in y]
ck = 1
res = 0
last = 0
for i in range(len(y)):
c = y[i]
if i != 0:
if c == y[i - 1]:
ck += 1
else:
last = ck
ck = 1
if ck > k:
if i == len(y) - 1:
y[i] = 1 - y[i]
elif y[i] == 1 - y[i + 1] and (last < k or k > 1):
y[i - 1] = 1 - y[i - 1]
ck = 1
last = 1
else:
y[i] = 1 - y[i]
last = ck - 1
ck = 1
res += 1
s = ""
for i in y:
s += str(i)
res1 = 0
ck = 1
last = 0
for i in range(len(z)):
c = z[i]
if i != 0:
if c == z[i - 1]:
ck += 1
else:
last = ck
ck = 1
if ck > k:
if i == len(z) - 1:
z[i] = 1 - z[i]
elif z[i] == 1 - z[i + 1] and (last < k or k > 1):
z[i - 1] = 1 - z[i - 1]
ck = 1
last = 1
else:
z[i] = 1 - z[i]
last = ck - 1
ck = 1
res1 += 1
s1 = ""
for i in z:
s1 += str(i)
res1 = n - res1
if res1 < res:
print(res1)
print(s1)
else:
print(res)
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | T = int(input())
for __ in range(T):
n = int(input())
l = list(map(int, input().split()))
i = 0
j = n - 1
flag = 1
if l[i] != 1:
flag = 0
else:
while i < j:
if l[i] != l[j]:
flag = 0
if l[i] + 1 != l[i + 1] and l[i] != l[i + 1]:
flag = 0
break
i += 1
j -= 1
if l[i] != 7:
flag = 0
if flag:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
req = [1, 2, 3, 4, 5, 6, 7]
l, r = 0, len(arr) - 1
flag = True
for i in range(7):
if arr[l] != req[i]:
flag = False
break
while l <= r and arr[l] == req[i]:
if arr[l] != arr[r]:
flag = False
break
l += 1
r -= 1
print("yes") if flag else print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
x = [1, 2, 3, 4, 5, 6, 7]
for i in range(t):
N = int(input())
a = list(map(int, input().split()))
rev = a[::-1]
dup = set(a)
if rev == a and list(dup) == x:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | T = int(input())
for test_case in range(T):
N = int(input())
A = tuple(int(_) for _ in input().split())
strict_incr_decr = True
seven_exists = A[(len(A) - 1) // 2] == 7
prev = 1
flip = False
for a in A:
if (
a > 7
or not flip
and a not in (prev, prev + 1)
or flip
and a not in (prev, prev - 1)
):
strict_incr_decr = False
break
prev = a
if a == 7:
flip = True
print("yes" if all([A == A[::-1], strict_incr_decr, seven_exists]) else "no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR STRING STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | from sys import stdin
l = []
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
beg = 0
end = n - 1
curr = 1
rainbow = True
while beg <= end:
if arr[beg] != arr[end]:
rainbow = False
break
elif arr[beg] == curr + 1:
curr += 1
if curr > 7:
rainbow = False
break
elif arr[beg] != curr:
rainbow = False
break
beg += 1
end -= 1
if rainbow and curr == 7:
l.append("yes")
else:
l.append("no")
for i in l:
print(i) | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
low, high, ans = 0, n - 1, "yes"
if arr[low] != 1:
ans = "no"
while low < high:
if arr[low] != arr[low + 1] and arr[low + 1] != arr[low] + 1:
ans = "no"
break
if arr[low] != arr[high]:
ans = "no"
break
low += 1
high -= 1
if arr[low] != 7:
ans = "no"
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR VAR VAR ASSIGN VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for i in range(t):
l = int(input())
li = list(map(int, input().split()))[:l]
a = []
b = [1, 2, 3, 4, 5, 6, 7]
c = li[::-1]
for i in li:
if i not in a:
a.append(i)
a.sort()
if a == b and li == c:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | tkk = int(input())
for z in range(tkk):
n = int(input())
lt = list(map(int, input().split()))
x = len(lt)
t = 1
f = "yes"
for i in range(x - 1):
if lt[i] == t and lt[x - i - 1] == t:
if lt[i + 1] != t:
t += 1
if t > 7:
break
else:
f = "no"
break
if t < 7:
f = "no"
print(f) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | tests = int(input())
for i in range(tests):
length = int(input())
arr = list(map(int, input().split()))
ans = "yes"
if sum(set(arr)) != 28 or max(arr) != 7:
ans = "no"
else:
i = 0
maximum = 1
j = length - 1
while i < j:
if arr[i] != arr[j] or arr[i] < maximum:
ans = "no"
break
maximum = max(maximum, arr[i])
i += 1
j -= 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
ans = "no"
tra = 1
j = 1
if arr == arr[::-1] and {1, 2, 3, 4, 5, 6, 7} == set(arr):
ans = "yes"
for i in arr:
if tra == 7:
j = -1
if i == tra:
continue
elif i == tra + j:
tra += j
else:
ans = "no"
break
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | def validate_rainbow(arr):
flag1 = True
flag2 = True
flag3 = True
flag4 = True
i, n = 0, len(arr) - 1
while i <= n:
if arr[i] != arr[n]:
flag1 = False
i += 1
n -= 1
for i in range(1, len(arr) // 2):
if arr[i] > arr[i + 1]:
flag2 = False
for i in range(len(arr)):
if arr[i] < 1 or arr[i] > 7:
flag3 = False
if sum(set(arr)) != 28:
flag4 = False
if flag1 and flag2 and flag3 and flag4:
return "yes"
return "no"
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
print(validate_rainbow(arr)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for T in range(t):
n = int(input())
q = [int(x) for x in input().split()]
prev = 1
flag = 0
if not q[0] == 1:
print("no")
continue
for i in range(-(-n // 2)):
if (
(q[i] == prev or q[i] == prev + 1)
and q[i] == q[n - i - 1]
and q[i] > 0
and q[i] < 8
):
pass
else:
flag = 1
continue
prev = q[i]
if prev != 7:
flag = 1
if flag:
print("no")
else:
print("yes") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for i in range(t):
k = int(input())
rain = list(map(int, input().split()))
h = 1
f = 0
for i in range(k - 1):
if rain[i] == h and rain[k - i - 1] == h:
if rain[i + 1] != h:
h = h + 1
if h > 7:
break
else:
f = 1
break
if f == 0 and h >= 7:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | def calc(array):
if max(array) == 7 and 1 in array:
if array == array[::-1] and len(array) >= 13:
for i in range(len(array)):
if abs(array[i - 1] - array[i]) > 1:
return "no"
return "yes"
return "no"
n = int(input())
for i in range(n):
length = int(input())
ins = list(map(int, input().split(" ")))
print(calc(ins)) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN STRING RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | tests = int(input())
for i in range(tests):
length = int(input())
arr = list(map(int, input().split()))
repetitions = [0] * 7
exists = [0] * 7
ans = "yes"
seven_found = False
for j in range(length):
if j != 0 and (
seven_found
and arr[j] > arr[j - 1]
or not seven_found
and arr[j] < arr[j - 1]
):
ans = "no"
break
if arr[j] > 7:
ans = "no"
break
else:
exists[arr[j] - 1] = 1
if arr[j] == 7:
seven_found = True
elif not seven_found:
repetitions[arr[j] - 1] += 1
else:
repetitions[arr[j] - 1] -= 1
if min(repetitions) != 0 or max(repetitions) != 0 or sum(exists) != 7:
ans = "no"
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
l = 0
r = n - 1
c = 1
while l <= r:
if arr[l] == arr[r] and arr[l] == c:
l += 1
r -= 1
else:
c += 1
if arr[l] == arr[r] and arr[l] == c:
l += 1
r -= 1
else:
c -= 1
break
if c == 7:
print("yes")
else:
print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
c = 1
mid = n // 2
for i in range(mid + 1):
if l[i] == c:
c += 1
if c < 8 or l[:] != l[::-1]:
print("no")
else:
print("yes") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for i in range(int(input())):
size = int(input())
nums = list(map(int, input().split()))
s = sorted(list(set(nums)))
dup = []
for j in range(len(nums) // 2 + 1):
if nums[j] not in dup:
dup.append(nums[j])
if s == [1, 2, 3, 4, 5, 6, 7] and dup == [1, 2, 3, 4, 5, 6, 7]:
flag = 1
else:
flag = 0
if nums == nums[::-1] and flag == 1:
print("yes")
else:
print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | def rb():
try:
num_elements = int(input())
array = list(map(int, input().split()))
master = 1
flag = 0
for i in range(num_elements // 2 + 1):
if array[i] == array[num_elements - 1 - i] and (
array[i] == master or array[i] == master + 1
):
master = array[i]
else:
flag = 1
break
if array[num_elements // 2] == 7 and flag == 0:
print("yes")
else:
print("no")
except Exception as e:
pass
t = int(input())
for i in range(t):
rb() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
t = 1
for i in range(n):
if arr[i] != arr[n - i - 1]:
t = 0
break
if n % 2 == 0:
m = n // 2
else:
m = n // 2 + 1
for j in range(1, m):
if arr[j - 1] + 1 == arr[j] or arr[j - 1] == arr[j]:
pass
else:
t = 0
break
for k in range(n):
if 1 <= arr[k] <= 7:
pass
else:
t = 0
break
if t and len(set(arr)) == 7:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
required = "1234567"
for _ in range(t):
n = int(input())
x = list(map(int, input().split(" ")))
str_x = sorted(list(set(x)))
str_x = [str(i) for i in str_x]
str_x = "".join(str_x)
if str_x != required:
print("no")
else:
flag = True
for i in range(n // 2):
if x[i] != x[n - i - 1]:
flag = False
break
if i != 0 and x[i] - x[i - 1] not in [0, 1]:
flag = False
break
print("yes" if flag else "no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
li = []
for j in l:
if j not in li:
li.append(j)
li.sort()
print("yes" if li == [1, 2, 3, 4, 5, 6, 7] and l == l[::-1] else "no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR VAR NUMBER STRING STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for t_ in range(t):
n = int(input())
mid = (n - 1) // 2
arr = list(map(int, input().split()))
valid = True
if arr[0] != 1 or arr[mid] != 7:
valid = False
for i in range(0, mid):
if arr[i + 1] != arr[i] and arr[i + 1] != arr[i] + 1:
valid = False
break
if arr != list(reversed(arr)):
valid = False
if valid:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | def check(L, n):
if L == L[::-1] and L[0] == 1:
for i in range(n // 2):
if L[i + 1] == L[i] or L[i + 1] == L[i] + 1:
continue
else:
return "no"
if L[i + 1] == 7:
return "yes"
return "no"
for T in range(int(input())):
n = int(input())
print(check(list(map(int, input().split())), n)) | FUNC_DEF IF VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
m = [int(i) for i in input().split()]
z = 1
for i in range(n // 2 + 1):
if m[i] == z:
z = z + 1
print("no" if m != m[::-1] or z < 8 else "yes") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER STRING STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | refList = [1, 2, 3, 4, 5, 6, 7]
t = int(input())
for i in range(t):
a = int(input())
list1 = list(map(int, input().split()))[:a]
sevenInt = []
revList = list1[::-1]
for i in list1:
if i not in sevenInt:
sevenInt.append(i)
if sevenInt == refList and list1 == revList:
print("yes")
else:
print("no") | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | def rbow(a, n):
for i in range(1, 8):
if i not in a:
return "no"
l = n // 2 if n % 2 == 0 else n // 2 + 1
for i in range(1, l):
if a[i] < a[i - 1]:
return "no"
for i in range(l):
if a[i] != a[-(i + 1)]:
return "no"
return "yes"
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
print(rbow(a, n)) | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
if 7 not in l:
print("no")
continue
s = l.index(7)
k = list(set(l[: s + 1]))
t = [i for i in range(1, 8)]
if k == t and l == l[::-1]:
print("yes")
else:
print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for i in range(int(input())):
r = [1, 2, 3, 4, 5, 6, 7]
N = int(input())
li = list(map(int, input().split()))
x = []
for j in li:
if j not in x:
x.append(j)
if li == li[::-1] and x == r:
print("yes")
else:
print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for i in range(int(input())):
N = int(input())
L = list(map(int, input().split()))
arr = [0] * 7
j = 0
number = 1
pred = 0
suc = N - 1
res = ""
while suc >= pred:
if L[pred] == number and L[pred] == L[suc]:
arr[L[pred] - 1] += 1
pred += 1
suc -= 1
elif L[pred] != number and L[pred] == number + 1:
number += 1
if number > 7:
res = "no"
break
else:
res = "no"
break
if res == "no" or min(arr) == 0:
print("no")
else:
print("yes") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | test = int(input())
while test:
flag = 1
n = int(input())
elem = list(map(int, input().split()))
p1, p2 = 0, n - 1
if elem[0] != 1:
flag = 0
else:
while p1 < p2:
if elem[p1] != elem[p2]:
flag = 0
break
if not (elem[p1] == elem[p1 + 1] or elem[p1] + 1 == elem[p1 + 1]):
flag = 0
break
if elem[p1] > 7:
flag = 0
break
p1 += 1
p2 -= 1
if elem[p1] != 7:
flag = 0
if flag == 1:
print("yes")
else:
print("no")
test -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for _ in range(int(input())):
flag = 0
n = int(input())
l1 = list(map(int, input().split()))
if l1[::-1] != l1:
print("no")
flag = -1
if flag != -1:
d = {}
for i in l1:
d[i] = 1
l2 = list(d.keys())
temp = max(l2)
for i in range(1, temp + 1):
if i not in l2:
print("no")
flag = -1
break
for i in range(0, n // 2):
if l1[i] > l1[i + 1]:
print("no")
flag = -1
if l1[n // 2] != 7 and l1[n // 2 + 1] != 7 and flag != -1:
print("no")
flag = -1
if flag != -1:
print("yes") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | def distinct(a):
b = []
for i in a:
if a not in b:
b.append(i)
return b
k = [1, 2, 3, 4, 5, 6, 7]
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if (
a == a[::-1]
and set(distinct(a)) == set(k)
and sorted(a[: n // 2]) == a[: n // 2]
):
print("yes")
else:
print("no") | FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for i in range(int(input())):
n = int(input())
list1 = list(map(int, input().split()))
check = False
for i in range(1, 8):
if i not in list1:
check = True
break
if check:
print("no")
else:
check = True
list2 = []
for i in range(len(list1)):
if list1[i] == 7:
list2.insert(i, list1[i])
break
else:
list2.insert(i, list1[i])
for i in range(len(list2) - 1):
if list2[i] <= list2[i + 1]:
check = True
else:
check = False
break
if not check:
print("no")
else:
for i in range(int(len(list1) / 2)):
if list1[i] != list1[-i - 1]:
check = False
break
else:
check = True
if check:
print("yes")
else:
print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
s = set(l)
if sum(s) != 28:
print("no")
else:
k = n // 2
if l[0 : k + 1] != sorted(l[0 : k + 1]):
print("no")
elif l == l[::-1]:
print("yes")
else:
print("no") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef likes all arrays equally. But he likes some arrays more equally than others. In particular, he loves Rainbow Arrays.
An array is Rainbow if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
- Next a6 elements equal 6.
- Next a5 elements equal 5.
- Next a4 elements equal 4.
- Next a3 elements equal 3.
- Next a2 elements equal 2.
- Next a1 elements equal 1.
- ai can be any non-zero positive integer.
- There are no other elements in array.
Help Chef in finding out if the given array is a Rainbow Array or not.
-----Input-----
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains an integer N, denoting the number of elements in the given array.
- The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
-----Output-----
- For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
-----Constraints-----
- 1 β€ T β€ 100
- 7 β€ N β€ 100
- 1 β€ Ai β€ 10
-----Subtasks-----
- Subtask 1 (100 points) : Original constraints
-----Example-----
Input
3
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
14
1 2 3 4 5 6 7 6 5 4 3 2 1 1
13
1 2 3 4 5 6 8 6 5 4 3 2 1
Output
yes
no
no
-----Explanation-----
The first example satisfies all the conditions.
The second example has 1 element of value 1 at the beginning and 2 elements of value 1 at the end.
The third one has no elements with value 7 after elements with value 6. | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
i = 0
j = n - 1
ans = True
r = []
while i <= j:
if arr[i] == arr[j]:
r.append(arr[i])
i += 1
j -= 1
else:
ans = False
break
if sorted(r) != r:
ans = False
for i in r:
if i < 1 and i > 7:
ans = False
break
for i in range(1, 8):
if i not in r:
ans = False
break
if ans:
print("yes")
else:
print("no") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given a prime number p, n integers a_1, a_2, β¦, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 β€ i < j β€ n) for which (a_i + a_j)(a_i^2 + a_j^2) β‘ k mod p.
Input
The first line contains integers n, p, k (2 β€ n β€ 3 β
10^5, 2 β€ p β€ 10^9, 0 β€ k β€ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ p-1). It is guaranteed that all elements are different.
Output
Output a single integer β answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 β‘ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 β‘ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 β‘ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6). | def count(n, p, k, L):
s = {}
for x in L:
m = (x**4 - x * k) % p
if m in s:
s[m] += 1
else:
s[m] = 1
res = 0
for x in s:
y = s[x]
res += y * (y - 1) // 2
return res
n, p, k = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
print(count(n, p, k, L)) | FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
start = 0
end = n - 1
res = 0
while start < end:
mini = min(height[start], height[end])
area = (end - start - 1) * mini
if res < area:
res = area
if height[start] < height[end]:
start += 1
else:
end -= 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
ans = 0
if n <= 2:
return 0
low = 0
high = n - 1
while low < high:
length = high - low - 1
candy = length * min(arr[low], arr[high])
if candy > ans:
ans = candy
if arr[low] <= arr[high]:
low += 1
else:
high -= 1
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
obj = Solution()
print(obj.maxCandy(arr, n)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
i = 0
mc = 0
j = n - 1
while i < j:
if height[i] < height[j]:
mc = max(mc, (j - i - 1) * height[i])
i = i + 1
else:
mc = max(mc, (j - i - 1) * height[j])
j = j - 1
return mc | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
i = 0
j = n - 1
ans = 0
y = 0
while i < j:
y = min(height[i], height[j])
ans = max(ans, (j - i - 1) * y)
if y == height[i]:
i += 1
else:
j -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, A, N):
ans = 0
i, j = 0, N - 1
while j - i > 1:
ans = max(ans, (j - i - 1) * min(A[i], A[j]))
if A[i] <= A[j]:
i += 1
else:
j -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
if n == 1 or n == 2:
return 0
res = -1
l, r = 0, n - 1
while l < r:
res = max(res, min(height[l], height[r]) * (r - l - 1))
if height[l] < height[r]:
l += 1
else:
r -= 1
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, a, n):
i = 0
j = n - 1
ans = 0
while i < j:
if a[i] > a[j]:
ans = max(ans, a[j] * (j - i - 1))
j -= 1
elif a[i] < a[j]:
ans = max(ans, a[i] * (j - i - 1))
i += 1
else:
ans = max(ans, a[j] * (j - i - 1))
i += 1
j -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
maxcandy = 0
i = 0
j = len(height) - 1
while i < j:
maxcandy = max(maxcandy, min(height[i], height[j]) * (j - i - 1))
if height[i] > height[j]:
j -= 1
else:
i += 1
return maxcandy | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, arr, n):
start = 0
end = n - 1
if n == 1:
return 0
res = float("-inf")
while start <= end:
if arr[start] > arr[end]:
res = max(res, arr[end] * (end - start - 1))
end -= 1
else:
res = max(res, arr[start] * (end - start - 1))
start += 1
return res
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
obj = Solution()
print(obj.maxCandy(arr, n)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
if n == 1:
return 0
output = min(height[0], height[-1]) * (n - 2)
i = -1
while i < n - 1:
i += 1
output = max(output, min(height[i], height[-1]) * (n - i - 2))
while i < n - 1 and height[-1] <= height[i]:
height.pop()
n -= 1
output = max(output, min(height[i], height[-1]) * (n - i - 2))
return output | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
if n <= 2:
return 0
ans = 0
i = 0
j = n - 1
while i < j:
if height[i] < height[j]:
ans = max(ans, height[i] * (j - i - 1))
i += 1
if height[i] > height[j]:
ans = max(ans, height[j] * (j - i - 1))
j -= 1
if height[i] == height[j]:
ans = max(ans, height[i] * (j - i - 1))
i += 1
j -= 1
return ans
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
obj = Solution()
print(obj.maxCandy(arr, n)) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
i = 0
j = len(height) - 1
a = 0
s = 0
while i < j:
if height[i] == height[j]:
s = height[i] * (j - i - 1)
i += 1
j -= 1
elif height[i] < height[j]:
s = height[i] * (j - i - 1)
i += 1
else:
s = height[j] * (j - i - 1)
j -= 1
if a < s:
a = s
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
start = 0
end = n - 1
global_sum = 0
while start < end:
local_sum = 0
local_sum = min(height[start], height[end]) * (end - start - 1)
global_sum = max(global_sum, local_sum)
if height[start] < height[end]:
start += 1
else:
end -= 1
return global_sum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
i = 0
j = len(height) - 1
maxi = 0
while i < j:
if height[i] <= height[j]:
area = (j - i - 1) * height[i]
maxi = max(maxi, area)
i = i + 1
elif height[i] > height[j]:
area = (j - i - 1) * height[j]
maxi = max(maxi, area)
j = j - 1
return maxi | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
def calc(p, q, r):
l = min(p, q)
b = r
return max(ans, l * b)
s = 0
e = n - 1
ans = 0
while s < e:
b = e - s - 1
area = calc(height[s], height[e], b)
ans = max(ans, area)
if height[s] < height[e]:
s += 1
else:
e -= 1
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
left, right = 0, len(height) - 1
area = 0
b = 0
length = 0
while left < right:
length = right - left - 1
b = min(height[right], height[left])
area = max(area, length * b)
if height[left] < height[right]:
left += 1
else:
right -= 1
return area | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
h = height
if n < 3:
return 0
p = 0
q = n - 1
a = 0
m = (n - 2) * min(h[0], h[n - 1])
while p < q:
a = min(h[p], h[q]) * (q - p - 1)
m = max(a, m)
if h[p] < h[q]:
p += 1
else:
q -= 1
return m | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
if n == 1 or n == 2:
return 0
front = 0
tail = n - 1
ans = 0
while front < tail:
ans = max(ans, (tail - front - 1) * min(height[front], height[tail]))
if height[front] > height[tail]:
tail -= 1
elif height[front] < height[tail]:
front += 1
else:
front += 1
tail -= 1
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
a = height
n = len(a)
left = 0
right = n - 1
area = 0
while left < right:
area = max(area, (right - left - 1) * min(a[left], a[right]))
if a[left] < a[right]:
left = left + 1
elif a[left] > a[right]:
right = right - 1
else:
left = left + 1
right = right - 1
return area
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
obj = Solution()
print(obj.maxCandy(arr, n)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
j = n - 1
i = 0
a = height
ans = 0
res = 0
while i < j:
ans = (j - i - 1) * min(a[i], a[j])
if ans > res:
res = ans
if a[i] > a[j]:
j -= 1
else:
i += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
i = 0
j = n - 1
d = 0
while i < j:
g = (j - i - 1) * min(height[i], height[j])
if height[i] > height[j]:
j -= 1
else:
i += 1
if g > d:
d = g
return d | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
if len(height) <= 2:
return 0
i = 0
j = n - 1
ans = 0
while i < j:
maxh = min(height[i], height[j])
gap = maxh * (j - i - 1)
ans = max(ans, gap)
if height[i] > height[j]:
j -= 1
else:
i += 1
return ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
s = 0
e = n
res = 0
while s < e:
if res < (e - s - 2) * min(height[s], height[e - 1]):
res = (e - s - 2) * min(height[s], height[e - 1])
elif height[s] <= height[e - 1]:
s += 1
elif height[s] >= height[e - 1]:
e -= 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, a, n):
i, j = 0, n - 1
ans = 0
while j > i:
if a[j] >= a[i]:
ans = max(ans, a[i] * (j - i - 1))
i = i + 1
continue
if a[i] > a[j]:
ans = max(ans, a[j] * (j - i - 1))
j = j - 1
continue
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek wants to make a special space for candies on his bookshelf. Currently, it has N books, each of whose height is represented by the array height[] and has unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books. The task is to find out the area between two books that can hold the maximum candies without changing the original position of selected books.
Example 1:
Input: N = 3, height[] = {1, 3, 4}
Output: 1
Explanation:
1. Area between book of height 1 and book of
height 3 is 0 as there is no space between
them.
2. Area between book of height 1 and book of
height 4 is 1 by removing book of height 3.
3. Area between book of height 3 and book of
height 4 is 0 as there is no space between them.
Example 2:
Input: N = 6, height[] = {2, 1, 3, 4, 6, 5}
Output: 8
Explanation: Remove the 4 books in the middle
creating length = 4 for the candy dam. Height
for dam will be min(2,5) = 2.
Area between book of height 2 and book
of height 5 is 2 x 4 = 8.
Your Task:
You don't need to read input or print anything. Complete the function maxCandy() which takes the array height[] and size of array N as input parameters and returns the maximum candies geek can store
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 β€ N β€ 10^{5}
1 β€ height[i] β€ 10^{5} | class Solution:
def maxCandy(self, height, n):
i = 0
j = n - 1
max1 = 0
while i < j:
max1 = max((j - i - 1) * min(height[i], height[j]), max1)
if height[i] < height[j]:
i += 1
elif height[i] > height[j]:
j -= 1
elif height[i] == height[j]:
if height[i + 1] > height[j - 1]:
i = i + 1
else:
j = j - 1
return max1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12
Output: 21
Example 2:
Input: 21
Output: -1 | class Solution:
def nextGreaterElement(self, n):
digits = list(map(int, list(str(n))))
last = None
for i in range(0, len(digits) - 1):
if digits[i + 1] > digits[i]:
last = i
if last == None:
return -1
rst = ""
i = last
for j in range(0, i):
rst += str(digits[j])
p = digits[i]
for x in range(p + 1, 10):
if x in digits[i + 1 :]:
break
rst += str(x)
used = False
for p in sorted([digits[i]] + digits[i + 1 :]):
if p == x and not used:
used = True
else:
rst += str(p)
f = int(rst)
if f > 2147483647:
return -1
else:
return f | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12
Output: 21
Example 2:
Input: 21
Output: -1 | class Solution:
def nextGreaterElement(self, n):
nn = str(n)
l = len(nn)
if l < 2:
return -1
x = -1
for i in range(l - 1, 0, -1):
if nn[i] > nn[i - 1]:
x = i - 1
break
if x == -1:
return -1
y = nn[x + 1]
yy = x + 1
for i in range(x + 1, l):
if nn[i] > nn[x]:
y = min(y, nn[i])
if y == nn[i]:
yy = i
left = nn[:x] + y
right = nn[yy + 1 : l] + nn[x] + nn[x + 1 : yy]
res = int(left + "".join(sorted(right)))
return res if res < 2**31 else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL STRING FUNC_CALL VAR VAR RETURN VAR BIN_OP NUMBER NUMBER VAR NUMBER |
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12
Output: 21
Example 2:
Input: 21
Output: -1 | class Solution:
def nextGreaterElement(self, n):
s = [i for i in str(n)]
exist = -1
for i in range(len(s) - 1, 0, -1):
if s[i - 1] < s[i]:
temp = sorted(s[i - 1 :])
pivot = temp.index(s[i - 1])
for j in range(pivot + 1, len(temp)):
if temp[j] > s[i - 1]:
pivot = j
break
s[i - 1] = temp[pivot]
del temp[pivot]
s[i:] = temp
exist = 1
break
ret = int("".join(s))
if exist == 1 and ret < 2147483647:
return ret
else:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN NUMBER |
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12
Output: 21
Example 2:
Input: 21
Output: -1 | class Solution:
def nextGreaterElement(self, n):
sn = str(n)
nlen = len(sn)
if nlen == 1:
return -1
else:
i, j = -1, -2
end, check = i, True
sl = list(sn)
while j != -nlen and sl[j] >= sl[i]:
if sl[end] < sl[j] and check:
end = j
check = False
i -= 1
j -= 1
if sl[j] == sl[end]:
if i > j:
end = i
if sl[j] == min(sl):
end = -1
sl[j], sl[end] = sl[end], sl[j]
front, back = sl[: j + 1], sorted(sl[j + 1 :])
result = int("".join(front + back))
return result if result > n and result < 2147483647 else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR NUMBER |
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12
Output: 21
Example 2:
Input: 21
Output: -1 | class Solution:
def nextGreaterElement(self, n):
s = str(n)
l = len(s)
digits = [int(c) for c in s]
i = l - 1
while i > 0 and s[i] <= s[i - 1]:
i -= 1
if i == 0:
return -1
j = l - 1
while j > i - 1 and s[j] <= s[i - 1]:
j -= 1
digits[i - 1], digits[j] = digits[j], digits[i - 1]
res = digits[:i] + sorted(digits[i:])
res = int("".join(str(d) for d in res))
if res > 2**31:
return -1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR |
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Input: 12
Output: 21
Example 2:
Input: 21
Output: -1 | class Solution:
def nextGreaterElement(self, n):
nums = list(str(n))
size = len(nums)
for i in range(size - 1, -1, -1):
if nums[i - 1] < nums[i]:
break
if i > 0:
for j in range(size - 1, -1, -1):
if nums[j] > nums[i - 1]:
nums[i - 1], nums[j] = nums[j], nums[i - 1]
break
for k in range((size - i) // 2):
nums[i + k], nums[size - k - 1] = nums[size - k - 1], nums[i + k]
ans = int("".join(nums))
return n < ans <= 2147483647 and ans or -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR VAR NUMBER VAR NUMBER |
You have two strings $s_1$ and $s_2$ of length $n$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times:
Choose a positive integer $1 \leq k \leq n$.
Swap the prefix of the string $s_1$ and the suffix of the string $s_2$ of length $k$.
Is it possible to make these two strings equal by doing described operations?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then the test cases follow.
Each test case consists of three lines.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the length of the strings $s_1$ and $s_2$.
The second line contains the string $s_1$ of length $n$, consisting of lowercase English letters.
The third line contains the string $s_2$ of length $n$, consisting of lowercase English letters.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise.
-----Examples-----
Input
7
3
cbc
aba
5
abcaa
cbabb
5
abcaa
cbabz
1
a
a
1
a
b
6
abadaa
adaaba
8
abcabdaa
adabcaba
Output
YES
YES
NO
YES
NO
NO
YES
-----Note-----
In the first test case:
Initially $s_1 = \mathtt{cbc}$, $s_2 = \mathtt{aba}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{abc}$, $s_2 = \mathtt{abc}$.
In the second test case:
Initially $s_1 = \mathtt{abcaa}$, $s_2 = \mathtt{cbabb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{bbcaa}$, $s_2 = \mathtt{cbaab}$.
Operation with $k = 3$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbbc}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{cabaa}$, $s_2 = \mathtt{cbbba}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{babaa}$, $s_2 = \mathtt{cbbca}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbcb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{cbbaa}$, $s_2 = \mathtt{cbbaa}$.
In the third test case, it's impossible to make strings equal. | t = int(input())
for i in range(t):
length = int(input())
s1 = list(input())
s2 = list(input())
tmp = dict()
for j in range(0, length):
c1 = s1[j]
c2 = s2[length - j - 1]
if c2 < c1:
c1, c2 = c2, c1
tmp[c1 + c2] = tmp.get(c1 + c2, 0) + 1
flag = False
flag_odd = 0
for x in tmp:
if tmp[x] % 2 == 1:
flag_odd += 1
if length % 2 == 0:
print("NO")
flag = True
break
elif flag_odd > 1 or x[0] != x[1]:
print("NO")
flag = True
break
if flag:
continue
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
You have two strings $s_1$ and $s_2$ of length $n$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times:
Choose a positive integer $1 \leq k \leq n$.
Swap the prefix of the string $s_1$ and the suffix of the string $s_2$ of length $k$.
Is it possible to make these two strings equal by doing described operations?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then the test cases follow.
Each test case consists of three lines.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the length of the strings $s_1$ and $s_2$.
The second line contains the string $s_1$ of length $n$, consisting of lowercase English letters.
The third line contains the string $s_2$ of length $n$, consisting of lowercase English letters.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise.
-----Examples-----
Input
7
3
cbc
aba
5
abcaa
cbabb
5
abcaa
cbabz
1
a
a
1
a
b
6
abadaa
adaaba
8
abcabdaa
adabcaba
Output
YES
YES
NO
YES
NO
NO
YES
-----Note-----
In the first test case:
Initially $s_1 = \mathtt{cbc}$, $s_2 = \mathtt{aba}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{abc}$, $s_2 = \mathtt{abc}$.
In the second test case:
Initially $s_1 = \mathtt{abcaa}$, $s_2 = \mathtt{cbabb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{bbcaa}$, $s_2 = \mathtt{cbaab}$.
Operation with $k = 3$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbbc}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{cabaa}$, $s_2 = \mathtt{cbbba}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{babaa}$, $s_2 = \mathtt{cbbca}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbcb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{cbbaa}$, $s_2 = \mathtt{cbbaa}$.
In the third test case, it's impossible to make strings equal. | def putin():
return map(int, input().split())
def solve():
n = int(input())
s = input()
r = input()
pairs = []
for i in range(len(s)):
pairs.append(tuple(sorted([s[i], r[n - 1 - i]])))
D = {}
for elem in pairs:
if elem in D:
D[elem] += 1
else:
D[elem] = 1
odd = 0
for elem in D:
if D[elem] % 2 == 1:
if elem[0] != elem[1]:
return "NO"
odd += 1
return "YES" if odd < 2 else "NO"
t = int(input())
for i in range(t):
print(solve()) | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING VAR NUMBER RETURN VAR NUMBER STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You have two strings $s_1$ and $s_2$ of length $n$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times:
Choose a positive integer $1 \leq k \leq n$.
Swap the prefix of the string $s_1$ and the suffix of the string $s_2$ of length $k$.
Is it possible to make these two strings equal by doing described operations?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then the test cases follow.
Each test case consists of three lines.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the length of the strings $s_1$ and $s_2$.
The second line contains the string $s_1$ of length $n$, consisting of lowercase English letters.
The third line contains the string $s_2$ of length $n$, consisting of lowercase English letters.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise.
-----Examples-----
Input
7
3
cbc
aba
5
abcaa
cbabb
5
abcaa
cbabz
1
a
a
1
a
b
6
abadaa
adaaba
8
abcabdaa
adabcaba
Output
YES
YES
NO
YES
NO
NO
YES
-----Note-----
In the first test case:
Initially $s_1 = \mathtt{cbc}$, $s_2 = \mathtt{aba}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{abc}$, $s_2 = \mathtt{abc}$.
In the second test case:
Initially $s_1 = \mathtt{abcaa}$, $s_2 = \mathtt{cbabb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{bbcaa}$, $s_2 = \mathtt{cbaab}$.
Operation with $k = 3$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbbc}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{cabaa}$, $s_2 = \mathtt{cbbba}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{babaa}$, $s_2 = \mathtt{cbbca}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbcb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{cbbaa}$, $s_2 = \mathtt{cbbaa}$.
In the third test case, it's impossible to make strings equal. | import sys
t = int(input())
for _ in range(t):
n = int(input())
s1 = input()
s2 = input()
if s1 == s2:
print("YES")
continue
s2 = s2[::-1]
d = {}
for i in range(n):
temp = max(ord(s1[i]), ord(s2[i])), min(ord(s1[i]), ord(s2[i]))
if temp not in d.keys():
d[temp] = 1
else:
d[temp] += 1
if n % 2 == 0:
stop = 0
for j in d.keys():
if d[j] % 2 == 1:
stop = 1
break
if stop == 1:
print("NO")
else:
print("YES")
else:
odd = 0
stop = 0
for j in d.keys():
if d[j] % 2 == 1:
odd += 1
if j[0] != j[1] or odd > 1:
stop = 1
break
if stop == 1:
print("NO")
else:
print("YES") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You have two strings $s_1$ and $s_2$ of length $n$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times:
Choose a positive integer $1 \leq k \leq n$.
Swap the prefix of the string $s_1$ and the suffix of the string $s_2$ of length $k$.
Is it possible to make these two strings equal by doing described operations?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then the test cases follow.
Each test case consists of three lines.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the length of the strings $s_1$ and $s_2$.
The second line contains the string $s_1$ of length $n$, consisting of lowercase English letters.
The third line contains the string $s_2$ of length $n$, consisting of lowercase English letters.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise.
-----Examples-----
Input
7
3
cbc
aba
5
abcaa
cbabb
5
abcaa
cbabz
1
a
a
1
a
b
6
abadaa
adaaba
8
abcabdaa
adabcaba
Output
YES
YES
NO
YES
NO
NO
YES
-----Note-----
In the first test case:
Initially $s_1 = \mathtt{cbc}$, $s_2 = \mathtt{aba}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{abc}$, $s_2 = \mathtt{abc}$.
In the second test case:
Initially $s_1 = \mathtt{abcaa}$, $s_2 = \mathtt{cbabb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{bbcaa}$, $s_2 = \mathtt{cbaab}$.
Operation with $k = 3$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbbc}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{cabaa}$, $s_2 = \mathtt{cbbba}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{babaa}$, $s_2 = \mathtt{cbbca}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbcb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{cbbaa}$, $s_2 = \mathtt{cbbaa}$.
In the third test case, it's impossible to make strings equal. | import sys
input = sys.stdin.readline
def solve():
n = int(input())
s1 = input().strip()
s2 = input().strip()
cnt = {}
for i in range(n):
x, y = ord(s1[i]), ord(s2[n - 1 - i])
x, y = min(x, y), max(x, y)
if (x, y) not in cnt:
cnt[x, y] = 0
cnt[x, y] += 1
if all([(cnt[key] % 2 == 0) for key in cnt]):
return "YES"
else:
un = 0
same = False
for key in cnt:
if cnt[key] % 2:
un += 1
same = key[0] == key[1]
return "YES" if un == 1 and same else "NO"
for _ in range(int(input())):
print(solve()) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You have two strings $s_1$ and $s_2$ of length $n$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times:
Choose a positive integer $1 \leq k \leq n$.
Swap the prefix of the string $s_1$ and the suffix of the string $s_2$ of length $k$.
Is it possible to make these two strings equal by doing described operations?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then the test cases follow.
Each test case consists of three lines.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the length of the strings $s_1$ and $s_2$.
The second line contains the string $s_1$ of length $n$, consisting of lowercase English letters.
The third line contains the string $s_2$ of length $n$, consisting of lowercase English letters.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise.
-----Examples-----
Input
7
3
cbc
aba
5
abcaa
cbabb
5
abcaa
cbabz
1
a
a
1
a
b
6
abadaa
adaaba
8
abcabdaa
adabcaba
Output
YES
YES
NO
YES
NO
NO
YES
-----Note-----
In the first test case:
Initially $s_1 = \mathtt{cbc}$, $s_2 = \mathtt{aba}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{abc}$, $s_2 = \mathtt{abc}$.
In the second test case:
Initially $s_1 = \mathtt{abcaa}$, $s_2 = \mathtt{cbabb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{bbcaa}$, $s_2 = \mathtt{cbaab}$.
Operation with $k = 3$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbbc}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{cabaa}$, $s_2 = \mathtt{cbbba}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{babaa}$, $s_2 = \mathtt{cbbca}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbcb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{cbbaa}$, $s_2 = \mathtt{cbbaa}$.
In the third test case, it's impossible to make strings equal. | tc = int(input())
for i in range(tc):
all_pairs = {}
s = int(input())
s1 = input()
s2 = input()
for i in range(s):
k1 = s1[i], s2[-1 - i]
k2 = s2[-1 - i], s1[i]
if k1 in all_pairs:
all_pairs[k1] += 1
else:
all_pairs[k1] = 1
if k2 in all_pairs and k2 != k1:
all_pairs[k2] += 1
elif k2 != k1:
all_pairs[k2] = 1
works = True
flag = True
for key in all_pairs:
if all_pairs[key] % 2 == 1:
if key[0] == key[1] and flag:
flag = False
else:
works = False
break
if works:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You have two strings $s_1$ and $s_2$ of length $n$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times:
Choose a positive integer $1 \leq k \leq n$.
Swap the prefix of the string $s_1$ and the suffix of the string $s_2$ of length $k$.
Is it possible to make these two strings equal by doing described operations?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Then the test cases follow.
Each test case consists of three lines.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) β the length of the strings $s_1$ and $s_2$.
The second line contains the string $s_1$ of length $n$, consisting of lowercase English letters.
The third line contains the string $s_2$ of length $n$, consisting of lowercase English letters.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise.
-----Examples-----
Input
7
3
cbc
aba
5
abcaa
cbabb
5
abcaa
cbabz
1
a
a
1
a
b
6
abadaa
adaaba
8
abcabdaa
adabcaba
Output
YES
YES
NO
YES
NO
NO
YES
-----Note-----
In the first test case:
Initially $s_1 = \mathtt{cbc}$, $s_2 = \mathtt{aba}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{abc}$, $s_2 = \mathtt{abc}$.
In the second test case:
Initially $s_1 = \mathtt{abcaa}$, $s_2 = \mathtt{cbabb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{bbcaa}$, $s_2 = \mathtt{cbaab}$.
Operation with $k = 3$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbbc}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{cabaa}$, $s_2 = \mathtt{cbbba}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{babaa}$, $s_2 = \mathtt{cbbca}$.
Operation with $k = 1$, after the operation $s_1 = \mathtt{aabaa}$, $s_2 = \mathtt{cbbcb}$.
Operation with $k = 2$, after the operation $s_1 = \mathtt{cbbaa}$, $s_2 = \mathtt{cbbaa}$.
In the third test case, it's impossible to make strings equal. | import builtins
tests = int(input())
for it in range(tests):
n = int(input())
s1 = input()
s2 = input()
d = dict()
for i in range(n):
l = min(s1[i], s2[n - i - 1]), max(s1[i], s2[n - i - 1])
if l not in d:
d[l] = 0
d[l] += 1
cnt = 0
cnt2 = 0
for i in d:
if i[0] == i[1] and d[i] % 2 == 1:
cnt += 1
if i[0] != i[1] and d[i] % 2 == 1:
cnt2 = 1
if cnt2 > 0:
print("NO")
continue
if n % 2 == 0:
if cnt > 0:
print("NO")
continue
print("YES")
continue
if cnt != 1:
print("NO")
continue
print("YES") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Chef has $N$ boxes arranged in a line.Each box has some candies in it,say $C[i]$. Chef wants to distribute all the candies between of his friends: $A$ and $B$, in the following way:
$A$ starts eating candies kept in the leftmost box(box at $1$st place) and $B$ starts eating candies kept in the rightmost box(box at $N$th place). $A$ eats candies $X$ times faster than $B$ i.e. $A$ eats $X$ candies when $B$ eats only $1$. Candies in each box can be eaten by only the person who reaches that box first. If both reach a box at the same time, then the one who has eaten from more number of BOXES till now will eat the candies in that box. If both have eaten from the same number of boxes till now ,then $A$ will eat the candies in that box.
Find the number of boxes finished by both $A$ and $B$.
NOTE:- We assume that it does not take any time to switch from one box to another.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains three lines of input.
The first line of each test case contains $N$, the number of boxes.
- The second line of each test case contains a sequence,$C_1$ ,$C_2$ ,$C_3$ . . . $C_N$ where $C_i$ denotes the number of candies in the i-th box.
- The third line of each test case contains an integer $X$ .
-----Output:-----
For each testcase, print two space separated integers in a new line - the number of boxes eaten by $A$ and $B$ respectively.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq N \leq 200000$
- $1 \leq C_i \leq 1000000$
- $1 \leq X \leq 10$
- Sum of $N$ over all test cases does not exceed $300000$
-----Subtasks-----
Subtask 1(24 points):
- $1 \leq T \leq 10$
- $1 \leq N \leq 1000$
- $1 \leq C_i \leq 1000$
- $1 \leq X \leq 10$
Subtask 2(51 points): original constraints
-----Sample Input:-----
1
5
2 8 8 2 9
2
-----Sample Output:-----
4 1
-----EXPLANATION:-----
$A$ will start eating candies in the 1st box(leftmost box having 2 candies) . $B$ will start eating candies in the 5th box(rightmost box having 9 candies) .As $A's$ speed is 2 times as that of $B's$, $A$ will start eating candies in the 2nd box while $B$ would still be eating candies in the 5th box.$A$ will now start eating candies from the 3rd box while $B$ would still be eating candies in the 5th box.Now both $A$ and $B$ will finish candies in their respective boxes at the same time($A$ from the 3rd box and $B$ from 5th box). Since $A$ has eaten more number of boxes($A$ has eaten 3 boxes while $B$ has eaten 1 box) till now,so $A$ will be eating candies in the 4th box.
Hence $A$ has eaten 4 boxes and $B$ has eaten 1 box. | for _ in range(int(input())):
number = int(input())
inA = list(map(int, input().split()))
X = int(input())
sum = 0
for i in range(number):
sum += inA[i]
leftSum = inA[0]
rightSum = sum - leftSum
lAns = 1
rAns = number - 1
for i in range(1, number):
if rightSum - inA[i] == leftSum / X:
if lAns >= rAns - 1:
lAns += 1
rAns -= 1
break
else:
break
elif rightSum - inA[i] < leftSum / X:
break
else:
lAns += 1
rAns -= 1
leftSum += inA[i]
rightSum -= inA[i]
print(lAns, rAns) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Chef has $N$ boxes arranged in a line.Each box has some candies in it,say $C[i]$. Chef wants to distribute all the candies between of his friends: $A$ and $B$, in the following way:
$A$ starts eating candies kept in the leftmost box(box at $1$st place) and $B$ starts eating candies kept in the rightmost box(box at $N$th place). $A$ eats candies $X$ times faster than $B$ i.e. $A$ eats $X$ candies when $B$ eats only $1$. Candies in each box can be eaten by only the person who reaches that box first. If both reach a box at the same time, then the one who has eaten from more number of BOXES till now will eat the candies in that box. If both have eaten from the same number of boxes till now ,then $A$ will eat the candies in that box.
Find the number of boxes finished by both $A$ and $B$.
NOTE:- We assume that it does not take any time to switch from one box to another.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains three lines of input.
The first line of each test case contains $N$, the number of boxes.
- The second line of each test case contains a sequence,$C_1$ ,$C_2$ ,$C_3$ . . . $C_N$ where $C_i$ denotes the number of candies in the i-th box.
- The third line of each test case contains an integer $X$ .
-----Output:-----
For each testcase, print two space separated integers in a new line - the number of boxes eaten by $A$ and $B$ respectively.
-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq N \leq 200000$
- $1 \leq C_i \leq 1000000$
- $1 \leq X \leq 10$
- Sum of $N$ over all test cases does not exceed $300000$
-----Subtasks-----
Subtask 1(24 points):
- $1 \leq T \leq 10$
- $1 \leq N \leq 1000$
- $1 \leq C_i \leq 1000$
- $1 \leq X \leq 10$
Subtask 2(51 points): original constraints
-----Sample Input:-----
1
5
2 8 8 2 9
2
-----Sample Output:-----
4 1
-----EXPLANATION:-----
$A$ will start eating candies in the 1st box(leftmost box having 2 candies) . $B$ will start eating candies in the 5th box(rightmost box having 9 candies) .As $A's$ speed is 2 times as that of $B's$, $A$ will start eating candies in the 2nd box while $B$ would still be eating candies in the 5th box.$A$ will now start eating candies from the 3rd box while $B$ would still be eating candies in the 5th box.Now both $A$ and $B$ will finish candies in their respective boxes at the same time($A$ from the 3rd box and $B$ from 5th box). Since $A$ has eaten more number of boxes($A$ has eaten 3 boxes while $B$ has eaten 1 box) till now,so $A$ will be eating candies in the 4th box.
Hence $A$ has eaten 4 boxes and $B$ has eaten 1 box. | for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
rsm, lsm = 0, 0
x = int(input())
k = 0
a = [(0) for i in range(n)]
b = [(0) for i in range(n)]
a[0] = c[0]
b[n - 1] = c[n - 1]
for i in range(1, n):
a[i] = c[i] + a[i - 1]
b[n - 1 - i] = b[n - i] + c[n - 1 - i]
i, j = 0, n - 1
for i in range(n):
b[i] = b[i] * x
i, j = 0, n - 1
while i != j:
k = i
while a[i] < b[j]:
i += 1
if i == j:
i -= 1
break
if a[i] == b[j] and i + 1 >= n - j and i + 1 < j:
i += 1
k = i
if i == j:
break
j -= 1
k += 1
if n == 1:
k = 1
print(k, n - k) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR |
Subsets and Splits