Greedy Puppy Solution

 Greedy Puppy

Tuzik is a little dog. But despite the fact he is still a puppy he already knows about the pretty things that coins are. He knows that for every coin he can get very tasty bone from his master. He believes that some day he will find a treasure and have loads of bones.

And finally he found something interesting. A wooden chest containing N coins! But as you should remember, Tuzik is just a little dog, and so he can't open it by himself. Actually, the only thing he can really do is barking. He can use his barking to attract nearby people and seek their help. He can set the loudness of his barking very precisely, and therefore you can assume that he can choose to call any number of people, from a minimum of 1, to a maximum of K.

When people come and open the chest they divide all the coins between them in such a way that everyone will get the same amount of coins and this amount is maximal possible. If some coins are not used they will leave it on the ground and Tuzik will take them after they go away. Since Tuzik is clearly not a fool, he understands that his profit depends on the number of people he will call. While Tuzik works on his barking, you have to find the maximum possible number of coins he can get.

Input Format

The input line  contains 2 space-separated integers: N and K

Output Format

Output one integer - the maximum possible number of coins Tuzik can get.

Constraints

  • 1 ≤ N, K ≤ 105

 

Sample Test Cases:

Input1:

5 2

Output1:

1

Explanation1:

He should call two people. Each of them will take 2 coins and they will leave 1 coin for Tuzik

Input2:

11 3

Output2:

2

Explanation2:

He should call 3 people and they will leave 2 coins for Tuzik

 

 

SAMPLE INPUT
 
5 2
SAMPLE OUTPUT
 
1




Explanation:

In Python, we don't declare the data type of a variable explicitly like we do in C. Instead, we can directly assign values to the variables. The map() function is used to read two integer values separated by a space, and the split() method splits the input at the space and returns a list of strings. The int() function is used to convert the strings to integers.

The for loop in Python uses the range() function to generate a sequence of numbers, and the third argument specifies the step value (in this case, -1 for counting down). The if condition and the assignment statement inside it are the same as in the C code. Finally, we print the value of coins using the print() function.

Program:


n, k = map(int, input().split())
coins = 0
for i in range(k, 0, -1):
if n % i > coins:
coins = n % i
print(coins)




Post a Comment

0 Comments