Placing 01 And 10
Source: codechef
Difficulty: 1114

Placing 01 And 10
You have$X$$01$strings and$Y$$10$strings. You will form a new string$S$by first rearranging these strings in some order, and then concatenate them in this
rearranged order
.
Let$f(S)$denote the number of indices$i$such that$1 \le i < |S|$and$S_i \ne S_{i + 1}$. Find the
minimum possible
value of$f(S)$.
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 contains$2$integers -$X$and$Y$.
Output Format
For each test case, output on a new line the minimum possible value of$f(S)$.
Constraints$1 \le T \le 10^4$$1 \le X, Y \le 100$Sample 1:
Explanation:
Test case$1$: We rearrange as$10$,$01$,$10$and concatenate to get$S = 100110$which has$f(S) = 3$as the indices$i = 1, 3, 5$satisfy$S_i \ne S_{i + 1}$.
Test case$2$:$S = 01 + 10 = 0110$has$f(S) = 2$.

Sample Input:
4
1 2
1 1
10 20
5 4

Sample Output:
3
2
39
9

C++ solution:
#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define endl cout << "\n"
#define pii pair<int, int>
#define ll long long
#define pll pair<ll, ll>

int main()
{
    int tc, x,y;
    cin >> tc;
    while (tc--)
    {
        cin >> x >> y;
        if(x==y)
        cout << x*2;
        else
        cout << max(x,y)*2-1;
        endl;
        /*
        int lo=0,hi=78497,mid;
        bool found=false;
        while(lo<=hi){
            mid=(hi+lo)/2;
            if(all_prime[mid]==n)
            {
                found=true;
                break;
            }
            else if(all_prime[mid]>n)
            hi=mid-1;
            else lo=mid+1;
        }
        if(found)
        yes;
        else no;
    }
        */
    }
    return 0;
}

Go solution:
package main
import (
    "os"
    "fmt"
    "bufio"
)

func calculateNumberOfTransactions(p1 int, p2 int) int {
    var transaction int = 0
    if p1 > p2 {
        temp := p2
        p2 = p1
        p1 = temp
    }
    transaction += p1 * 2
    p2 = p2 - p1
    p1 = 0
    if p2 != 0 {
        transaction++
        p2--
        transaction += p2 * 2
    }
    return transaction
}

func main(){
    // your code goes here
    reader := bufio.NewReader(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    defer writer.Flush()
    
    var t int
    fmt.Fscanln(reader, &t)
    for index := 0; index < t; index++ {
       var p1, p2 int
       fmt.Fscanln(reader, &p1, &p2)
       fmt.Fprintln(writer, calculateNumberOfTransactions(p1, p2))
    }
    
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main{
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br=new BufferedReader(new InputStreamReader(System.in));
        }
        String next(){
            while(st==null || !st.hasMoreTokens()){
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str="";
            try {
                str=br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    static class FastWriter {
        private final BufferedWriter bw;

        public FastWriter() {
            this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
        }

        public void print(Object object) throws IOException {
            bw.append("" + object);
        }

        public void println(Object object) throws IOException {
            print(object);
            bw.append("\n");
        }

        public void close() throws IOException {
            bw.close();
        }
    }
    public static void main(String[] args) {
        try {
            FastReader in=new FastReader();
            FastWriter out = new FastWriter();
            int testCases=in.nextInt();
            while(testCases-- > 0){
                // write code here
                int x=in.nextInt();
                int y=in.nextInt();
               
               if(x<y){
                    int cnt=Math.abs(x-y);
                
                    int ans=(x+y)+(y-1-x);
                
                     out.println(ans);
               } else if(x>y){
                   int cnt=Math.abs(x-y);
                    int ans=(x+y)+(x-1-y);
                    out.println(ans);
                 
               }else{
                     out.println(x+y);
               }
               
                
            }
            out.close();
        } catch (Exception e) {
            return;
        }
    }
}

JavaScript solution:
// CodeChef JavaScript (Node.js) template
function main(input) {
    const inputArray = input.trim().split(/\s+/).map(Number);
    let index = 0;

    const T = inputArray[index++]; // Number of test cases

    for (let t = 0; t < T; t++) {
        const X = inputArray[index++]; // Number of "01" strings
        const Y = inputArray[index++]; // Number of "10" strings

        let result;
        if (X === Y) {
            // If the numbers are equal, optimal f(S) = X + Y.
            result = X + Y;
        } else {
            // Otherwise, let M be the larger count.
            const M = Math.max(X, Y);
            result = 2 * M - 1;
        }

        console.log(result);
    }
}

// CodeChef input handling
process.stdin.resume();
process.stdin.setEncoding('utf8');
let input = '';
process.stdin.on('data', (chunk) => {
    input += chunk;
});
process.stdin.on('end', () => {
    main(input);
});

Python solution:
import sys

def solve():
    input = sys.stdin.read().split()
    idx = 0
    T = int(input[idx])
    idx += 1
    results = []
    for _ in range(T):
        X = int(input[idx])
        Y = int(input[idx + 1])
        idx += 2
        if X == Y:
            res = 2 * X
        else:
            res = 2 * max(X, Y) - 1
        results.append(res)
    print('\n'.join(map(str, results)))

solve()

Ruby solution:
def solve
  input = $stdin.read.split.map(&:to_i)
  idx = 0
  t = input[idx]
  idx += 1
  results = []
  t.times do
    x = input[idx]
    y = input[idx + 1]
    idx += 2
    if x == y
      res = 2 * x
    else
      res = 2 * [x, y].max - 1
    end
    results << res
  end
  puts results.join("\n")
end

solve