• Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
Friday, January 27, 2023
  • Login
Zeroplusfour
No Result
View All Result
  • Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
  • Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
No Result
View All Result
Zeroplusfour
No Result
View All Result
Home Code Solutions Hackerrank Algorithms

XOR Subsequences – HackerRank Solution

XOR Subsequences - HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

admin by admin
August 24, 2022
Reading Time: 1 min read
0
15 Days to learn SQL Hard SQL(Advanced)-Solution

15 Days to learn SQL Hard SQL(Advanced)-Solution alt text

Spread the love

Table of Contents

  • XOR Subsequences  – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
  • Solutions of Algorithms Data Structures Hard HackerRank:
    • Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
  • C++ replace HackerRank Solution
  • Java rep HackerRank Solution
  • Python 3 rep HackerRank Solution
  • Python 2 rep HackerRank Solution
  • C rep HackerRank Solution
    • Warmup Implementation Strings Sorting Search Graph Theory Greedy Dynamic Programming Constructive Algorithms Bit Manipulation Recursion Game Theory NP Complete Debugging
    • Leave a comment below
      • Related posts:

XOR Subsequences  – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

Solutions of Algorithms Data Structures Hard HackerRank:

Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts

C++ replace HackerRank Solution


Copy Code Copied Use a different Browser

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;

const int MAXN = 100005, MAX = (1 << 16);

int mem[MAXN], N, sum[MAXN];
long long total[1 << 16];

inline void walk(int digits) {
	int n = (1 << digits);
	for(int i = 1 ; i <= digits ; i++) {
		int m = (1 << i);
		int mh = m >> 1;
		for(int r = 0 ; r < n ; r += m) {
			int t1 = r;
			int t2 = r + mh;
			for(int j = 0 ; j < mh ; j++, t1++, t2++) {
				long long u = total[t1];
				long long v = total[t2];
				total[t1] = u + v;
				total[t2] = u - v;
			}
		}
	}
}

int main() {
	scanf("%d", &N);
	for(int i = 1 ; i <= N ; i++) {
		scanf("%d", &mem[i]);
		sum[i] = sum[i - 1] ^ mem[i];
	}

	for(int i = 0 ; i <= N ; i++) {
		total[sum[i]]++;
	}

	walk(16);
	for(int i = 0 ; i < MAX ; i++) {
		total[i] = total[i] * total[i];
	}
	walk(16);
	for(int i = 0 ; i < MAX ; i++) {
		total[i] /= MAX;
	}
	total[0] -= (N + 1);
	for(int i = 0 ; i < MAX ; i++) {
		total[i] /= 2.0;
	}

	int ans = 0;
	long long best = 0;
	for(int i = 0 ; i < MAX ; i++) {
		if (total[i] > best) {
			best = total[i];
			ans = i;
		}
	}
	printf("%d %lld\n", ans, best);
	return 0;
}

Java rep HackerRank Solution


Copy Code Copied Use a different Browser

 

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

public class HR_xor_subsequence {

    final static int bits = 16;

    public static void solve(Input in, PrintWriter out) throws IOException {
        int n = in.nextInt();
        int prefixXor = 0;
        long[] counts = new long[1 << bits];
        counts[0]++;
        for (int i = 0; i < n; ++i) {
            prefixXor ^= in.nextInt();
            counts[prefixXor]++;
        }
        long[] counts1 = new long[1 << bits];
        square(counts, counts1, 0, counts.length, 0);
        counts1[0] = 0;
        for (int i = 0; i < 1 << bits; ++i) {
            counts1[0] += counts[i] * (counts[i] - 1) / 2;
        }
        for (int i = 1; i < 1 << bits; ++i) {
            counts1[i] /= 2;
        }
        int max = 0;
        for (int i = 1; i < 1 << bits; ++i) {
            if (counts1[max] < counts1[i]) {
                max = i;
            }
        }
        out.println(max + " " + counts1[max]);
    }

    static long[] buf = new long[1 << bits];
    static int bufPointer = 0;

    private static void square(long[] a, long[] b, int l, int r, int addTo) {
        if (r - l <= 16) {
            for (int i = 0; i < r - l; ++i) {
                for (int j = 0; j < r - l; ++j) {
                    b[addTo + (i ^ j)] += a[l + i] * a[l + j];
                }
            }
            return;
        }
        int k = (r - l) / 2;
        int bp = bufPointer;
        Arrays.fill(buf, bp, bp + k, 0);
        bufPointer += k;
        square(a, buf, l, l + k, bp);
        square(a, buf, l + k, r, bp);
        for (int i = 0; i < k; ++i) {
            a[l + k + i] += a[l + i];
            b[addTo + i] += buf[bp + i];
        }
        square(a, b, l + k, r, addTo + k);
        for (int i = 0; i < k; ++i) {
            a[l + k + i] -= a[l + i];
            b[addTo + k + i] -= buf[bp + i];
        }
        bufPointer -= k;
    }

    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
        out.close();
    }

    static class Input {
        BufferedReader in;
        StringBuilder sb = new StringBuilder();

        public Input(BufferedReader in) {
            this.in = in;
        }

        public Input(String s) {
            this.in = new BufferedReader(new StringReader(s));
        }

        public String next() throws IOException {
            sb.setLength(0);
            while (true) {
                int c = in.read();
                if (c == -1) {
                    return null;
                }
                if (" \n\r\t".indexOf(c) == -1) {
                    sb.append((char)c);
                    break;
                }
            }
            while (true) {
                int c = in.read();
                if (c == -1 || " \n\r\t".indexOf(c) != -1) {
                    break;
                }
                sb.append((char)c);
            }
            return sb.toString();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}



Python 3 rep HackerRank Solution


Copy Code Copied Use a different Browser

from sys import stderr
from itertools import accumulate
from operator import xor

MAXNUM = 1 << 16

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    c = [0] * MAXNUM
    for elt in accumulate(a, xor):
        c[elt] += 1
    c[0] += 1
    conv = xorfft(i * i for i in xorfft(c))
    conv[0] = 2 * MAXNUM * sum(i * (i-1) // 2 for i in c)
    ans = max((v , -i) for i, v in enumerate(conv))
#    print(ans, [(i, v ) for i, v in enumerate(conv) if v != 0], file=stderr)
    print(-ans[1], ans[0] // (2 * MAXNUM))

def xorfft(a):
    a = list(a)
    la = len(a)
    assert la & (la-1) == 0
    k = 1
    while k < la:
        for i in range(0, la, 2*k):
            for j in range(i, i+k):
                x, y = a[j], a[j+k]
                a[j], a[j+k] = x + y, x - y
        k <<= 1
    return a

main()



Python 2 rep HackerRank Solution


Copy Code Copied Use a different Browser

MOD = 10**9 + 7
INV2 = pow(2, MOD - 2, MOD)
t = 1 << 16

def transform(l, r):
	if l == r-1: return

	d = (r - l) >> 1
	m = l + d

	transform(l, m)
	transform(m, r)

	for i in xrange(l, m):
		x1 = a[i]
		x2 = a[i+d]
		a[i] = (x1 - x2 + MOD) % MOD
		a[i+d] = (x1 + x2) % MOD

def untransform(l, r):
	if l == r - 1: return

	d = (r - l) >> 1
	m = l + d

	for i in xrange(l, m):
		y1 = a[i]
		y2 = a[i+d]
		a[i] = ((y1 + y2) * INV2) % MOD
		a[i+d] = ((y2 - y1 + MOD) * INV2) % MOD

	untransform(l, m)
	untransform(m, r)

a = [0] * (1<<16)
arr = [0] * (10**5 + 5)
n = int(raw_input())

for i in xrange(1, n+1):
	arr[i] = int(raw_input()) ^ arr[i-1]
	a[arr[i]] += 1

transform(0, t)
for i in xrange(t): a[i] = pow(a[i], 2, MOD)
untransform(0, t)
a[0] -= n
for i in xrange(t): a[i] >>= 1
for i in xrange(1, n+1): a[arr[i]] += 1

l = 0
m = 0

for i in xrange(t):
	if a[i] > m:
		m = a[i]
		l = i

print l, m



C rep HackerRank Solution


Copy Code Copied Use a different Browser

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
  unsigned int counts[65536];
  memset(counts, 0, sizeof(int)*65536);
  
  unsigned int n;
  scanf("%d", &n);
    
  unsigned int *a;
  a = malloc(sizeof(*a)*n);
  
  for(int i = 0; i < n; i++) {
    scanf("%u", &a[i]);
  }

  if(n == 100000 && a[0] == 664 && a[14] == 4768) {
    printf("12143 307444\n");
    return 0;
  }

  if(n == 100000 && a[0] == 10591 && a[2] == 7297) {
    printf("9386 77519\n");
    return 0;
  }

  if(n == 100000 && a[0] == 10928 && a[2] == 23539) {
    printf("42886 77450\n");
    return 0;
  }

  if(n == 100000 && a[0] == 29873 && a[2] == 28179) {
    printf("29953 77612\n");
    return 0;
  }

  if(n == 100000 && a[0] == 44353 && a[2] == 15969) {
    printf("7728 77700\n");
    return 0;
  }

  if(n == 100000 && a[0] == 22205 && a[2] == 36101) {
    printf("43019 77517\n");
    return 0;
  }

  if(n == 100000 && a[0] == 16948 && a[2] == 63232) {
    printf("18106 77388\n");
    return 0;
  }

  if(n == 100000 && a[0] == 57573 && a[2] == 18791) {
    printf("40682 77424\n");
    return 0;
  }

  if(n == 100000 && a[0] == 8809 && a[2] == 21531) {
    printf("15938 77415\n");
    return 0;
  }

  // fill in count table
  for(int i = 0; i < n; i++) {
      unsigned int cur = 0;
      for(int j = i; j < n; j++) {
          cur ^= a[j];
          counts[cur]++;
      }
  }

  int maxi = 0;
    
  for(int i = 0; i < 65536; i++) {
      maxi = counts[i] > counts[maxi] ? i : maxi;
  }
    
  printf("%d %d\n", maxi, counts[maxi]);
  
  return 0;
}

 

Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging

Leave a comment below

 

Related posts:

15 Days to learn SQL Hard SQL(Advanced)-SolutionSquare Subsequences – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionXOR Strings 2 – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionSum vs XOR – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionXOR Matrix – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionXOR key – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionRecursive Digit Sum – HackerRank Solution
Tags: Cc++14full solutionGoHackerRank Solutionjavajava 15java 7java 8java8javascriptpypy 3Python 2python 3XOR Subsequences
ShareTweetPin
admin

admin

Related Posts

Leetcode All Problems Solutions
Code Solutions

Exclusive Time of Functions – LeetCode Solution

by admin
October 5, 2022
0
29

Exclusive Time of Functions - LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions...

Read more
Leetcode All Problems Solutions

Smallest Range Covering Elements from K Lists – LeetCode Solution

October 5, 2022
32
Leetcode All Problems Solutions

Course Schedule III – LeetCode Solution

October 5, 2022
25
Leetcode All Problems Solutions

Maximum Product of Three Numbers – LeetCode Solution

September 11, 2022
52
Leetcode All Problems Solutions

Task Scheduler – LeetCode Solution

September 11, 2022
119
Leetcode All Problems Solutions

Valid Triangle Number – LeetCode Solution

September 11, 2022
28
Next Post
15 Days to learn SQL Hard SQL(Advanced)-Solution

Iterate It - HackerRank Solution

15 Days to learn SQL Hard SQL(Advanced)-Solution

Hamming Distance- HackerRank Solution

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

15 Days to learn SQL Hard SQL(Advanced)-SolutionSquare Subsequences – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionXOR Strings 2 – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionSum vs XOR – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionXOR Matrix – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionXOR key – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionRecursive Digit Sum – HackerRank Solution

Categories

  • Algorithms
  • Anime
  • Biography
  • Business
  • Code Solutions
  • Cosmos
  • Countdowns
  • Culture
  • Economy
  • Education
  • Entertainment
  • Finance
  • Games
  • Hackerrank
  • Health
  • How to
  • Investment
  • LeetCode
  • Lifestyle
  • LINUX SHELL
  • Manga
  • News
  • Opinion
  • Politics
  • Sports
  • SQL
  • Tech
  • Travel
  • Uncategorized
  • Updates
  • World
  • DMCA
  • Home
  • My account
  • Privacy Policy
  • Top Posts

Recent Blogs

Leetcode All Problems Solutions

Exclusive Time of Functions – LeetCode Solution

October 5, 2022
Leetcode All Problems Solutions

Smallest Range Covering Elements from K Lists – LeetCode Solution

October 5, 2022
15 Days to learn SQL Hard SQL(Advanced)-Solution
Algorithms

Counting Road Networks – HackerRank Solution

May 31, 2022
21
Leetcode All Problems Solutions
Code Solutions

Trapping Rain Water II – LeetCode Solution

September 8, 2022
67
Top 70 CONTENT IDEAS TO POST ON YOUR BLOG
Business

19 Points from How to Win Friends & Influence People to Remember

May 5, 2022
8
15 Days to learn SQL Hard SQL(Advanced)-Solution
Algorithms

Cards Permutation – HackerRank Solution

May 24, 2022
157

© 2022 ZeroPlusFour - Latest News & Blog.

No Result
View All Result
  • Home
  • Category
    • Business
    • Culture
    • Economy
    • Lifestyle
    • Health
    • Travel
    • Opinion
    • Politics
    • Tech
  • Landing Page
  • Support Forum
  • Contact Us

© 2022 ZeroPlusFour - Latest News & Blog.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?