Opposite Attract
Source: codechef
Difficulty: 671

Opposite Attract
You are given a binary string$S$of length$N$. Your task is to generate a new binary string$T$of the same length such that each corresponding character of$T$is different from the character at the same position in$S$.
In other words, for each position$i$($0 ≤ i < N$), the following condition must hold$T_i ≠ S_i$.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of two lines of input.
The first line of each test case contains one integer$N$— the length of$S$.
The next line contains the binary string$S$.
Output Format
For each test case, output on a new line a binary string$T$of length$N$, where$T_i ≠ S_i$for all valid indices$i$.
Constraints$1 \leq T \leq 10^5$$1 \leq N \leq 10$$S$is a binary string, i.e, contains only the characters$0$and$1$.
The sum of$N$over all test cases won't exceed$2\cdot 10^5$.
Sample 1:
Explanation:
Test case$4$:$T \xrightarrow{} [1100]$, since$T_0 \neq S_0$,$T_1 \neq S_1$,$T_2 \neq S_2$and$T_3 \neq S_3$,$T$is a valid output.

Sample Input:
4
1
0
1
1
3
101
4
0011

Sample Output:
1
0
010
1100

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

int main() {
    int n;
    cin>>n;
    
    while(n--){
        int x;
        cin>>x;
        string str;
        cin>>str;
        for(auto st:str)cout<<(st=='0'?1:0);
        cout<<"\n";
    }

}

Go solution:
package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    defer writer.Flush()

    var t int
    fmt.Fscan(reader, &t)

    for t > 0 {
        t--
        var n int64
        var s string
        fmt.Fscan(reader, &n, &s)

        result := strings.Builder{}
        for i := int64(0); i < n; i++ {
            if s[i] == '0' {
                result.WriteByte('1')
            } else {
                result.WriteByte('0')
            }
        }
        fmt.Fprintln(writer, result.String())
    }
}

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 read = new Scanner(System.in);
        int t = read.nextInt();
StringBuilder r = new StringBuilder();
        while (t--> 0)
        {
            int n = read.nextInt();
            String s = read.next();
            StringBuilder T = new StringBuilder();
            for (int j = 0; j < n; j++)
              {
                    if (s.charAt(j) == '0'){
                        T.append('1');
                    }
                    else {
                        T.append('0');
                    }
    }
            r.append(T).append("\n");
        }
        System.out.println(r);
        }
}

JavaScript solution:
// your code goes here
process.stdin.setEncoding('utf8');
process.stdin.on("data", (input) => {
    let lines = input.trim().split("\n")
    let t = parseInt(lines[0].trim());
    let results = [];
    let inc =1;
    for(let i=0;i<t; i++){
         let n = parseInt(lines[1].trim());
        let a = lines[inc+1].split("");
        inc+=2;
        for(let p in a ){
            if(a[p]=="1"){
                a[p]= "0"
            }else{
                a[p]="1"
            }
        }
        results.push(a.join(""));
    }
    console.log(results.join("\n"));
});

Python solution:
# cook your dish here
def solve():
    n = int(input())
    s = input()
    
    t = ""
    for char in s:
        if char == '0':
            t += '1'
        else:
            t += '0'
    print(t)

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

Ruby solution:
# Read number of test cases
test_count = gets.to_i

test_count.times do
  # Read and ignore string length (not needed)
  gets
  
  # Read binary string and remove newline
  binary_str = gets.chomp
  
  # Generate opposite string
  opposite_str = binary_str.gsub(/[01]/) { |bit| bit == '0' ? '1' : '0' }
  
  # Output result
  puts opposite_str
end