2 Boxes
Source: codechef
Difficulty: 832

2 Boxes
You have$2$boxes, the first having$X$stones, and the second having$Y$stones.
You want the (absolute) difference between the stones of the$2$boxes to be exactly$K$.
Each second, you can take a stone from the$1^{st}$box and put it in the$2^{nd}$, or take from the$2^{nd}$and put it in the$1^{st}$box.
Find the minimum time till the difference between the stones of the$2$boxes becomes exactly$K$. If it is impossible, print$-1$instead.
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 and only line of input contains$3$integers -$X, Y$and$K$.
Output Format
For each test case, output on a new line the minimum time to get the difference to exactly$K$or$-1$if impossible.
Constraints$1 \le T \le 10^3$$1 \le X, Y \le 10$$0 \le K \le X + Y$Sample 1:
Explanation:
Test Case 1
: The difference is already$0$.
Test Case 2
: It can be shown that it is impossible.
Test case 3
: We can spend$2$seconds, taking one stone from$1^{st}$box to$2^{nd}$each time. So, we will have$0$stones in$1^{st}$box and$4$stones in$2^{nd}$box after$2$seconds. The difference is exactly$4$.

Sample Input:
7
2 2 0
2 2 1
2 2 4
2 2 2
8 3 1
4 8 2
4 8 3

Sample Output:
0
-1
2
1
2
1
-1

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int x, y, k;
        cin >> x >> y >> k;
        int total = x + y;
        int diff = abs(x - y);

        if (k > total || (total - k) % 2 != 0) {
            cout << -1 << endl;
        } else {
            cout << (abs(diff - k) / 2) << endl;
        }
    }
    return 0;
}

Go solution:
package main
import
(
    "fmt"
    "math"
    )
    
    
    func minTimeToDifference(X, Y, K int)int {
        diff := int(math.Abs(float64(X-Y)))
        if diff == K {
            return 0
        }
        maxPossiblediff :=X + Y 
        if K <= maxPossiblediff && (K%2==diff%2) {
            return (int(math.Abs(float64(K-diff))))/2
        } else {
            return -1
        }
    }

func main(){
    var T int
    fmt.Scan(&T)
    for i:=0;i<T;i++ {
        var X,Y,K int
        fmt.Scan(&X, &Y, &K)
        fmt.Println(minTimeToDifference(X,Y,K))
    }

}

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 in=new Scanner(System.in);
int t=in.nextInt();
while(t-->0){
    int x=in.nextInt();
    int y=in.nextInt();
    int k=in.nextInt();
    int dif=Math.abs(x-y);
        if((dif&1)==(k&1)){
            int ans=Math.abs(dif-k);
            ans=ans/2;
            System.out.println(ans);
        }
        else{
            System.out.println("-1");
        }
        
}
    }
}

JavaScript solution:
// your code goes here
function solve() {
  const fs = require("fs");
  const inputData = fs.readFileSync(0, "utf-8").trim().split(/\s+/);
  if (!inputData.length) return;
  const testCount = parseInt(inputData[0]);
  let pointer = 1;
  let outputLines = [];

  for (let i = 0; i < testCount; i++) {
    let firstBox = parseInt(inputData[pointer]);
    let secondBox = parseInt(inputData[pointer + 1]);
    let desiredDiff = parseInt(inputData[pointer + 2]);
    pointer += 3;
    let currentDiff = Math.abs(firstBox - secondBox);
    if (currentDiff === desiredDiff) {
      outputLines.push("0");
    } else if ((desiredDiff - currentDiff) % 2 !== 0) {
      outputLines.push("-1");
    } else {
      let movesRequired = Math.abs(desiredDiff - currentDiff) / 2;
      outputLines.push(movesRequired.toString());
    }
  }
  process.stdout.write(outputLines.join("\n") + "\n");
}

if (require.main === module) {
  solve();
}

Python solution:
# cook your dish here
def solve():
    x, y, k = map(int, input().split())
    
    if (x + y - k) % 2 != 0:
        print(-1)
    else:
        print((abs(x - y) - k) // 2 if abs(x-y) >=k else (k - abs(x - y))//2)

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

Ruby solution:
def solve
  x, y, k = gets.split.map(&:to_i)
  sum = x + y
  if (sum - k) % 2 != 0
    puts -1
  else
    diff = (x - y).abs
    if diff >= k
      puts (diff - k) / 2
    else
      puts (k - diff) / 2
    end
  end
end

t = gets.to_i
t.times { solve }