Sansa and XOR


Problem Statement :


Sansa has an array. She wants to find the value obtained by XOR-ing the contiguous subarrays, followed by XOR-ing the values thus obtained. Determine this value.

Example

Subarray	Operation	Result
3		None		3
4		None		4
5		None		5
3,4		3 XOR 4		7
4,5		4 XOR 5		1
3,4,5		3 XOR 4 XOR 5	2
Now we take the resultant values and XOR them together:

. Return .

Function Description

Complete the sansaXor function in the editor below.

sansaXor has the following parameter(s):

int arr[n]: an array of integers
Returns

int: the result of calculations

Input Format

The first line contains an integer , the number of the test cases.

Each of the next  pairs of lines is as follows:
- The first line of each test case contains an integer , the number of elements in .
- The second line of each test case contains  space-separated integers .



Solution :



title-img


                            Solution in C :

In  C :





#include <stdio.h>
#include <stdlib.h> 
#define MOD 1000000007
#define s(k) scanf("%d",&k);
#define sll(k) scanf("%lld",&k);
#define p(k) printf("%d\n",k);
#define pll(k) printf("%lld\n",k);
#define f(i,N) for(i=0;i<N;i++)
#define f1(i,N) for(i=0;i<=N;i++)
#define f2(i,N) for(i=1;i<=N;i++)
typedef long long ll;
int main(){
    int t,n,k,sum,i;
    s(t)
    while(t--){
        s(n)
        if(!(n%2)){
            printf("0\n");
            f(i,n)
                s(k)
        } else {
            sum=0;
            f(i,n){
                s(k)
                if(!(i%2))
                    sum^=k;
            }
            p(sum);
        }
    }
    return 0;
}
                        


                        Solution in C++ :

In  C++  :






#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cstring>
#include <string>
using namespace std;

#define pairii pair<int, int>
#define llong long long
#define pb push_back
#define sortall(x) sort((x).begin(), (x).end())
#define INFI  numeric_limits<int>::max()
#define INFLL numeric_limits<llong>::max()
#define INFD  numeric_limits<double>::max()
#define FOR(i,s,n) for (int (i) = (s); (i) < (n); (i)++)
#define FORZ(i,n) FOR((i),0,(n))

void solve() {
  int n;
  scanf("%d",&n);
  int res = 0;
  FORZ(i,n) {
    int x;
    scanf("%d",&x);
    if ((i+1)%2 && (n-i)%2) {
      res ^= x;
    }
  }
  printf("%d\n",res);
}

int main() {
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int t;
  scanf("%d",&t);
  FORZ(i,t) solve();
  return 0;
}
                    


                        Solution in Java :

In  Java :







import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Solution {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();
		int num = Integer.parseInt(line);
		
		for (int i = 0; i < num; i++) {
			String count = br.readLine();
			String[] ns = br.readLine().split(" ");
			sansaXor(ns, Integer.valueOf(count));
		}

	}
	
	public static void sansaXor(String[] strs, int count){
		Map<Integer,Integer> map = new HashMap<Integer,Integer>();
		long result = 0;
		for(int i = 0; i < count; i++){
			int numCount = (i + 1)*(count - i);
			int tmp = Integer.valueOf(strs[i]);
			if(map.containsKey(tmp)){
				map.put(tmp, numCount+map.get(tmp));
			}else{
				map.put(tmp, numCount);
			}
		}
		
		for(int k: map.keySet()){
			int value = map.get(k);
			if(value%2!=0){
				result = result^k;
			}
		}
		
		System.out.println(result);
	}
}
                    


                        Solution in Python : 
                            
In  Python3 :






for _ in range(int(input())):
    N = int(input())
    A = tuple(map(int,input().split()))
    X = 0
    for i, x in enumerate(A):
        if ((i+1) * (N-i)) % 2 == 1:
            X ^= x
    print(X)
                    


View More Similar Problems

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →