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

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →