Again XOR problem CodeChef Solution – Code Drive Solution
About Again XOR problem CodeChef Solution
- This is a coding contest based on algorithms, data structures, and problem-solving.
- Organizer: The contest is hosted by NIT Trichy.
- Prizes: NA
- Registrations for prizes: NA
Problem: Again XOR problem CodeChef Solution
You may have solved a lot of problems related to XOR and we are sure that you liked them, so here is one more problem for you.
You are given a binary string SS of length NN. Let string TT be the result of taking the XOR of all substrings of size KK of SS. You need to find the number of 1
bits in TT.
Note:
- A substring is a continuous part of string which can be obtained by deleting some (may be none) character’s from the beginning and some (may be none) character’s from the end.
- XOR of many similar-sized strings is defined here.
Input Format
- The first line of the input contains an integer TT – the number of test cases. The test cases then follow.
- The first line of each test case contains two integers NN and KK.
- The second line of each test case contains a binary string SS of size NN.
Output Format
For each test case, output on a single line number of 1
bits in the XOR of all substrings of size KK of SS.
Constraints
- 1≤T≤101≤T≤10
- 1≤K≤N≤2⋅1051≤K≤N≤2⋅105
- |S|=N|S|=N
- SS only contains characters
0
and1
.
Sample Input 1
4
4 1
1010
4 2
1001
4 3
1111
4 4
0110
Sample Output 1
0
2
0
2
Explanation
- Test case 11: All 11-sized substrings of SS are:
0
1
1
0
The XOR of these strings is 0
, therefore the number of 1
bits is 00.
- Test case 22: All 22-sized substrings of SS are:
10
00
01
The XOR of these strings is 11
, therefore the number of 1
bits is 22.
- Test case 33: All 33-sized substrings of SS are:
111
111
The XOR of these strings is 000
, therefore the number of 1
bits is 00.
- Test case 44: All 44-sized substrings of SS are:
0110
The XOR of these strings is 0110
, therefore the number of 1
bits is 22.
Solution: Again XOR problem CodeChef Solution
Again XOR problem CodeChef Solution Using Python
# cook your dish here
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
s=0
for j in l:
s=s^j
print(s*2)
Again XOR problem CodeChef Solution Using Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int arr[] = new int[n];
int result=0;
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
result^=(arr[i]+arr[i]);
}
System.out.println(result);
}
}
}
Again XOR problem CodeChef Solution Using C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
vector<ll>v;
ll xorr=0;
for(ll i=0;i<n;i++)
{
ll val;
cin>>val;
xorr=xorr^(2*val);
}
cout<<(xorr)<<endl;
}
}
Contest Details:
- This is an External Rated Contest.
- Duration: 3 Hours
- Start time: 30th December 2021, 20:00 hrs IST
- End time: 30th December 2021, 23:00 hrs IST
You may check your timezone here. - This contest is rated only for Division 2 and Division 3 users. Division 1 users can participate unofficially in this contest.
Get More CodeChef Solution >>
Substring Minimum Function FizzBuzz Solution
Magical Planks Fizzbuzz Solution
Favourite String of Chef CodeChef Solution
Sum this up Code Chef Solution
Game between friends CodeChef Solution