Last Updated: 29 Oct, 2021

Teams

Easy

Problem statement

You hosted a great party and invited all of your friends. You have an infinite number of friends. Since you can’t remember all of their names, you have given each of them an ‘id’ numbered from ‘1’ to ‘∞’. You decided to play a game ‘T’ times. Each time you select an even number of friends having ‘id’ from the range ‘L’ to ‘R’. You play the game by dividing all of your friends into ‘(R - L + 1) / 2’ teams, i.e. every team consists of two friends, and every friend is a member of exactly one team. But there is a problem. Two friends will only form a team if the gcd of their ‘id’ is 1. I.e. gcd(i, j) must be 1 and ‘L’ <= i < j <= ‘R’.

For each game. If you can divide the friends into ‘(R - L + 1) / 2’ teams, print the resulting pairs,i.e. Pair of friend’s id who are forming a team, else print no solution exists. If there are multiple solutions, you can print any of them.

Note: The solution function returns a single value, and based on your answer, the output will either be 1 or 0.

Input Format:
The first line contains ‘T’, denoting the number of games.
For each Test :
    The first line contains two space-separated integers, ‘L’ and ‘R’, denoting the friend’s ids’ range that will play the game. 
Output Format:
For each test case.
If a solution exists, the following ‘(R - L + 1) / 2’ lines should contain two space-separated integers whose gcd is 1, denoting the friend’s ids that will form a team.
Constraints -
1<= ‘T’ <= 10
1 <= ‘L’ < ‘R’ <= 10 ^ 18.
2 <= ‘R - L + 1’ <= 10 ^ 5.
‘R - L + 1’ is always even.

Time Limit: 1 sec

Approaches

01 Approach

Approach: GCD of every two adjacent numbers is always 1. Say ‘n’ has a divisor ‘q’, for which n / q = p. If we divide ‘n + 1’ by ‘q’, we will get (n + 1) / q = (n / q) + (1 / q) = p + (1 / q), thus for all q ≠ 1, ‘n + 1’ will not be divisible by ‘q’. Hence, gcd(n, n + 1) = 1. So

we can always divide our friends to play the game. Every neighbouring friends will form a team.

Algorithm:

  • Print ‘1’.
  • Iterate using a for loop from i = ‘L’ to ‘R’ with an increment of 2.
    • Print ‘i’ and ‘i + 1’.