Poster Perimeter
Source: codechef
Difficulty: 812

Poster Perimeter
Chef wants to print out a poster on a
rectangular
piece of paper.
The piece of paper he uses cannot be too large, or his printer will be unable to handle it.
Specifically, the length of the rectangle must be an
integer
between$1$and$N$units, and the width must be an
integer
between$1$and$M$units.
Chef would like it if the perimeter of the paper is as close to$K$as possible.
If Chef chooses the paper's dimensions optimally, find the
minimum possible difference
between the paper's perimeter and$K$.
Recall that the perimeter of a rectangle with length$l$and width$w$equals$2\cdot (l+w)$.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
The first and only line of input will contain three space-separated integers$N, M,$and$K$—— the maximum allowed length and width, and the target perimeter.
Output Format
For each test case, output on a new line the minimum possible difference between the paper's perimeter and$K$.
Constraints$1 \leq T \leq 100$$1 \leq N, M, K \leq 100$Sample 1:
Explanation:
Test case$1$:
The target perimeter is$12$, and the maximum allowed length and width are both$5$.
Chef can choose a length of$2$and a width of$4$, leading to a perimeter of$2\cdot (2 + 4) = 12$.
So, the minimum difference from$12$is$0$.
Test case$2$:
The target perimeter is$5$. It can be shown that attaining a perimeter of exactly$5$is impossible, but it's possible to reach$6$instead (with length$1$and width$2$, for example), which is only$1$away from the target.
Test case$3$:
The optimal perimeter is$6$, obtained by choosing length$2$and width$1$. This has a difference of$|6 - 9| = 3$away from the target, which is the best we can do.
Test case$4$:
Choosing$l = w = 1$gives a perimeter of$2\cdot (1 + 1) = 4$, which is the closest Chef can get to his target of$1$.
The difference is$|1 - 4| = 3$.

Sample Input:
4
5 5 12
3 4 5
2 1 9
34 45 1

Sample Output:
0
1
3
3

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

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--){
        int n,m,k;
        cin>>n>>m>>k;
        int perimeter=2*(n+m);
        if(perimeter<k){
            cout<<k-perimeter<<endl;
        }else{
            if(k==1 || k==2 || k==3 || k==4){
                cout<<4-k;
            }else{
                if(k%2==0) cout<<0;
                else cout<<1;
            }
        }
        cout<<endl;
    }
}

Go solution:
package main

import (
    "fmt"
    "math"
)

func main() {
    var t int
    fmt.Scanln(&t)
    results := make([]int, 0, t)

    for i := 0; i < t; i++ {
        var l, b, k int
        fmt.Scan(&l, &b, &k)

        min := math.MaxInt64

        for i := 1; i <= l; i++ {
            for j := 1; j <= b; j++ {
                perimeter := 2 * (i + j)
                diff := int(math.Abs(float64(perimeter - k)))

                if diff < min {
                    min = diff
                }
            }
        }
        results = append(results, min)
    }

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

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

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        
        while(T-->0){
        int N=sc.nextInt();
        int M=sc.nextInt();
        int K=sc.nextInt();
        
        int minDifference=Integer.MAX_VALUE;
        
        for(int l=1;l<=N;l++){
            for(int w=1;w<=M;w++){
                int perimeter=2*(l+w);
            int difference=Math.abs( perimeter-K);
            if(difference<minDifference){
                minDifference=difference;
            }
                
            }
            
            
        }
            
        
        System.out.println(minDifference);
    }
        

    }
}

JavaScript solution:
function main() {
    const SrIsaIbrAhmendra1 = require('fs').readFileSync('/dev/stdin', 'utf8');
    const SrIsaIbrAhmendra2 = SrIsaIbrAhmendra1.split('\n').map(SrIsaIbrAhmendra3 => SrIsaIbrAhmendra3.trim()).filter(SrIsaIbrAhmendra3 => 
        SrIsaIbrAhmendra3);

    let SrIsaIbrAhmendra4 = parseInt(SrIsaIbrAhmendra2[0]);
    let SrIsaIbrAhmendra5 = [];

    for (let SrIsaIbrAhmendra6 = 1; SrIsaIbrAhmendra6 <= SrIsaIbrAhmendra4; SrIsaIbrAhmendra6++) {
        const [SrIsaIbrAhmendra7, SrIsaIbrAhmendra8, SrIsaIbrAhmendra9] = SrIsaIbrAhmendra2[SrIsaIbrAhmendra6].split(' ').map(Number);
        let SrIsaIbrAhmendra10 = Infinity;

        for (let SrIsaIbrAhmendra11 = 1; SrIsaIbrAhmendra11 <= SrIsaIbrAhmendra7; SrIsaIbrAhmendra11++) {
            for (let SrIsaIbrAhmendra12 = 1; SrIsaIbrAhmendra12 <= SrIsaIbrAhmendra8; SrIsaIbrAhmendra12++) {
                let SrIsaIbrAhmendra13 = 2 * (SrIsaIbrAhmendra11 + SrIsaIbrAhmendra12);
                SrIsaIbrAhmendra10 = Math.min(SrIsaIbrAhmendra10, Math.abs(SrIsaIbrAhmendra13 - SrIsaIbrAhmendra9));
            }
        }

        SrIsaIbrAhmendra5.push(SrIsaIbrAhmendra10);
    }

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

main();

Python solution:
# cook your dish here
T= int(input())
for i in range(T):
    x,y,z= map(int,input().split())
    if 2*(x+y)<=z:
        print(z-2*(x+y))
    elif z<=4:
        print(4-z)
    elif z & 1:
        print(1)
    else:
        print(0)

Ruby solution:
def solve
  # Read input line safely
  input = gets
  return if input.nil? || input.strip.empty?
  
  n, m, k = input.strip.split.map(&:to_i)
  min_diff = Float::INFINITY

  # Iterate through all possible dimensions
  (1..n).each do |l|
    (1..m).each do |w|
      perimeter = 2 * (l + w)
      current_diff = (perimeter - k).abs
      min_diff = current_diff if current_diff < min_diff
      # Early exit if we find perfect match
      if min_diff == 0
        puts 0
        return
      end
    end
  end

  puts min_diff
end

# Main program
begin
  t = gets.to_i
  t.times { solve }
rescue NoMethodError, ArgumentError => e
  # Handle any unexpected input errors
  puts "Error processing input: #{e.message}"
end