Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Sansa and XOR problem solution

YASH PAL, 31 July 2024

In this HackerRank Sansa and XOR problem solution, we have an array and we need to find the value obtained by XOR using the contiguous subarrays, followed by XOR using the values thus obtained.

HackerRank Sansa and XOR problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

T = int(input())
for _ in range(T):
    N = int(input())
    ar = [int(i) for i in input().split()]
    
    result = 0
    count = 0
    for i in range(N):
        count += N - 2*i
        if (count) % 2 == 1:
            result = result ^ ar[i]
    print(result)

Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan = new Scanner(System.in);
        int numOfTests = scan.nextInt();
        
        for(int test = 0; test < numOfTests; test++){
            int[] array = new int[scan.nextInt()];
            
            for(int i = 0; i < array.length; i++){
                array[i] = scan.nextInt();
            }
            
            int result = 0;
            
            if(array.length == 0)
                result = 0;
            else if(array.length == 1)
                result = array[0];
            else if(array.length%2 == 0)
                result = 0;
            else{
                for(int i = 0; i < array.length; i+=2)
                    result^= array[i];
            }
            System.out.println(result);

        }
    }
    
    
}

Problem solution in C++.

#include <vector>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,ans=0;
        cin>>n;
        for(int f=0;f<n;f++)
        {
            int x;
            cin>>x;
            if(f%2==0)
                ans^=x;
        }
        if(n%2)
            cout<<ans<<endl;
        else
            cout<<0<<endl;
    }
}

Problem solution in C.

#include <stdio.h>
#include <stdlib.h>
int a[100000];

int main(){
  int T,N,ans,i;
  scanf("%d",&T);
  while(T--){
    ans=0;
    scanf("%d",&N);
    for(i=0;i<N;i++)
      scanf("%d",a+i);
    if(N%2){
      for(i=0;i<N;i+=2)
        ans^=a[i];
      printf("%dn",ans);
    }
    else
      printf("0n");
  }
  return 0;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes