Questions and Answers

qwerty

Added: 2025-09-26 08:45:13

Question Image

qwerty

Answer

✏️ Edit

qwerty

permutation modulo 10

Added: 2025-09-19 05:40:00

Question Image

permutation modulo 10

Answer

✏️ Edit
import sys
import math

MOD = 10**9 + 7

# Precompute factorials up to 5 * 10^5 (since sum of N across tests ≤ 5e5)
MAX = 5 * 10**5 + 5
fact = [1] * (MAX)
for i in range(1, MAX):
    fact[i] = (fact[i-1] * i) % MOD

def solve():
    input_data = sys.stdin.read().strip().split()
    t = int(input_data[0])
    idx = 1
    results = []
    
    for _ in range(t):
        N, K = int(input_data[idx]), int(input_data[idx+1])
        idx += 2
        A = list(map(int, input_data[idx:idx+N]))
        idx += N
        
        cnt_even = sum(1 for x in A if x % 2 == 0)
        cnt_odd = N - cnt_even
        
        odd_pos = (N + 1) // 2
        even_pos = N // 2
        
        ans = 0
        
        if K == 0:
            # All must be same parity
            if cnt_even == N or cnt_odd == N:
                ans = fact[N]
            else:
                ans = 0
        
        else:  # K == 1
            # Case 1: odd positions = odd numbers, even positions = even numbers
            if cnt_odd >= odd_pos and cnt_even >= even_pos:
                ans = (ans + fact[odd_pos] * fact[even_pos]) % MOD
            
            # Case 2: odd positions = even numbers, even positions = odd numbers
            if cnt_even >= odd_pos and cnt_odd >= even_pos:
                ans = (ans + fact[odd_pos] * fact[even_pos]) % MOD
        
        results.append(str(ans % MOD))
    
    print("\n".join(results))

if __name__ == "__main__":
    solve()

ZERO-INDEX ARRAY

Added: 2025-09-13 07:30:50

Question Image

ZERO-INDEX ARRAY

Answer

✏️ Edit
import ast

def odd_or_even(strings, m):
    total = 0
    for word in strings:
        product_parity = 1  # assume odd
        for ch in word:
            if ord(ch) % 2 == 0:  # even ASCII → product is even
                product_parity = 0
                break
        total += product_parity  # add 1 if odd, 0 if even
    return "EVEN" if total % 2 == 0 else "ODD"


# --- Input from user ---
arr_line = input().strip()   # e.g. "['ace','oas','oas']"
m = int(input().strip())

# safely parse the Python-style list of strings
strings = ast.literal_eval(arr_line)

print(odd_or_even(strings, m))

ravi and nithish

Added: 2025-09-13 07:10:02

Question Image

ravi and nithish

Answer

✏️ Edit
from math import isqrt
from collections import Counter

# Precompute perfect squares and cubes up to max_sum
MAX_SUM = 2 * 10**9

perfect_squares = set()
i = 1
while i * i <= MAX_SUM:
    perfect_squares.add(i * i)
    i += 1

perfect_cubes = set()
i = 1
while i * i * i <= MAX_SUM:
    perfect_cubes.add(i * i * i)
    i += 1

def is_perfect(x):
    return x in perfect_squares or x in perfect_cubes

def count_perfect_pairs(arr):
    freq = Counter(arr)
    keys = sorted(freq.keys())
    count = 0
    
    for i, x in enumerate(keys):
        for y in keys[i:]:
            total = x + y
            if is_perfect(total):
                if x == y:
                    # count combinations: nC2 = n*(n-1)/2
                    count += freq[x] * (freq[x] - 1) // 2
                else:
                    count += freq[x] * freq[y]
    return count

def main():
    T = int(input())
    for _ in range(T):
        N = int(input())
        arr = list(map(int, input().split()))
        print(count_perfect_pairs(arr))

if __name__ == "__main__":
    main()

byteland

Added: 2025-09-13 06:11:56

Question Image

byteland

Answer

✏️ Edit
#include <stdio.h>

int parent[100001];

int find(int x) {
    if (parent[x] != x) {
        parent[x] = find(parent[x]);
    }
    return parent[x];
}

void union_sets(int x, int y) {
    int px = find(x);
    int py = find(y);
    if (px != py) {
        parent[px] = py;
    }
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    
    for (int i = 1; i <= n; i++) {
        parent[i] = i;
    }
    
    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        union_sets(a, b);
    }
    
    int first_of_component[100001];
    int component_count = 0;
    
    for (int i = 1; i <= n; i++) {
        find(i);
    }
    
    for (int i = 1; i <= n; i++) {
        if (parent[i] == i) {
            first_of_component[component_count++] = i;
        }
    }
    
    printf("%d\n", component_count - 1);
    for (int i = 1; i < component_count; i++) {
        printf("%d %d\n", first_of_component[0], first_of_component[i]);
    }
    
    return 0;
}

remainder modulo 11

Added: 2025-09-13 06:05:56

Question Image

remainder modulo 11

Answer

✏️ Edit
number = input().strip()

remainder = 0
for digit in number:
    remainder = (remainder * 10 + int(digit)) % 11

print(remainder)