Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- // https://www.codechef.com/problems/XSQR
- class Codechef
- {
- static Scanner sc = new Scanner(System.in);
- public static int[] getArray(){
- String[] sArr = sc.nextLine().split(" ");
- int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
- return arr;
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- int tc = getArray()[0];
- while(tc>0){
- tc-=1;
- int arrLen = getArray()[0];
- int[] arr = getArray();
- int[] mapp = new int[(int)Math.pow(10,6)+1];//hashmap might result in TLE for this question and also no one crosses n xor operation when pairs of elements choosen below or equal to n
- int[] backupMapp = mapp.clone();
- /**
- * we dont need to care about ai aj ak al
- * as per diagram
- * given ai xor aj = ak xor al
- *
- * applying rhs xor to both rhs will give us
- * ai xor aj xor ak xor al = 0
- * this becomes simliar to what we solved in 4 sum n^2 solution
- */
- //load the n^2 combinations into mapp
- for(int k =0;k<arrLen;k+=1){
- for(int l = k+1 ; l<arrLen;l+=1) {
- mapp[arr[k]^arr[l]]+=1;
- }
- }
- int count = 0;
- for(int j = 1;j<arrLen;j+=1){
- // backupMapp = mapp.clone()
- for(int i =0 ;i<j;i+=1){
- count +=mapp[arr[j]^arr[i]];
- if(mapp[arr[j]^arr[i]]>0){
- mapp[arr[j]^arr[i]]-=1;
- }
- }
- //need to give the space for j+1 and remove already hashed combinations in backupMapp
- int jp1 = j+1;
- for(int temp =jp1+1;temp<arrLen;temp+=1 ){
- int currXor = arr[jp1]^arr[temp];
- if(backupMapp[arr[jp1]^arr[temp]]>0){
- backupMapp[arr[jp1]^arr[temp]]-=1;
- }
- }
- mapp = backupMapp.clone();//transfer to the main mapp
- }
- System.out.println(count*(4*3*2*1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement