LCM related problem in python


I try this so far:
(1st try : Wrong answer in test case 2 :frowning: )

    for _ in range(int(input())):
        l,r = map(int,input().split())
        if r%l==0 and l != 1:
            print(l,r,sep=" ")
        else:
            if r-l > l:
                c = (r-l)//2
                if l < 2:
                    print(2,c,sep=" ")
                else:
                    print(2*(l//2)+2,c,sep=" ")
            else:
                print(-1,-1,sep=" ")

(2nd try : Runtime error in test case 2 :roll_eyes: )

    import random
    import math
     
    def lcm(a, b):
        return abs(a*b) // math.gcd(a, b)
     
    def cool(x,y,l,r):
        if l <= x and l < y and x < r and y <= r:
            if l <= lcm(x,y) and lcm(x,y) <= r:
                return True
     
     
    for _ in range(int(input())):
        l,r = map(int,input().split())
        if r%l==0 and l != 1:
            print(l,r,sep=" ")
        else:
            if r - l > l:
                while (True):
                    x = random.randrange(l, r)
                    y = random.randrange(x+1, r)
                    if cool(x, y, l, r) == True:
                        print(x, y, sep=" ")
                        break
            else:
                print(-1, -1, sep=" ")

Here is the original link from codeforces.
Can you please share your way that you will use to solve this problem? :frowning_face:

Hi, what runtime error are you getting? I generally like your approach in the 2nd try, but I suspect you can probably pick your x and y better than randomly.

1 Like

My Runtime error is for 10000 test cases.Here is that,

After found this i am so depressed :sob:

for i in range(int(input())):
	a,b = map(int,input().split())
	c = a*2
	if b>=c:
		print(a,c)
	else:
		print(-1,-1)

What so wrong with me.Feeling like an alien :pleading_face:
Do you have any idea how can i improve my thinking skill?

Oh, I see what’s going on. I think this was more of a math challenge than a coding challenge. If you want to get good at solving problems like this, the trick is to solve lots and lots of problems like this.

I wouldn’t feel too bad about this problem. They made a clever solution, and I see this problem as more of a ‘gotcha’.

1 Like