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 solve(V):
n = len(V)
# instead of turning, the ants can be seen to pass each other
# so they simply go around and around.
# in each direction go about half of them,
# meeting the other half 2 times a round, 2x hello in a meeting
# 1 round is 10_000 sec, 10**9 sec is 10**5 rounds
meets = (n//2) * (n-n//2) * 2 * 2 * 10**5
# now what about the last 6 sec? => must maximize meetings here
# 6 sec is 0.6 m per ant, that is ants can only meet if adjacent
V.sort()
for pos in range(n):
if (V[pos]-V[pos-1])%1000 > 1: break
run = 0
for i in range(pos, pos+n):
if (V[i%n]-V[(i-1)%n])%1000 == 1:
run += 1
else:
meets += (run+1)//2 * 2
run = 0
else:
if run: meets += (run+1)//2 * 2
return meets
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Ants
You are viewing a single comment's thread. Return to all comments →
python solution
def solve(V): n = len(V) # instead of turning, the ants can be seen to pass each other # so they simply go around and around. # in each direction go about half of them, # meeting the other half 2 times a round, 2x hello in a meeting # 1 round is 10_000 sec, 10**9 sec is 10**5 rounds meets = (n//2) * (n-n//2) * 2 * 2 * 10**5 # now what about the last 6 sec? => must maximize meetings here # 6 sec is 0.6 m per ant, that is ants can only meet if adjacent V.sort() for pos in range(n): if (V[pos]-V[pos-1])%1000 > 1: break run = 0 for i in range(pos, pos+n): if (V[i%n]-V[(i-1)%n])%1000 == 1: run += 1 else: meets += (run+1)//2 * 2 run = 0 else: if run: meets += (run+1)//2 * 2 return meets