A. Bigger, Stronger
for a sequence to be strictly increasing, no two elements can be equal. Therefore, if there are any duplicate elements in the array, it is impossible to rearrange the array to make it strictly increasing.
Use a set to keep track of unique elements while iterating through the array. If, at any point during the iteration, an element is found to already exist in the set, it means there is a duplicate, and the answer is "NO." If the iteration completes without finding any duplicates, the answer is "YES."
t = int(input())
for _ in range(t):
nums = list(map(int,input().split()))
seen = set()
possible = True
for i in nums:
if i in seen:
possible = False
seen.add(i)
if possible:
print("YES")
else:
print("NO")
B. Application
Consider using a hashmap to store the count of change requests made by a given username.
We can use a hashmap to store the usernames along with their change counts. It also makes it easier to check if a username exists in the database, and to access the count of changes made for a person.
If a person is not registered we store $$$0$$$ as a value for the username. Afterwards we increment this value if a change request is made again with this username.
n = int(input())
database = {}
for _ in range(n):
username = input()
if username in database:
database[username] += 1
print(username + str(database[username]))
else:
database[username] = 0
print('OK')
C. Under Attack!!!
Let's denote the position of the bishop as $$$(i, j)$$$. The cells that the bishop can attack lie along diagonals that either ascend from left to right or descend from right to left. In the case of the left-to-right diagonal, the sum of the column numbers for all cells $$$(i, j)$$$ on the same diagonal is constant $$$( i + j = c )$$$ for some constant $$$c$$$.
For the right-to-left diagonal, the difference of the coordinates remains constant $$$(i - j = c)$$$ for some constant $$$c$$$.
We can store the values of the left-to-right diagonal sums and right-to-left diagonal sums and later calculate the total sum for each cell based on these precomputed values.
from collections import defaultdict
T = int(input())
for _ in range(T):
n,m = map(int,input().split())
mat = [list(map(int,input().split())) for i in range(n)]
front_diagonal = defaultdict(int)
back_diagonal = defaultdict(int)
for r, row in enumerate(mat):
for c, val in enumerate(row):
front_diagonal[r + c] += val
back_diagonal[r - c] += val
ans = 0
for r in range(n):
for c in range(m):
cur_total = front_diagonal[r + c] + back_diagonal[r - c] - mat[r][c]
ans = max(ans,cur_total)
print(ans)
D. Quadruplet Matrix
Let's rotate the grid by 0∘, 90∘, 180∘, and 270∘, and mark all cells that map to each other under these rotations. For example, for 4×4 and 5×5 grids, the grid must have the following patterns, the same letters denoting equal values:
In general, we can rotate the grid by 0∘, 90∘, 180∘, and 270∘ and see which cells need to have equal values by seeing the positions which each cell maps to.
Now to solve the problem, we consider each equal value (each of the letters a, b, c, ... in the above figures) independently, and consider the minimum number of moves to make them all 0 or all 1. The answer is the total across all values.
The time complexity is O($$$ n^2 $$$). And the space complexity is O($$$ 1 $$$).
Let's consider the upper triangular quarter of the grid. If we are at $$$(i, j)$$$, after rotating it by 0∘, 90∘, 180∘, and 270∘, the bit at this position is replaced by the one at $$$(j, n - i - 1)$$$, $$$(n - i - 1, n - j - 1)$$$, $$$(n - j - 1, i)$$$ respectively. From these four positions we count the numbers of zeros and ones and take the minimum. We sum those minimums for all positions in the upper triangular quarter of the grid.
t = int(input())
for _ in range(t):
n = int(input())
grid = []
for i in range(n):
grid.append(input())
min_ops = 0
for row in range(n//2):
for col in range(row, n - row - 1):
zero_one_count = [0, 0]
zero_one_count[int(grid[row][col])] += 1
zero_one_count[int(grid[col][n - row - 1])] += 1
zero_one_count[int(grid[n - row - 1][n - col - 1])] += 1
zero_one_count[int(grid[n - col - 1][row])] += 1
min_ops += min(zero_one_count)
print(min_ops)