BOWLERS - Editorial

Simple C++ solution
short and clear video:

what is the problem in my code

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t; cin>>t;
while(t–)
{
int n,k,l; cin>>n>>k>>l; int ans; ans=-1;
if(k==1&&l!=1&&n!=1)
{
cout<<ans<<endl;
break;
}
int arr[k+1];int i;
for(int i=1;i<k+1;i++)
arr[i]=l;
if(n>k*l)
cout<<ans<<endl;
else
{
i=1;
while(n–)
{
cout<<i<<" ";
i++;
if(i>k)
i=1;
}
cout<<endl;
}
}
return 0;
}

@rahuldugar please help

My submission

Can someone help me with this solution. I didn’t understand why is this failing.
thanks in advance :slight_smile:
I didn’t kept an order, just kept track of who did the last over and find any bowler who has thrown overs less than L and is not the previous bowler and print it.
Why is this approach wrong

Scanner scan = new Scanner(System.in);
		int testCases = scan.nextInt();
		int n, k, l;
		while (testCases > 0) {
			n = scan.nextInt();
			k = scan.nextInt();
			l = scan.nextInt();
			if (n > k * l || (k == 1 && n < 1)) {
				System.out.println(-1);
			} else {
				for (int i = 0; i < n; i++) {
					System.out.print((i % k) + 1+" ");
				}
				System.out.println();
			}
			testCases--;
		}
		scan.close();

Click here for submission link.

I am actually new to codechef contests. I did participate in September cook off contest and for the bowler’s problem I gave this solution. I got it to be wrong. I knew that I have tried the edge case, but still I got it wrong. I didn’t feel myself to try again with another question, and went to sleep.

And yesterday my rating before the contest was 0 and now it is 1452. I got it to be wrong and also got some ratings.

Can someone tell me what is wrong with my solution and how does ratings work?

In my original solution I was using a function that returned a StringBuffer variable as a result and wasn’t checking for the boundary case when K == 1 and N > 1. After going through the editorial, I’ve tried testing the solution as it’s explained and the code still fails. Could anyone please tell me why?

Thanks!

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        sc.nextLine();
        while (T-- > 0) {
            String[] parts = sc.nextLine().split(" ");
            int N = Integer.parseInt(parts[0]);
            int K = Integer.parseInt(parts[1]);
            int L = Integer.parseInt(parts[2]);
            
            if(K*L < N) {
                System.out.println(-1);
            } else if(K==1&&N>1) {
                System.out.println(-1);
            } else {
                int rep = 0;
                for(int i=0; i<N; i++) {
                    if(i == K) rep = 0;
                    System.out.print(++rep + " ");
                }
                System.out.println();
            }
        }
	}
}

Can you please tell for which possible test case it won’t work ??

@krishna_kc it’s still not working

@ayukismis `

your code with a small change and it worked

`

@krishna_kc thanks bro I got it why it wasn’t working bcoz I had put ios_base::sync_with_stdio(false); cin.tie(NULL); inside my test case loop.

1 Like

Can someone check my request above

I am totally upset with this…
Just forgot this simple test case if k == 1 and n != 1, then return -1;
Because of this i coudn’t submit :disappointed_relieved:

Wrong on testcase
1
15 5 3
Your way of assignment gives consecutive pair of numbers alternatively to avoid repitition of same bowler, on above testcase it will result in an assignment:
1 2 1 2 1 2 3 4 3 4 3 4 till 12 overs and afterwards will be forced to give the last 3 overs to 5th bowler again and again since there is no 6th bowler to complete the pair and avoid repitition. Hence approach is wrong…

Hope it helps :upside_down_face: :slightly_smiling_face:

1 Like

can anyone tell me why i’m getting runtime error in this code:-
#include<bits/stdc++.h>
using namespace std;

#define in unsigned long long int

int main(){
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
// freopen(“output.txt”, “w”, stdout);
#endif
in T;
cin>>T;
while(T–){
in n,k,l;
cin>>n>>k>>l;
if(kl < n ||(l == 1 && n != 1)) cout<<“-1”<<endl;
else{
if(n == 0){cout<<endl; continue;}
in loop = (k
l)/n;
in rmd = n-(loop*k);
for(in i=0;i<10;i++){
for(in j=1;j<=5;j++){
cout<<j<<" “;
}
}
in player = 1;
while(rmd–){
cout<<player<<” ";
player++;
}
cout<<endl;
}
}

return 0;

}

I do a little debugging and i get to know that because of multiply sign in these two lines i’m getting runtime error:-
in loop = (kl)/n;
in rmd = n-(loop
k);

so please anyone tell me what’s wrong with these two lines plz??
here’s my submission:- CodeChef: Practical coding for everyone

same bowler is doing consecutive overs which is wrong

That’s what i said :confused:

https://www.codechef.com/viewsolution/38143679

can anyone tell me reason of getting error.

if(i == K) rep = 0; resets rep only once

Ah, stupid mistake!
Thanks

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTRUN ios_base::sync_with_stdio(NULL); cin.tie(NULL);
int main()
{int t;
cin>>t;
while(t–)
{ FASTRUN ;
int n,k,l;
cin>>n>>k>>l;
if(k*l<n)
cout<<"-1";
else
{
if(k==1&&n>1)
cout<<"-1 “;
else
{int p=0;
for(int i=0;i<n;i++)
{if(p==k)
p=0;
cout<<++p<<” ";}

    }
}
cout<<"\n";
}

}