# Chef and Work Codechef Solution

Chef and Work Codechef Solution: Chef has N small boxes arranged on a line from 1 to N. For each valid i, the weight of the ii-th box is WiWi. Chef wants to bring them to his home, which is at the position 00. He can hold any number of boxes at the same time; however, the total weight of the boxes he’s holding must not exceed K at any time, and he can only pick the ith box if all the boxes between Chef’s home and the ith box have been either moved or picked up in this trip.

Therefore, Chef will pick up boxes and carry them home in one or more round trips. Find the smallest number of round trips he needs or determine that he cannot bring all boxes home.

### Input

• The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
• The first line of each test case contains two space-separated integers NN and KK.
• The second line contains NN space-separated integers W1,W2,…,WNW1,W2,…,WN.

### Output

For each test case, print a single line containing one integer ― the smallest number of round trips or −1−1 if it is impossible for Chef to bring all boxes home.

### Constraints

• 1≤T≤1001≤T≤100
• 1≤N,K≤1031≤N,K≤103
• 1≤Wi≤1031≤Wi≤103 for each valid ii

```4
1 1
2
2 4
1 1
3 6
3 4 2
3 6
3 4 3
```

```-1
1
2
3
```

### Explanation

Example case 1: Since the weight of the box higher than KK, Chef can not carry that box home in any number of the round trip.

Example case 2: Since the sum of weights of both boxes is less than KK, Chef can carry them home in one round trip.

Example case 3: In the first round trip, Chef can only pick up the box at position 11. In the second round trip, he can pick up both remaining boxes at positions 22 and 33.

Example case 4: Chef can only carry one box at a time, so three round trips are required

### Chef and Work CodeChef Solution in JAVA

```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);
int tc = Integer.parseInt(sc.next());
while (tc-- != 0) {
int n = Integer.parseInt(sc.next());
int k = Integer.parseInt(sc.next());
int trips = 1;
boolean flag = false;
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = Integer.parseInt(sc.next());
}
int temp = 0;
for (int j = 0; j < n; j++) {
if (arr[j] > k) {
flag = true;
break;
}
temp += arr[j];
if (temp > k) {
trips++;
temp = 0;
j--;
}
}
if (flag)
trips = -1;
System.out.println(trips);
}
}
}
```

### Chef and Work CodeChef Solution in CPP

```#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int a[n];
int l=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]>k)
{
l=1;
}
}
if(l==1)
{
cout<<"-1\n";
continue;
}
int sum=0,d=1;
for(int i=0;i<n;i++)
{
if(sum+a[i]<=k)
{
sum=sum+a[i];
}
else
{
sum=0;
d++;
sum=sum+a[i];
}
}
cout<<d<<endl;
}
}```

### Chef and Work CodeChef Solution in Python

```from math import *
import sys
def input():
sys.setrecursionlimit(10**9)
for _ in range(int(input())):
n,k = list(map(int,input().split()))
l = list(map(int,input().split()))
s = 0
ans = 0
box_c = 0
for i in range(n):
if l[i]>k:
ans = -1
break
if s+l[i] > k:
if box_c == 0:
ans = -1
break
else:
box_c = 1
ans += 1
s = l[i]
else:
s += l[i]
box_c +=1
else:
if s!=0 and box_c>0:
ans+=1
print(ans)```

Disclaimer: The above Problem (Chef and Work ) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring