# MONKEYJUMP - Editorial

Author: Ram Agrawal
Tester: Yogesh Deolalkar
Editorialist: Prathamesh Sogale

MEDIUM

# PREREQUISITES:

Conditional Statement, Math

# PROBLEM:

The monkey is located on the numeric axis at the point with coordinate x_0.

Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate x with a distance d to the left moves the monkey to a point with a coordinate \$x\$−\$d\$, while jumping to the right moves him to a point with a coordinate x+d.

The monkey is very fond of positive integers, so for each integer i starting with 1 the following holds: exactly i minutes after the start he makes a jump with a distance of exactly i. So, in the first minutes he jumps by 1, then by 2, and so on.

The direction of a jump is determined as follows: if the point where the monkey was before the jump has an even coordinate, the monkey jumps to the left, otherwise he jumps to the right.

For example, if after 18 consecutive jumps he arrives at the point with a coordinate 7, he will jump by a distance of 19 to the right, since 7 is an odd number, and will end up at a point 7+19=26. Since 26 is an even number, the next jump the monkey will make to the left by a distance of 20, and it will move him to the point \$26\$−\$20\$=6.

Find exactly which point the monkey will be at after exactly n jumps.

# QUICK EXPLANATION:

Using Copy and pen when we try it for some sample test cases we will notice a paten in the code as follows.

1. when x is even
a.if(n%4)==0 monkey will move x distance.
b.if(n%4)==1 monkey will move x+1 distance.
c.if(n%4)==2 monkey will move x+n+1 distance.
d.if(n%4)==3 monkey will move x-n distance.
2. when x is odd
a.if(n%4)==0 monkey will move x distance.
b.if(n%4)==1 monkey will move x-1 distance.
c.if(n%4)==2 monkey will move x-n-1 distance.
d.if(n%4)==3 monkey will move x+n distance.

# SOLUTIONS:

Setter's Solution
``````T = int(input())
for i in range(T):
x , n = map(int,input().split())
res = 0
if(x%2==0):
if(n%4==0):
res=x
elif(n%4==1):
res=x-n
elif(n%4==2):
res=x+1
else:
res=x+(n+1)
else:
if(n%4==0):
res=x
elif(n%4==1):
res=x+n
elif(n%4==2):
res=x-1
else:
res=x-n-1
print(res)
``````
Editorialist's Solution
``````T = int(input())
for i in range(T):
x , n = map(int,input().split())
res = 0
if(x%2==0):
if(n%4==0):
res=x
elif(n%4==1):
res=x-n
elif(n%4==2):
res=x+1
else:
res=x+(n+1)
else:
if(n%4==0):
res=x
elif(n%4==1):
res=x+n
elif(n%4==2):
res=x-1
else:
res=x-n-1
print(res)
``````