I always require some time to think about the end condition, but I bet it is fine as long as I understand how the algorithm works.
def can_i_square(a: int) -> bool:
# binary search
li: int = 1
ri: int = a
while li < ri - 1:
mi: int = (li + ri) // 2
sq: int = mi * mi
if sq == a:
return True
elif sq < a:
li = mi
else:
ri = mi
return li * li == a
for _ in range(int(input())):
_: int = int(input())
a: int = sum(map(int, input().split()))
if can_i_square(a):
print("YES")
else:
print("NO")
Only numbers with odd indicnes can be left in the result.
Trying to leave a number with an even index makes the lengths of the remaining two parts odd, meaning it's impossible.
def solve(n: int, a: list[int]) -> int:
odds: list[int] = []
for i in range(n):
if i % 2 == 1:
continue
odds.append(a[i])
return max(odds)
for _ in range(int(input())):
n: int = int(input())
a: list[int] = list(map(int, input().split()))
print(solve(n, a))
def possible(n: int, s: str) -> bool:
ones: int = len([c for c in s if c == "1"])
# two positions are required to perform one operation
if ones % 2 == 1:
return False
if ones == 2 and s.rfind("1") - s.find("1") == 1:
return False
# if there are more than two positions to turn on, it\'s always possible
# for i in range((num of "1") // 2):
# turn_on(i-th "1", (num of "1") // 2 + 1-th "1")
return True
for _ in range(int(input())):
n: int = int(input())
s: str = input()
if possible(n, s):
print("YES")
else:
print("NO")
Salad 600 Mexico rice 500 Protein shake 400 Protein bar 200
Total 1700 kcal
push-ups, pull-ups
MUST:
TODO: