# Greedy Puppy | CodeChef Solution

Hello coders, today we are going to solve Greedy Puppy CodeChef Solution whose Problem Code is GDOG.

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 first line of the input contains an integerÂ TÂ denoting the number of test cases. Each of nextÂ TÂ lines contains 2 space-separated integers:Â NÂ andÂ K, for this test case.

## Output Format

For each test case output one integer – the maximum possible number of coins Tuzik can get.

## Constraints

• 1Â â‰¤Â TÂ â‰¤Â 50
• 1Â â‰¤Â N, KÂ â‰¤Â 105

Example

Sample Input

``````2
5 2
11 3``````

Sample Output

``````1
2``````

Explanation

In the first example he should call two people. Each of them will take 2 coins and they will leave 1 coin for Tuzik.

In theÂ second exampleÂ he should call 3 people.

## Solution – Greedy Puppy | CodeChef Solution

### C++

```#include <iostream>
using namespace std;

int main() {
int t,n,k,instanceMax,maxx=0;
cin>>t;
while(t--){
cin>>n>>k;
maxx=0;
for(int i=1;i<=k;i++){
instanceMax = n%i;
if(instanceMax > maxx){
maxx = instanceMax;
}
}
cout<<maxx<<endl;
}
return 0;
}
```

### Python

```#This code is given by-kiran_abc02
#import math
for _ in range(int(input())):
a,b=map(int,input().split())
s=0
for i in range(1,b+1):
s=max(s,a%i)
print(s)```

### Java

```import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int maxMod = -1;
int j = 1;
while(j <= k) { // go from 1 till k
if(n%j > maxMod) {
maxMod = n%j;
}
j++;
}
System.out.println(maxMod);
}
sc.close();
}
}
```

Disclaimer: The above Problem (Greedy Puppy) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.