My code is working in my PC, then why is it not working on the editor of Codechef. Please help
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
System.out.println(“Enter the number of test cases”);
int testcases = sc.nextInt();
sc.nextLine();
while(testcases > 0){
System.out.println("Enter the values of n and k, respectively");
String str = sc.nextLine();
String[] array = str.split(" ");
int n = Integer.parseInt(array[0]);
int k = Integer.parseInt(array[1]);
System.out.println("Enter the number of queries in consecutive days");
String strs = sc.nextLine();
String[] arrays = strs.split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++){
arr[i] = Integer.parseInt(arrays[i]);
}
int sum = 0, count = 0;
for (int i = 0; i<n; i++){
sum = sum + arr[i];
if (arr[i] < k){
if (sum < (i+1)*k){
count = i+1;
break;
}
}
}
if (count == 0) {
count = (sum/k)+1;
}
System.out.println(count);
testcases--;
}
}
can anybody tell me why I am failling subtask 1 but passing subtask 2.
Here is my solution in c++.
The Code is failing on subtask-1 !!! I am not being able to find the issue ???
``#include<bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long
int main()
{
FAST_IO;
long T;
cin >> T;
while(T--)
{
ll n,k,q,a=0,day;
cin >> n >> k;
bool ans = false;
// reading the input
for(ll i=0; i<n; i++)
{
cin >> q;
a = (q+a);
if((a-k) < 0)
{ ans = true;
day = (i+1);
break;
}
else
{
a = a-k;
}
}
if(!ans)
{
day = (n+ (a/k)+1);
}
cout << day << endl;
}
return 0;
}``
can you plz tell me why we have to use long long here
https://www.codechef.com/viewsolution/38992356
I solved the CodeChef: Practical coding for everyone problem, but I got an 80 only, I got RE(SIGFPE) error which generally comes when a number is divided by zero, which is not possible for my case. I tried to solves that error for an hour. Could someone please help me solve this?
For ease, I am putting my code here:
#include
using namespace std;
int main() {
// your code goes here
long long t;
cin>>t;
while(t–)
{
long long n,k;
cin>>n>>k;
long long acc = 0,q,i=0;
bool flag= false;
for(i=0;i<n;i++)
{
cin>>q;
acc += q;
acc -= k;
if(acc<0)
{
flag = true;
break;
}
}
i = (flag)?i+1:n+(acc/k)+1;
cout<<i<<endl;
}
return 0;
}
//NODEJS
process.stdin.resume();
process.stdin.setEncoding('utf8');
let read = '';
process.stdin.on('data' , function(chunk){read+=chunk});
process.stdin.on('end' , function(){
let input = read.split('\n');
let counter = 0;
let t = input[counter++];
for(let i = 0; i<t; i++){
let nk = input[counter++].split(' ').map(Number);
//console.log(nk);
let n = nk[0];
let k = nk[1];
let kNumbers = input[counter++].split(' ').map(Number);
let temp = 0;
for(let j = 0; j<kNumbers.length; j++){
temp+=kNumbers[j];
if(temp<k){
console.log(j+1);
}
temp-=k;
}
if(temp>=k){
temp = Math.floor(temp/k)+1;
console.log(temp + n);
}
}
});
I don’t know why the sub task 1 is failed with error WA. Can someone help me? Is there anything wrong with the above code?
1 Like
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
int k;
cin>> n>> k;
int array[n];
for(int i=0;i<n;i++)
{
cin>>array[i];
}
int a=0;
int b=0;
if(n==1)
{
cout<<array[0]-k+2;
cout<<’\n’;
}
for(int i=0;i<n;i++)
{
a=array[i]+b;
if(a<k)
{
cout<<i+1;
cout<<"\n";
}
else
{
b=a-k;
}
}
}
return 0;
}
// someone please help me to find what Im missing?
//please tell me whats the problem with folowing code.
#include
using namespace std;
int main()
{
int n,k,t;
cin>>t;;
for(int i=0;i<t;i++)
{
cin>>n>>k;
int arr[n];
int sum=0;
int free_day;
for(int i=0;i<n;i++)
{
cin>>arr[i];
sum+=arr[i];
}
if(n==1)
{
cout<<(arr[0]/k)+1;
}
else
{
free_day=(sum/k)+1;
cout<<free_day;
}
}
return 0;
}
#include <bits/stdc++.h>
#include
using namespace std;
int main() {
long long int t;
cin>>t;
while(t--){
long int n , k,sum=0;
cin>>n>>k;
vector <long long int> q(n);
for(long long int i=0; i<n ;i++){
cin>>q[i];
sum = sum+q[i];
}
cout<<(sum/k)+1<<endl;
}
return 0;
}
I am failing to clear sub task 1 . But I’m not able to debug my code. Please help
Abhijeet1233… I think you have a problem of division by zero. You need to check if the divisor is zero before executing division. I think I had the same problem as you and I solved it this way. Try it out.
Add an if(k != 0) before “i = (flag)?i+1:n+(acc/k)+1”. I think it will solve the division by zero problem. It solved for me. Now my code is failing subtasks 1 and I don’t know why.
I wrote this code for the Chef Easy Queries Problem in CPP (Contest Page | CodeChef). It is satisfying the sample cases as well as all the test cases except one (the first test case). I have tried nearly everything, but that case just does not budge.
My algorithm -
- Input the values of T,n,k.
- Then run a loop T times. Inside this run another loop n times. In the inside loop, ask for Query (Q) and increment the day (day) by 1. Add it to the leftover queries from days before (left). From this quantity subtract (k). If it becomes negative, then break the loop and output the value of day we have.
- If the whole loop runs without (left) becoming negative, it means no free days till now. So, we increment the (day) variable by the quotient of (left) upon (k) plus 1.
This is our answer if (left) does not become negative.
I can’t find anything wrong with this, yet I am not able to solve the first test case of this problem. Any help would be appreciated.
#include
using namespace std;
int main() {
long long int T;
cin>>T;
for(long long int i=0;i<T;i++){
long long int n,k,left=0,Q,day=0;
cin>>n>>k;
for(long long int j=0;j<n;j++){
day++;
cin>>Q;
left+=Q;
left-=k;
if(left<0){
cout<<day<<endl;
break;
}
}
if(left>=0){
day+=(left/k)+1;
cout<<day<<endl;
}
}
return 0;
}
On running this, I get the error RE(SIGFPE) but I am not dividing by 0. Only dividing once in the whole program, and that too by k, which is given in the problem to be positive.
I think the issue is with the break:
I think you can’t stop reading the inputs if the condition is satisfied. I believe you need to read all the inputs regardless of the condition t exit is satisfied or not.
1 Like
That worked perfectly! Thank you so much! I did not even fathom that could be a problem.
Hi,
Here you just have to find the first day when (a[i]<k).Here a[i] is included with the buffer.
So if you get the first day where a[i]<k,break the loop and print i+1.Else if a[i]>=k,then according to the question you answer only k and carry forward the remaining a[i]-k to next day a[i]+1.
Below is my code,I hope you will get it.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin>>t;
while(t–)
{
long long n,k;
cin>>n>>k;
long long a[n],s=0;
for(long long i=0;i<n;i++)
cin>>a[i];
for(long long i=0;i<n;i++)
s=s+a[i];
bool flag=0;
long long res;
for(long long i=0;i<n-1;i++)
{
if(a[i]<k)
{
res=i+1;
flag=1;
break;
}
else
{
a[i+1]+=(a[i]-k);
}
}
if(flag)
cout<<res<<endl;
else
cout<<(s/k)+1<<endl;
}
return 0;
}