GDOG - Editorial

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

What is wrong in my code? It is giving RE(NZEC)

def func(n,k):
if k==1:
return 0
else:
return max(n%k,func(n,k-1))
for i in range(int(input())):
n,k=map(int,input().split())
print(func(n,k))

Simple java solution :smile:

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{

	Scanner sc=new Scanner(System.in);
   try{ int t=sc.nextInt();
    for(int k=0;k<t;k++)
    {
        int n=sc.nextInt();
        int m=sc.nextInt();
        int x=0;
        for(int i=1;i<=m;i++)
        {
            x=Math.max(x,n%i);
        }
        System.out.println(x);
    }
}catch(Exception e){};
}

}

why u are taking y==0 ,it’s start from 1 that is given in question,and you did not understand the question ,read again.

Try this

#include<bits/stdc++.h>
using namespace std;
int main() {
int T;
cin>>T;
while(T–) {
int N,K;
cin>>N>>K;
int count=0;
if(N==K) {
cout<<count<<"\n";
}
else if(N<K) {
cout<<N<<"\n";
}
else {
for(int i=1;i<=K;i++) {
count=N%i>count?N%i:count;
}
cout<<count<<"\n";
}
}
}

look at my code…

#include
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int rem,max=0;

    for(int i=1;i<=k;i++)
    {
        rem=n%i;
        if(rem>max)
        {
            max=rem;
        }
    }
    cout<<max<<endl;
}
return 0;

}