Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:10:31+00:00 2026-05-30T15:10:31+00:00

I am working on a problem in which I am expected to take the

  • 0

I am working on a problem in which I am expected to take the xor of
all the pair of integers in an array and then find the K smallest
integers produced from xor’ing. The size of the array can be N=100000
and so K can be quite large but its limited to 250000.

For example,
if N=5 and K=4,

our array is {1 3 2 4 2}

The numbers resulting from xoring(1 and 3, 1-2, 1-4, 1-2, 3-2, 3-4, 3-2 etc)

3 3 2 5 0 1 6 1 6 7

Since K=4, we have to print 4 smallest integers.
so the answer would be 0 1 1 2.

Since the time limit is 2 sec and very tight, using the brute force approach
of xoring all the numbers would time out. My approach was wrong and so I need
help. May be we can exploit the limit on K=250000 and want to know if it is
possible to get the K smallest numbers without xoring all the integers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:10:32+00:00Added an answer on May 30, 2026 at 3:10 pm
    (x ^ y) == (x | y) - (x & y) >= |y - x|
    

    Sorting your numbers in order would be a start, because the difference between the pairs will give you a lower bound for the xor, and therefore a cutoff point for when to stop looking for numbers to xor x with.

    There is also a shortcut to looking for pairs of numbers whose xor is less than (say) a power of 2, because you’re only interested in x <= y <= x | (2 ^ N – 1). If this doesn’t give you enough pairs, increase N and try again.

    EDIT: You can of course exclude the pairs of numbers that you already found whose xor is less than the previous power of 2, by using x | (2 ^ (N – 1) – 1) < y <= x | (2 ^ N) – 1.

    Example based on (sorted) [1, 2, 2, 3, 4]

    Start by looking for pairs of numbers whose xor is less than 1: for each number x, search for subsequent numbers y = x. This gives {2, 2}.

    If you need more than one pair, look for pairs of numbers whose xor is less than 2 but not less than 1: for each number x, search for numbers x < y <= x | 1. This gives {2, 3} (twice).

    Note that the final xor values aren’t quite sorted, but each batch is strictly less than the previous batch.

    If you need more than that, look for pairs of numbers whose xor is less than 4 but not less than 2: for each number x, search for numbers x | 1 < y <= x | 3. This gives {1, 2} (twice); {1, 3}.

    If you need more than that, look for pairs of numbers whose xor is less than 8 but not less than 4: for each number x, search for numbers x | 3 < y <= x | 7. This gives {1, 4}; {2, 4} (twice); {3, 4}.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've been working on a problem for a while now, which involves targeting the
Real problem with an existing webapp which has been working for the last couple
I'm working in an iOS application which uses libXML2 to read XML retrieved from
This time I have a very lame problem, which was working perfectly but now
My problem is: I have a perl script which uses lot of memory (expected
I am working on an Android app that displays photos which are downloaded from
I encounter really weird problem on which I stuck. I am working on some
I'm working on problem four of Project Euler and am running into a stackoverflow
I had a problem working with the image classes in java. I am creating
I have a strange problem working with HTML,CSS in different browsers: Firefox 3.6 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.