You have N coins. You are about to call any number of people between 1 and K and divide the coins to them in such a way that each person gets the same amount of coins and this amount is maximal. You take all coins left after this process. Your task is to compute the maximal number of coins which you can get.
QUICK EXPLANATION:
For each possible number of people, compute the amount of coins which are left after the process. Finally, return the maximum from these values.
EXPLANATION:
Let assume that you decided to call exactly k people and you want to divide N coins to them in the way described in the statement. It means, that each person gets the same amount of coins and this amount is maximal from all possible ones. You can simulate this process by giving one coin to all people at once until you have less than k coins, because then you cannot divide them equally. The number of coins which each person has after the process equals N \mathbin{/} k, where \mathbin{/} is the integer division, i.e. 7 \mathbin{/} 3 = 2. Based on this fact, it is easy to see that there are r = N - k \cdot (N \mathbin{/} k) coins left after the process and this is the amount of coins which you get. Here r is called a remainder after dividing N by k and it is very common in computation. Most programming languages have built in operation for computing a reminder. It is often represented by \texttt{%} or \texttt{mod}.
Since K is not so big, we can iterate over all possible values of it and for each of them, compute the remainder of N \mathbin{/} k, where k is the current number of people, and at the end print the maximum from these remainders.
Time complexity:
We are iterating over K values and for each of them we do constant time operations, so the total complexity is O(K).
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here
Tester’s solution can be found here
import java.util.Scanner;
class GDOG
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for ( int i=0; i<t; i++ )
{
int n=in.nextInt();
int k=in.nextInt();
int res=n-k;
while ( res>=0 )
{
n=res;
res=n-k;
}
System.out.println(n);
}
}
}
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);
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.
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.