We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def dynamicArray(n, queries):
# Initialize the array of n empty lists
arr = [[] for _ in range(n)]
# Initialize lastAnswer to 0
lastAnswer = 0
# List to store the results of type 2 queries
result = []
# Iterate through each query
for query in queries:
# Split the query into components
q, x, y = map(int, query.split())
# Determine the index using the given formula
idx = (x ^ lastAnswer) % n
if q == 1:
# Append y to arr[idx]
arr[idx].append(y)
elif q == 2:
# Find the element at position y % size(arr[idx]) in arr[idx]
value = arr[idx][y % len(arr[idx])]
# Assign this value to lastAnswer
lastAnswer = value
# Append the result to the result list
result.append(lastAnswer)
return result
Read the input values
first_line = input().strip().split()
n = int(first_line[0])
q = int(first_line[1])
queries = [input().strip() for _ in range(q)]
Call the function and get the result
output = dynamicArray(n, queries)
Print the results of type 2 queries
for res in output:
print(res)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Dynamic Array
You are viewing a single comment's thread. Return to all comments →
def dynamicArray(n, queries): # Initialize the array of n empty lists arr = [[] for _ in range(n)] # Initialize lastAnswer to 0 lastAnswer = 0 # List to store the results of type 2 queries result = []
Read the input values
first_line = input().strip().split() n = int(first_line[0]) q = int(first_line[1]) queries = [input().strip() for _ in range(q)]
Call the function and get the result
output = dynamicArray(n, queries)
Print the results of type 2 queries
for res in output: print(res)