Look for missing integers in the array (find the missing integer)

Source: Internet
Author: User

The array A contains n elements, whose elements are between [0,n], and there are no duplicate elements, you find the missing element in the array (because there are n+1 elements between [0,n], and the array can only store n elements, so there must be an element missing). The operation of an array of arrays satisfies the following conditions: The elements in the element cannot be read in constant time, but a bit value of the elements in the array can be read, allowing the position of the two elements of the array to be exchanged within a constant time. Design an algorithm that allows you to find missing elements in the array in a linear time. (n=2^k)

an array  a[]  contains all of the integers from 0 to N, except 1. However, you cannot access an element with a single operation. Instead, you can call  Get (i, K)  which returns the KTH bit of &NBSP a[i]  or you can call  Swap (i, J) Span style= "line-height:22.5px" > which swaps the ith and jth elements of  a[]

Algorithm design: There is a problem set to know, [0,n] between the odd and even number of the difference is equal to 1, if the missing even, the number of odd and even numbers equal, and vice versa missing odd. How do you judge the parity of an element? The problem sets a bit value that accesses the array element only in constant time, so it is only possible to see whether the last bit of the element is 0 or 1, so that N/2 elements can be excluded by a single scan of the array. Using this method (judging the number of 0, 1 numbers) We can find the missing element. Repeat the above actions.

Algorithm Performance Analysis:

The first sweep surface element to judge parity, need to loop n times, thus excluding N/2 elements, so the second cycle needs to cycle N/2 times, one analogy, the total number of cycles is t

T=n+n/2+n/4+n/8+......+1=o (n), this has achieved the requirements of the topic.

Algorithm implementation:

<span style= "font-size:10px;" >void swap (int* A, int i, int j) {int temp = A[i];a[i] = a[j];a[j] = temp;} int get (int* A, int i, int k) {int res = a[i]>>k & 1;return res;} int Find_the_missing_integer (int* A, int low, int. high) {int res = 0;int Power = 1;int Count0 = 0;int Count1 = 0;int n = hi Gh-low+1;int Pointer0 = 0;int i=0;while (n>0) {for (int j=low; j<=high;j++) {if (get (a,j,i) = = 0) {Count0++;swap (a,j, POINTER0);p ointer0++;} else{count1++;}} if (count0>count1) {res = res + Power*1;low = Pointer0;n = Count1;} Else{res = res + Power*0;high = Pointer0-1;pointer0 = Low;n = count0;} Power = Power*2;count0 = 0;count1 = 0;i++;} return res;} </span>

Algorithm explanation: Find_the_missing_intger, the function uses pointer to record the last element's 0 or 1 of the cutoff point, and then each time the sweep surface to meet our requirements of the part of the element, until finally there is no element position.

Test code:

#include <iostream>using namespace Std;void swap (int* A, int i, int j), int get (int* A, int i, int k); int find_the_mi Ssing_integer (int* A, int low, int.); void main () {int A[]={1,2,3,4,5,6,7,0};cout<<find_the_missing_integer (a , 0,7);}

If there is no clear place to welcome the discussion.

Look for missing integers in the array (find the missing integer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.