Minimum Bottles
Source: codechef
Difficulty: 656

Minimum Bottles
There are$N$identical water bottles, each of which has a capacity of$X$liters.
The$i^{th}$bottle initially contains$A_i$liters of water.
You want to go on a trip and want to carry
all
your water with you.
However, to not make packing a hassle, you also want to carry the least number of bottles with you.
You can transfer any amount of water from one bottle to another, provided there is no spillage and no bottle contains more than$X$liters. Water from one bottle can be transferred to different bottles if you wish to do that.
What is the
minimum
number of bottles that you can carry with you, while still having all your water?
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of two lines of input.
The first line of each test case contains two space-separated integers$N$and$X$— the number of bottles and capacity of each bottle in liters, respectively.
The second line contains$N$space separated integers$A_1,A_2,\ldots,A_N$denoting the volume of water in liters filled in each bottle.
Output Format
For each test case, output on a new line the minimum number of bottles that can be carried.
Constraints$1 \leq T \leq 100$$1 \leq N \leq 100$$1 \leq X \leq 1000$$1 \leq A_i \leq X$Sample 1:
Explanation:
Test case$1$:
Transfer all the water from the second and third bottles into the first bottle.
The first bottle will now contain$6$liters of water (which is within its capacity of$X = 10$), while the second and third bottles will be empty.
So, we only need to carry the first bottle with us.
Test case$2$:
Transfer one liter of water from the first bottle to the fourth bottle. The bottles now have$[0, 2, 2, 2]$liters of water.
We'll take only the second, third, and fourth bottles - for three in total.

Sample Input:
2
3 10
1 2 3
4 2
1 2 2 1

Sample Output:
1
3

C++ solution:
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        vector<int> a(n);
        int sum=0;
        int count=0;
        for (int i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        if(sum % x == 0)
        {
            cout << sum / x << endl;
        }
        else
        {
            cout << sum/x + 1<< endl;
        }
        
        
        
    }

}

Go solution:
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    T, _ := strconv.Atoi(strings.TrimSpace(readLine(reader)))
    for t := 0; t < T; t++ {
        line1 := strings.Split(readLine(reader), " ")
        N, _ := strconv.Atoi(line1[0])
        X, _ := strconv.Atoi(line1[1])
        A := strings.Split(readLine(reader), " ")
        totalWater := 0
        for i := 0; i < N; i++ {
            water, _ := strconv.Atoi(A[i])
            totalWater += water
        }
        fmt.Println((totalWater + X - 1) / X)
    }
}

func readLine(reader *bufio.Reader) string {
    line, _ := reader.ReadString('\n')
    return strings.TrimSpace(line)
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

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();
        while(t-->0)
        {
            int n=sc.nextInt();
            int x=sc.nextInt();
            int totalb=0;
            for(int i=0;i<n;i++)
            {
                totalb+=sc.nextInt();
                
            }
            int minb=(totalb+x-1)/x;
            System.out.println(minb);
            
        }

    }
}

JavaScript solution:
let stdInput = '';
process.stdin.on('data', input => stdInput += input);
process.stdin.on('end', () => {
    const input = stdInput.trim().split('\n');
    const T = parseInt(input[0]);
    let results = [];

    for (let i = 1; i < input.length; i += 2) {
        const [N, X] = input[i].split(' ').map(Number);
        const A = input[i + 1].split(' ').map(Number);

        const totalWater = A.reduce((sum, value) => sum + value, 0);
        const minBottles = Math.ceil(totalWater / X);
        results.push(minBottles);
    }

    console.log(results.join('\n'));
});

Python solution:
t = int(input())
for _ in range(t):
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    total_water = sum(a)
    min_bottles = (total_water + x - 1) // x
    print(min_bottles)

Ruby solution:
def main
  t = gets.to_i
  t.times do
    n, x = gets.split.map(&:to_i)
    a = gets.split.map(&:to_i)
    total_water = a.sum
    min_bottles = (total_water + x - 1) / x  # Integer division for ceiling
    puts min_bottles
  end
end

main