Add Odd or Subtract Even (Codeforces 1311A)
Tags: Greedy, implementation

You are given two positive integers $$$a$$$ and $$$b$$$.In one move, you can change $$$a$$$ in the following way:  Choose any positive odd integer $$$x$$$ ($$$x > 0$$$) and replace $$$a$$$ with $$$a+x$$$;  choose any positive even integer $$$y$$$ ($$$y > 0$$$) and replace $$$a$$$ with $$$a-y$$$. You can perform as many such operations as you want. You can choose the same numbers $$$x$$$ and $$$y$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Then $$$t$$$ test cases follow. Each test case is given as two space-separated integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).

Output: For each test case, print the answer — the minimum number of moves required to obtain $$$b$$$ from $$$a$$$ if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.

Note: In the first test case, you can just add $$$1$$$.In the second test case, you don't need to do anything.In the third test case, you can add $$$1$$$ two times.In the fourth test case, you can subtract $$$4$$$ and add $$$1$$$.In the fifth test case, you can just subtract $$$6$$$.

Sample Inputs:
5
2 3
10 10
2 4
7 4
9 3

Sample Outputs:
1
0
2
2
1

Solution (Python):
import sys
t = int(sys.stdin.readline().split()[0])
for _ in range(t):
    n = [int(i) for i in sys.stdin.readline().split()]
    (a, b) = (n[0], n[1])
    if a == b:
        print(0)
    elif a > b:
        if (a - b) % 2 == 0:
            print(1)
        else:
            print(2)
    elif a < b:
        if (b - a) % 2 == 0:
            print(2)
        else:
            print(1)

Solution (Python):
def main():
    ans = []
    t = int(input())
    for _ in range(t):
        (a, b) = map(int, input().split())
        x = b - a
        if x > 0:
            if x % 2 != 0:
                ans.append(1)
            else:
                ans.append(2)
        elif x < 0:
            if x % 2 == 0:
                ans.append(1)
            else:
                ans.append(2)
        else:
            ans.append(0)
    for i in ans:
        print(i)
if __name__ == '__main__':
    main()

Solution (Python):
def main() -> None:
    for tc in range(ii()):
        (a, b) = mi()
        diff = abs(b - a)
        if a == b:
            p(0)
        elif b > a:
            if diff % 2 == 1:
                p(1)
            else:
                p(2)
        elif diff % 2 == 1:
            p(2)
        else:
            p(1)
if __name__ == '__main__':
    master_flag = False
    L = lambda string: exec('from hq import L\nL(string)') if not master_flag else False
    LT = lambda tc, custom, string: exec('from hq import LT\nLT(tc, custom, string)') if not master_flag else False
    import os, sys, math, itertools
    from collections import deque, defaultdict, OrderedDict, Counter
    from bisect import bisect, bisect_left, bisect_right, insort
    from heapq import heapify, heappush, heappop, nsmallest, nlargest, heapreplace, heappushpop
    (ii, si) = (lambda : int(input()), lambda : input())
    (mi, msi) = (lambda : map(int, input().strip().split(' ')), lambda : map(str, input().strip().split(' ')))
    (li, lsi) = (lambda : list(mi()), lambda : list(msi()))
    (out, export, p, pp) = ([], lambda : print('\n'.join(map(str, out))), lambda x: out.append(x), lambda array: p(' '.join(map(str, array))))
    try:
        exec('from hq import see,info,cmdIO,_generator_\nline=[cmdIO(),main(),export(),_generator_()]\nfor l in line:l')
    except (FileNotFoundError, ModuleNotFoundError):
        master_flag = True
        main()
        export()