Breaking Sticks
Source: codechef
Difficulty: 596

Breaking Sticks
You have$N$sticks, of integral lengths$A_1, A_2, ..., A_N$.
You can take any stick of length$X$and break it into$2$positive integer
lengths$Y$and$Z$such that$Y + Z = X$, i.e. now instead of the stick of length$X$, you obtained one stick of length$Y$and another of length$Z$.
One such action is called a
break
. Find the maximum number of
breaks
you can perform.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first line of input contains$N$- the number of sticks.
The second line contains$N$integers -$A_1, A_2, ..., A_N$, the lengths of the sticks
Output Format
For each test case, find the maximum number of
breaks
you can perform.
Constraints$1 \le T \le 100$$1 \le N \le 100$$1 \le A_i \le 100$Sample 1:
Explanation:
Test Case 1
: There are no possible
breaks
to do because it is not possible to split$1$into$2$positive integer numbers.
Test Case 2
: We can break the stick of length$3$into sticks of lengths$2$and$1$. Further, we then break the stick of length$2$into sticks of lengths$1$and$1$. Thus, we performed$2$breaks
.

Sample Input:
2
2
1 1
1
3

Sample Output:
0
2

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

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a,b=0;
        for(int i=0;i<n;i++){
            cin>>a;
            b=b+a-1;
        }
        cout<<b<<endl;
    }
}

Go solution:
package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    T, _ := strconv.Atoi(scanner.Text())
    for ; T > 0; T-- {
        scanner.Scan()
        N, _ := strconv.Atoi(scanner.Text())
        scanner.Scan()
        parts := strings.Fields(scanner.Text())
        sum := 0
        for i := 0; i < N; i++ {
            a, _ := strconv.Atoi(parts[i])
            sum += a - 1
        }
        fmt.Println(sum)
    }
}

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 ttt = sc.nextInt();
        while(ttt-->0){
            int nt = sc.nextInt();
            int bt = 0;
            
            for(int i=0;i<nt;i++){
                int le = sc.nextInt();
                if(le>1){
                    bt+=(le-1);
                }
            }
            System.out.println(bt);
        }
    }
}

JavaScript solution:
function solve(){
    const readline = require("readline").createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: false,
    })
    
    let lines = [];
    
    readline.on("line", (line) => {
        lines.push(line);
    })
    
    readline.on("close", () => {
        processTestCases(lines);
    })
}

function processTestCases(lines){
    let t = parseInt(lines[0]);
    let lineInd = 1;
    while(t--){
        let n = parseInt(lines[lineInd++]);
        let arr = lines[lineInd++].split(" ").map(Number);
        let sum = arr.reduce((acc, curr) => acc + curr, 0);
        console.log(sum - n);
    }
}

solve();

Python solution:
# cook your dish here
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    total_breaks = 0
    for length in a:
        if length > 1:
            total_breaks += (length - 1)
    print(total_breaks)

t = int(input())
for _ in range(t):
    solve()

Ruby solution:
def calculate_max_breaks
  # Read number of test cases
  num_test_cases = gets.to_i

  num_test_cases.times do
    # Read number of sticks
    num_sticks = gets.to_i
    # Read stick lengths
    sticks = gets.split.map(&:to_i)

    total_breaks = 0
    sticks.each do |length|
      total_breaks += length - 1 if length > 1
    end

    puts total_breaks
  end
end

calculate_max_breaks