To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor. /
package javaapplication60;
/
Author : Pujan
*/
import java.util.Scanner;
class stack
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int x,y;
x = in.nextInt();
y=in.nextInt();
int temp = x/y;
int temp1 = x%y;
System.out.println("Max coins which people will get: "+temp);
System.out.println("Coins which are left: "+temp1);
Okay after scratching my head for sometime I got what the problem is about. The problem statement is somewhat misleading. The value of k is the number of people around the dog. We have to iterate over k to check how many people he should call so that the remainder is maximum. So simple n%k would not work and you have to find a number between 1 and k for which n%k is maxmimum.
So, find out max no. of coins our puppy can have,keeping in mind… K, may or may not be the no. of people he actually called where min. value for K is 1(cz dogs can’t open a wooden chest!!.. LOL)
2)When K>(N/2) but K<=N, it will always return (N/2)+1.
3)When K<=(N/2), it’s a little tricky, but bear with me. Here it goes-
our main goal is to choose a number num (1<=num<=K),
so that (N%num) will be max for the given range of K. Now how do we do this?
We choose num such that num is the smallest number such that N/K = N/num.
We can start from k and instead going down to 1 we can go to whatever maximum mod m we are getting. Because going further down will only yield [0, m-1]
This will result in faster closing of loop.
Here’s my code:
#include <iostream>
using namespace std;
int main(){
int t, n, k, i, m;
cin >> t;
while(t-->0){
cin >> n >> k;
m = n%k;
for(i=k; i>m; --i){
if(n%i>m) m = n%i;
}
cout << m << endl;
}
return 0;
}
int main() {
int tests = 0;
cin>>tests;
while(tests–)
{
long long int N = 0, K = 0, res = 0;
cin>>N;
cin>>K;
res = 0;
while(K>0)
{
if(N%K > res)
{
res = N%K;
}
K–;
}
cout<<res<<endl;
}
return 0;
}