Binary Sum
Source: codechef
Difficulty: 1015

Binary Sum
You're given two integers$N$and$K$. Your task is to determine if there exists a binary string$S$of length$N$such that:$S_1 + S_2 + S_3 + \ldots + S_N = K$, i.e, the sum of all the digits of the string equals$K$; and$S_i \neq S_{i+1}$for every$1 \leq i \lt N$, meaning that no two adjacent digits are equal.
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 each test case contains two space-separated integers$N$and$K$.
Output Format
For each test case, output on a new line a single string denoting the answer:
"YES"
if a valid binary string exists, and
"NO"
otherwise (without quotes).
Each character of the output may be printed in either uppercase or lowercase, i.e., the strings
NO
,
No
,
nO
, and
no
will all be treated as equivalent.
Constraints$1 \leq T \leq 100$$1 \leq N \leq 100$$0 \leq K \leq 100$Sample 1:
Explanation:
Test case$1$:$S=\mathtt{01010}$is valid, since no two adjacent digits are equal and the sum of its digits equals$0+1+0+1+0 = 2$.
Test case$2$:
It can be proved that no valid string$S$exists for$N = 7$and$K = 5$.
Test case$3$:$S=\mathtt{101010101}$is valid.

Sample Input:
5
5 2
7 5
9 5
12 7
82 41

Sample Output:
YES
NO
YES
NO
YES

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

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--){
        int N, K;
        cin >> N >> K;
        if(N%2==0){
            if(K==N/2){
                cout<<"YES"<<endl;
            }else{
                cout<<"NO"<<endl;
            }
        }else{
            if(K==N/2 + 1 || K==N/2){
                cout<<"YES"<<endl;
            }else{
                cout<<"NO"<<endl;
            }
        }
        
    }

}

Go solution:
package main

import "fmt"

func main() {
    var test int
    fmt.Scan(&test)
    for test > 0 {
        test--

        var akki, bunny int
        fmt.Scan(&akki, &bunny)

        maxi := (akki + 1) / 2
        mini := akki / 2

        if bunny >= mini && bunny <= maxi {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

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

class Codechef {
    public static void main(String[] args) throws java.lang.Exception {
        // Create a scanner object to read input
        Scanner scanner = new Scanner(System.in);
        
        // Read the number of test cases
        int T = scanner.nextInt();
        
        // Process each test case
        for (int t = 0; t < T; t++) {
            // Read N and K for each test case
            int N = scanner.nextInt();
            int K = scanner.nextInt();
            
            // Calculate the maximum and minimum number of 1's in the binary string
            int maxOnes = (N + 1) / 2;
            int minOnes = N / 2;
            
            // Check if K matches either the max or min number of 1's
            if (K == maxOnes || K == minOnes) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }

        // Close the scanner object
        scanner.close();
    }
}

JavaScript solution:
// your code goes here
process.stdin.setEncoding('utf8')
process.stdin.on("data" ,function(input){
    input = input.trim().split('\n');
    let T = parseInt(input[0]);
    for(let i=1; i<=T; i++){
        let arr =input[i]
        arr = arr.split(' ').map(Number)
        let n = arr[0];
        let k = arr[1];
        
        let divition = Math.floor(n / 2);
         
        if (n % 2 == 0){
            
                if (divition == k){
                                       console.log("YES");
                }else{
                    console.log("NO");
                }
            
        }else{
                if(divition == k || divition+1 == k ){
                                        console.log("YES");
                
                }else{
                                        console.log("NO");
                     }
             }
        
    }
         

});

Python solution:
# cook your dish here
T = int(input())
for _ in range(T):
    N, K = map(int, input().split())
    max_ones = (N + 1) // 2
    min_ones = N // 2
    if min_ones <= K <= max_ones:
        print("YES")
    else:
        print("NO")

Ruby solution:
T = gets.to_i
T.times do
  n, k = gets.split.map(&:to_i)
  max_ones = (n + 1) / 2
  min_ones = n / 2
  if k >= min_ones && k <= max_ones
    puts "YES"
  else
    puts "NO"
  end
end