CHEFNWRK - Editorial

Java solution : -

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) {
    int T = fs.nextInt();
    for (int t = 0; t < T; t++) {
        solve();
    }
    out.close();
}

private static void solve() {
    int n = fs.nextInt();
    int k = fs.nextInt();

    int[] arr = fs.readArray(n);
    int rounds = 0;
    for(int i = 0;i<n;i++) {
        if(arr[i]>k) {
            out.println(-1);
            return;
        }
        int sum = arr[i];
        int j = i+1;
        while (j<n && sum+arr[j] <= k) {
            sum += arr[j++];
        }
        i = j-1;
        rounds++;
    }
   out.println(rounds);
}

private static final FastScanner fs = new FastScanner();
private static final PrintWriter out = new PrintWriter(System.out);

public static class FastScanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");

    String next() {
        while (!st.hasMoreTokens())
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    int[] readArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = nextInt();
        return a;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextString() {
        return next();
    }
}

}

Thanks sir, but sorting work here for checking any element is greater then k.

for i in range(int(input())):
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    d = sorted(a)
    i = 0
    b = 0
    c = []
    if(d[-1] > k):
        print(-1)
    else:
        while(len(a) > 0):
            if(b+a[i] <= k):
                b += a[i]
                a.pop(i)
            else:
                c.append(b)
                b = 0
        if(b != 0):
            c.append(b)
            b = 0
        print(len(c))

It works here.

here also you found your answer in unsorted array. you used sorted copy of given array just for impossible case . in your previous code you were finding answer in sorted array.
use carefully

  1. a.sort()-- it will sort a
  2. b=sorted(a) – here b will be sorted but not a
    i also get confuse here many times.
    :wink:

Yup sir for possible cases I used unsorted array and used b for impossible cases. Thanks sir for your help.

1 Like

happy to help :grinning:

1 Like

You can only pick up the boxes in the order given in the input.

So sorting a won’t work. Try processing a in the original order

I’ve caught your mistake. You’ve put a conditional return statement in the input loop. If there is some Wi > k in the input, you code returns from the function and starts treating further input as another test case whereas the actual current testcase wasn’t over.

Thanks a lot.

Sir, I did sorting in a different array not in original for checking is there any element which is greater then k.

I tried without array, can anyone tell me what is wrong in this code.
#include
#include<stdio.h>
#define ll long long
using namespace std;

int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,k;
	    cin>>n>>k;
	    ll temp,sum=0,ans=0;
	    ll flag=0;
	    for(ll i=0;i<n;i++)
	    {
	        cin>>temp;
	        if(temp>k)
	        {    
	            cout<<-1<<"\n";
	            flag=1;
	            break;        
	        }
	        sum+=temp;
	        if(sum>k){
	            ans++;
	            sum=temp;
	        }
	    }
	    if(flag==1)
	        continue;
	    if(sum>0)
	       ans++;
	    
	    cout<<ans<<"\n";
	}
	return 0;
}

Can any one help me to find my mistake in this code???

#include
using namespace std;

int main(){

int t;scanf("%d",&t);
while(t--){
    int n,k; scanf("%d%d",&n,&k);    
    
    long int sum=0;int noOfTrip=0;int flag=0;
    for(int i=0;i<n;i++){
        int num;
        scanf("%d",&num);
        
        if(num>k){
            flag=1;
            break;

            
        }
        
        sum+=num;
        if(sum==k){
         noOfTrip+=1;
         sum=0;
        }
        else if(sum>k){
            noOfTrip+=1;
            sum=0;
            sum+=num;
            if(sum==k||i==n-1){
                noOfTrip+=1;sum=0;
            }
        }
        
        else if(i+1==n)
        noOfTrip++;
    }
    if(flag==1)
    printf("-1\n");
    else printf("%d\n",noOfTrip);
}
return 0;

}

Please correct. My code is not getting accepted, though it passes the test-cases. It is showing as WA.

#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){

ios_base::sync_with_stdio(true);
cin.tie(NULL);cout.tie(NULL);

ll t;cin>>t;while(t–){
ll n,k;
cin>>n>>k;
ll count =0;
ll a[n]; for(auto &i:a) cin>>i;

for(ll i=0;i<n;i++){ 

   if(a[i]<=k){ 
    ll sum = a[i];
    while(sum<=k){  
           i++;
           sum+=a[i];
     } 
    count++;
    i--; 
     }

    if(a[i]>k){
         if(i==0) {count = -1 ; }
         break; }

}
cout<<count;
cout<<endl;

}
return 0;
}

In line 16, just remove if(i==0) condition and it will work.
if (a[i] > k) {
count = -1;
break;
}

1 Like

Thank you for the suggestion @zacros .It works now.

1 Like

i am unable to figure out whats the flaw in my code can any one help me

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

{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i =0;i<T;i++){
int n = sc.nextInt();
int k = sc.nextInt();
int[] w = new int[n];
boolean b = true;
for(int j=0;j<n;j++){
w[j] = sc.nextInt();
}
for(int j = 0;j<n;j++){

	    if(w[j] > k){

	        System.out.println(-1);
	        b = false;
                    break;
	       
	    }
	        
	    }
	    if(b == true){
	    getCount(w,n,k);
	    }
	}
	
}
public static void getCount(int[] w, int n,int k){
    int tot = 0;
    int count = 0;
    for(int i = 0;i<n;i++){
        tot += w[i];
  
            if(tot>k){
                tot =w[i];
                count++;
            }
            
        
    }
    System.out.println(++count);
}

}

keep up the hard work, this mistake will only improve you

Why this is giving wrong answer?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}

int max=*max_element(arr, arr + n);
int h=0;
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
if(sum>k)
{ sum=arr[i];
h++;
}
}
if(max>k){
cout<<-1<<endl;
}
else{
cout<<h+1;
}
}
return 0;
}

while submitting it is showing the wrong answer and I think it’s working fine can anyone tell me what’s wrong with it any reply will be helpful…:slight_smile:

#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T–){
int n,k;
cin>>n>>k;
int a[n],sum=0,count=0;
for(int i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
}
//cout<<sum<<endl;
if(sum<=k){
count++;
}else{
for(int i=0;i<n;i++){
if(a[i]>k){
count = -1;
break;
}

   else if(sum-a[i]<=k){
    count+=2;
    break;
    }
    else{
    	sum = sum-a[i];
        count++;
        }	
    }

}

cout<<count<<endl;
}
return 0;
}

for i in range(int(input())):
    n,k=map(int,input().split())
    w=list(map(int,input().split()))
    p=0
    count=0
    if w[0]>k:
        print("-1")
    for j in range(n):
         if p+w[j]<=k:
                p+=w[j]
    else:
        count+=1
        p=0
        j-=1
count+=1
print(count)

Any idea whats wrong with this?

@anyamanask you were not taking the whole input, that’s why you got the wrong answer, i tried to submit your code , and got ac , here’s the link CodeChef: Practical coding for everyone
and in setter’s code there is no need to check the sum>0 in last step , just print ans+1.