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 7508963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:46:40+00:00 2026-05-29T22:46:40+00:00

Given an array that has the numbers {0……2^k -1} except for one number ,

  • 0

Given an array that has the numbers {0......2^k -1} except for one number ,
find a good algorithm that finds the missing number.

Please notice , you can only use :

  • for A[i] return the value of bit j.

  • swap A[i] with A[j].

My answer : use divide & conquer , check the bit number K of all the numbers , if the K bit (now we’re on the LSB) is 0 then move the number to the left side, if the K bit is 1 then move the number to the right side.
After the 1st iteration , we’d have two groups , where one of them is bigger than the other , so we continue to do the same thing, with the smaller group , and I think that I need to check the K-1 bit this time.

But from some reason I’ve tried with 8 numbers , from 0.....7 , and removed 4 (say that I want to find out that 4 is the missing number) , however to algorithm didn’t work out so good. So where is my mistake ?

  • 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-29T22:46:42+00:00Added an answer on May 29, 2026 at 10:46 pm

    Ron,

    your solution is correct. This problem smells Quicksort, doesn’t it ?

    What you do with the Kth bit (all 0’s to the left, 1’s to the right) is a called a partition – you need to find the misplaced elements in pairs and swap them. It’s the process used in Hoare’s Selection and in Quicksort, with special element classification – no need to use a pivot element.

    You forgot to tell in the problem statement how many elements there are in the array (2^k-2 or more), i.e. if repetitions are allowed.

    If repetitions are not allowed, every partition will indeed be imbalanced by one element. The algorithm to use is an instance of Hoare’s Selection (only partition the smallest halve). At every partition stage, the number of elements to be considered is halved, hence O(N) running time. This is optimal since every element needs to be known before the solution can be found.

    [If repetitions are allowed, use modified Quicksort (recursively partition both halves) until you arrive at an empty half. The running time is probably O(N Lg(N)) then, but this needs to be checked.]

    You say that the algorithm failed on your test case: you probably mis-implemented some detail.

    An example:

    Start with
    5132670 (this is range {0..7})

    After partitioning on bit weight=4 you get
    0132|675

    where the shortest half is
    675 (this is range {4..7})

    After partitioning on bit weight=2, you get
    5|67

    where the shortest half is
    5 (this is range {4..5})

    After partitioning on bit weight=1, you get
    |5

    where the shortest half is empty (this is range {4}).
    Done.

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

Sidebar

Related Questions

I am given an array of real numbers, A. It has n+1 elements. It
Given an array A of integers, find any 3 of them that sum to
Given an unsorted array of numbers, write a function that returns true if array
Given that EVAL is Evil how do I create an Array name dynamically: I
I have string that I want to chop to array of substrings of given
Given an array like {one two, three four five}, how'd you calculate the total
Given an array of items, each of which has a value and cost ,
I'm iterating through an array of numbers that come through an API call: [6000,
If I have an array in Javascript that has been filled sporadically, how do
I have an array of numbers that potentially have up to 8 decimal places

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.