Assignment Score
Source: codechef
Difficulty: 794

Assignment Score
Chef has a total of$N + 1$assignments. He knows his score on the first$N$of them, and he is now working on his last assignment. His scores in the first$N$assignment are$A_1, A_2, ..., A_N$. Each of them are graded out of$100$marks, and his total marks is just a sum of all his$N + 1$assignment marks.
Chef is now worried about he might fail in his course. He will fail if he scores less than$50\%$of the total marks. Can you help Chef find what is the minimum score he needs to get to not fail? It may be impossible for Chef to pass, output$-1$then.
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 contains$N$- the number of completed assignments
The second line contains$N$integers -$A_1, A_2, ...., A_N$- the scores on those assignments
Output Format
For each test case, output on a new line
The minimum score needed on the$N + 1$'th assignment for Chef to pass, if it possible for him to pass$-1$if it is impossible for Chef to pass
Constraints$1 \le T \le 100$$1 \le N \le 100$$0 \le A_i \le 100$Sample 1:
Explanation:
Test Case 1
: Chef needs atleast$50 \%$, i.e.$0.5 * 200 = 100$marks to pass. He has$67$marks, hence he needs$33$marks more.
Test Case 3
: Even if Chef gets$100$, he will only have$25 \%$marks. Hence, it is impossible for Chef to pass.

Sample Input:
4
1
67
2
53 32
3
0 0 0
2
100 100

Sample Output:
33
65
-1
0

C++ solution:
#include <iostream>
#include<vector>
using namespace std;

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        int s=0;
        for(int i=0; i<n; i++){
            cin>>a[i];
            s+=a[i];
            
        }
        int x=(n+1)*100;
        int p=x/2;
        if(s>=p){
            cout<<0<<endl;
        }
        else{
            int y=p-s;
            if(y>100){
                cout<<-1<<endl;
            }
            else{
                cout<<y<<endl;
            }
        }
    }

}

Go solution:
package main

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

func chefMinimumScoreNeeded() {
    reader := bufio.NewReader(os.Stdin)
    tInput, _ := reader.ReadString('\n')
    T, _ := strconv.Atoi(strings.TrimSpace(tInput))

    results := make([]int, T)

    for i := 0; i < T; i++ {
        nInput, _ := reader.ReadString('\n')
        N, _ := strconv.Atoi(strings.TrimSpace(nInput))

        scoreLine, _ := reader.ReadString('\n')
        scoreStrings := strings.Fields(scoreLine)
        scores := make([]int, N)

        for j := 0; j < N; j++ {
            scores[j], _ = strconv.Atoi(scoreStrings[j])
        }

        currentScore := 0
        for _, score := range scores {
            currentScore += score
        }

        requiredTotal := ((N + 1) * 100) / 2
        requiredOnLast := requiredTotal - currentScore

        if requiredOnLast > 100 {
            results[i] = -1
        } else {
            results[i] = max(0, requiredOnLast)
        }
    }

    for _, result := range results {
        fmt.Println(result)
    }
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func main() {
    chefMinimumScoreNeeded()
}

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

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();
        
        for (int i=0; i<T; i++) {
            int N = sc.nextInt();
            int score = 0;
            
            for (int j=0; j<N; j++) {
                int A = sc.nextInt();
                score += A;
            }
            int scorePass = (N+1)*100/2;
            int needScore = scorePass - score;
            
            if (needScore <= 0) {
                System.out.println("0");
            } else if (needScore > 100) {
                System.out.println("-1");
            } else {
                System.out.println(needScore);
            }
        }
    }
}

JavaScript solution:
// your code goes here
process.stdin.setEncoding('utf8');
let input = "";

process.stdin.on('data',function(user){
    input += user;
});

process.stdin.on('end', function(){
    let t = input.split("\n");
    let b = parseInt(t[0]);
    let mul = b*2;
    for(let i=1; i<=mul; i=i+2){
        let n = parseInt(t[i]);
        let m = ((n+1)*100)*0.5;
        let a = t[i+1].split(" ");
        let sum = 0;
        for(let j=0; j<n; j++){
            let c = parseInt(a[j])
            sum = sum+c;
        }
        sum = sum+100;
        if(sum<m){
            console.log(-1);
        }
        else{
            sum = sum -100;
            if(sum<m){
            console.log(m-sum);
        }
        if(sum>=m){
            console.log(0);
        }
        }
        
        
    }
});

Python solution:
# cook your dish here
T = int(input())

for _ in range(T):
    N = int(input())
    A = list(map(int, input().split()))
    
    total_possible = (N + 1) * 100
    min_required = total_possible // 2
    current_sum = sum(A)
    
    needed = min_required - current_sum
    
    if needed > 100:
        print(-1)
    else:
        print(max(0, needed))

Ruby solution:
T = gets.to_i

T.times do
  N = gets.to_i
  A = gets.split.map(&:to_i)
  
  total_possible = (N + 1) * 100
  min_required = total_possible / 2
  current_sum = A.sum
  
  needed = min_required - current_sum
  
  if needed > 100
    puts -1
  elsif needed <= 0
    puts 0
  else
    puts needed
  end
end