GDOG - Editorial

Not working why???

import java.util.Scanner;
class puppy{
public static void main(String args[]){
Scanner ashok=new Scanner(System.in);
int test=ashok.nextInt();
int i;
for(i=1;i<=test;i++){
int coins=ashok.nextInt();
int people=ashok.nextInt();
int j=1,x=1,count=1;
while(j>0){
count=peoplex;
if(count>coins){
System.out.println(coins-(people
(x-1)));
break;
}
//if(count==coins){
//System.out.println(“0”);
//break;
//}
x++;
}
}
}
}

/*

  • 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);
    

    }
    }

#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int r=0;
while(k)
{
if((n-(n/k)*k)>r)
{
r=n-(n/k)*k;
}
else
break;
k–;
}
cout<<r<<endl;
}
}

#include
using namespace std;
int main()
{ long t,n,k,ans,i,a;
cin>>t;
while(t–)
{ cin>>n>>k; ans=0;
for(i=2;i<=k;i++)
{ a=n%i;
if(ans<a)
ans=a;
}
cout<<ans<<"\n";

  }

return 0;
}

What is wrong with this code?
#include
using namespace std;

int main() {
int x,y,t;
cin>>t;
while(t>0){
cin>>x>>y;
if(y==0)
cout<<x;
else
cout<<x%y<<endl;
t–;
}
return 0;
}

What is wrong with this code?

#include <iostream>
 using namespace std;

 int main() {
 int x,y,t;
 cin>>t;
    while(t>0){
     cin>>x>>y;
      if(y==0)
        cout<<x;
      else
     cout<<x%y<<endl;
     t--;
}
return 0;

}

@sprea27

Your code will fail in many cases, one of them is-
When N=200, K=20
Your code will give (N%K)=0,
Actual answer will be (100%17)=15

@sprea27
but that will not be maximum that people can have… maximum can be 20 so why 17?

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.

7 Likes

Why is my code wrong?

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;

    for(int i=0; i<t; i++)
    {
    int n,k;
    cin >> n >> k;
    int q,r;

    for(int j=1; j<=n+1; j++)
    {
        if(k>n/j)
        {
            q = (n/j)+1;
            r = n%q;
            break;
        }
    }
    cout << r << endl;
}
}

I also did N%K at first …
but actually…

K IS NOT the no. of people Tuzik called…

K IS MAX no. of people Tuzik CAN CALL .

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)

Moral: READ Questions CAREFULLY.

HAPPY CODING :slight_smile:

4 Likes

#include
using namespace std;
int main()
{ long n,k,m,r;
int t;
cin>>t;
while(t–)
{cin>>n>>k;
m=n/k;
r=n-(m*k);
cout<<r<<endl;

   }
 return 0;

}

WHAT WRONG IN MY CODE???

Nice! Can you give a short explanation on how it works? How did you land upon that equation?

@raviojha2105

1)When K>N, it will always return N.

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.

This solution is wrong. Try with N = 100 and K = 7. Your solution will give answer as 2, but the correct answer is 4 (which is 100%6).

The testdata has been updated to include this now.

1 Like

The testdata has been updated to include this now.

SAVAGE !!! LOL :slight_smile:

So simple n%k would not work and you have to find a number between 1 and k for which n%k is maxmimum.

t = int(input())
for i in range(t):
n,k = map(int,input().split())
data = []
for j in range(1,k+1):
data.append(n%j)
print(max(data))

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;
}

why am I getting partially solved though I have 100% for subtasks?
Can anyone please explain?
Here is my code

#include < iostream>
using namespace std;

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;
}