Decreasing Srrnmieeda CodeChef October CookOff Editorial and Python solution

Decreasing Srrnmieeda Editorial and Solutions

PROBLEM STATEMENT

YOU CAN FIND THE COMPLETE PROBLEM STATEMENT HERE :PROBLEM STATEMENT

You are given two integers L and R. Find the smallest non-negative integer N such that

Here, % is the modulo operator, so A%B is the remainder of A after division by B. For example, 11%3=2.

UNDERSTANDING THE PROBLEM:

So We are given two integers as the input L and R respectively, now our job is to find the minimum possible non-negative integer such that :
N%L>N%(L+1)>>N%(R1)>N%R.
So If We take any number we will notice that the largest modulo is given by number+(number-1) because after this we will get 2*number which will give the modulo as 0. If we now increase the number we will notice that the remainder starts repeating themselves.
So by this logic the value of r should not be greater than the number which gives the largest modulo which is 1 less than twice the number so:
if r is less than twice l then we will get the answer or else its impossible to get such an n as if its greater than n it would be increasing and then decrease after reaching the highest remainder.

Now in the same way we will get the minimum n only if the remainder is 0. because if n gives remainder 1 with the lower number it means that there is another value of n and as the function asks us to find the minimum value of n we can safely say that the minimum value of n will be the value of r

PYTHON 3 CODE:

Now that we know the logic behind the code lets implement it 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# cook your dish
testcases = int(input())
for i in range(testcases):
	a = input()
	num1,num2 = a.split(" ")
	num1,num2 = int(num1),int(num2)
	if num2<2*num1:
		print(num2)
	else:
		print(-1)

Comments