XOR Product CodeChef Solution

Problem -XOR Product CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

XOR Product CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#define all(x)  (x).begin(),(x).end()
#define fr(i,n) for(ll i=0;i<n;++i)
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define per(i,a,b) for(ll i=a;i>=b;i--)
#define sz(x) (ll)((x).size())
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> v;
ll mod=998244353;

void testcase(){
    ll n;
    cin >> n;
    priority_queue<ll,vector<ll>,greater<ll>> e;
    ll cnt=0,ans=1;
    for(ll i=0;i<n;i++){
        ll x;
        cin >> x;
        if(x==1)    ++cnt;
        if(x%2==0)  e.push(x);
        if(x&1) ans = (ans*x)%mod;
    }
    while(1){
        if(cnt==0 || e.empty()) break;
        ans = ((ans%mod)*(e.top()+1))%mod;
        e.pop();
        cnt-=1;
    }
    while(!e.empty()){
        ans = (ans*e.top())%mod;
        e.pop();
    }
    cout << ans << "\n";
    return;
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);//freopen("out.txt","w",stdin);
    ll t=1;cin >> t;
    rep(i,1,t)    testcase();
    return 0;
}

XOR Product CodeChef Solution in C++14

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

int main()
{
    int t = 0;
    cin >> t;
    long long mod = 998244353;
    while (t--)
    {
        long long n = 0, x = 0;
        cin >> n;
        long long a = 0;
        vector<long long> v;
        long long p = 1;
        for (long long i = 0; i < n; i++)
        {
            cin >> x;
            if (x == 1)
            {
                a++;
            }
            else if ((x % 2) == 1)
            {
                p = (p * x) % mod;
            }
            else
            {
                v.push_back(x);
            }
        }
        sort(v.begin(), v.end());
        for (int i = 0; i < v.size(); i++)
        {
            if (a > 0)
            {
                v[i]++;
                a--;
            }
            p = (p * v[i]) % mod;
        }
        cout << p << endl;
    }
    return 0;
}

XOR Product CodeChef Solution in PYTH 3

import sys
from math import *
from collections import *
inp = lambda: sys.stdin.buffer.readline().decode().strip()
out=sys.stdout.write
# n=int(inp())
# arr=list(map(int,inp().split()))
for _ in range(int(inp())):
    n=int(inp())
    arr=list(map(int,inp().split()))
    evens=[]
    rest=[]
    ones=0
    mod=998244353
    for num in arr:
        if num==1: ones+=1
        elif num%2: rest.append(num)
        else: evens.append(num)
    evens=deque(sorted(evens))
    ans=1
    # print(ones,evens,rest)
    while ones>0 and evens:
        ev=evens.popleft()
        ans=(ans*(ev+1))%mod
        ones-=1
    for num in rest:
        ans=(ans*num)%mod
    for num in evens:
        ans=(ans*num)%mod
    print(ans)
    
    
    
    

XOR Product CodeChef Solution in C

#include <stdio.h>
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
 
    
    int L[n1], R[n2];
 
    
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    /* Copy the remaining elements of L[], if there
    are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    /* Copy the remaining elements of R[], if there
    are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l + (r - l) / 2;
 
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
 
        merge(arr, l, m, r);
    }
}

int main(void) {
	
	int t;
	scanf("%d",&t);
	int n;
	int a[1000002];
	while(t--)
	{
	  scanf("%d",&n);
	  int o=0;
	  for(int i=0;i<n;i++)
	  {
	      scanf("%d",&a[i]);
	      if(a[i]==1)
	      {
	          o++;
	      }
	  }
	  mergeSort(a,0,n-1);
	  for(int i=0;i<n;i++)
	  {
	      if(o==0)
	      {
	          break;
	      }
	      if(a[i]%2==0)
	      {
	         a[i]++; 
	         o--;
	      }
	  }
	  long long int ans=1;
	  for(int i=0;i<n;i++)
	  {
	      ans=((ans*a[i])%998244353);
	  }
	  printf("%lld\n",ans);
	  
	}
	
	return 0;
}


    
    


XOR Product CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef{
	final static int MOD = 998244353;
	public static void solve(int n, int[] a){
		Arrays.sort(a);
		int ones = 0;
		for(int i:a){
			if(i==1)ones++;
			else break;
		}
		for(int i = 0;i<n&&ones>0;i++){
			if((a[i]&1)==0){
				a[i]=a[i]^1;
				ones--;
			}
		}
		long ans = 1;
		for(int i: a){
			ans*=i;
			ans%=MOD;
		}
		System.out.println(ans);
	}
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while(t-->0){
			int n = s.nextInt();
			int[] a = readArray(n,s);
			solve(n, a);
		}
	}
	public static int[] readArray(int n, Scanner s){
		int[] ret = new int[n];
		for(int i = 0;i<n;i++){
			ret[i]=s.nextInt();
		}
		return ret;
	}
	public static int[][] readMatrix(int row, int col, Scanner s){
		int[][] ans = new int[row][col];
		for(int i = 0;i<row;i++){
			for(int j = 0;j<col;j++){
				ans[i][j]=s.nextInt();
			}
		}
		return ans;
	}
}
class Pair{
	int first;
	int second;
	public Pair(int f, int s){
		first = f;
		second = s;
	}
}

XOR Product CodeChef Solution in PYPY 3

import sys, os
if os.path.exists('input.txt'):
    sys.stdin = open("input.txt","r")
    sys.stdout = open("output.txt","w")
inputarr = lambda : list(map(int, sys.stdin.readline().split()))
inputval = lambda : sys.stdin.readline().strip()
printarr = lambda arr : sys.stdout.write(' '.join(str(i) for i in arr)+'\n')
printval = lambda val : sys.stdout.write(str(val)+'\n')


def solution(n, arr):
    arr.sort()
    ans = 1
    mod = 998244353
    for i in range(n):
        if arr[i]>1:
            break
    for j in range(i, n):
        if arr[j]%2==0 and i:
            ans *= (arr[j]+1)
            i -= 1
        else:
            ans *= arr[j]
        ans %= mod
    printval(ans)

#main
for _ in range(int(inputval())):
    inp = ''
    while inp == '':
        inp = inputval()
    n = int(inp)
    arr = inputarr()
    solution(n, arr)

XOR Product CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

const MOD = 998244353

func solve(A []int) int {
	sort.Ints(A)
	var cnt1 int
	var evens []int
	var others []int
	for i := 0; i < len(A); i++ {
		if A[i] == 1 {
			cnt1++
		} else if A[i]&1 == 0 {
			evens = append(evens, A[i])
		} else {
			others = append(others, A[i])
		}
	}

	for i := 0; i < len(evens) && cnt1 > 0; i++ {
		evens[i]++
		cnt1--
	}

	res := 1

	for _, num := range evens {
		res = modMul(res, num)
	}
	for _, num := range others {
		res = modMul(res, num)
	}

	return res
}

func modMul(a, b int) int {
	r := int64(a) * int64(b)
	return int(r % MOD)
}
XOR Product CodeChef Solution Review:

In our experience, we suggest you solve this XOR Product CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the XOR Product CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this XOR Product CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *