Solve an interview question

Source: Internet
Author: User

Problem description: there is a set of numbers, from 1 to n, which reduces the number of 3, the order is also disrupted, placed in an array of n-3

Please find the missing number. It is better to have a program, and the best algorithm is faster.
Suppose n = 10000


My solutions:

I implemented it using the bitmap method. The idea is as follows:
A bitmap of [1, m] (simple, each element is of the bool type), all initialized to false.
Step 1
Convenience target array. Use each value as the following table to locate the corresponding location in bitmap and set it to true.
Part 2:
Scan a node in bitmap that is true and record it. This is the answer.
You only need to facilitate the two-frequency group.

The c code is as follows:

Key Problem Solving functions:


Void FindLostNum (int * pData, int nTotalNum, int * pResult, int nLost)
Parameter explanation: pData points to an integer array containing nTotalNum unordered data elements. The element value [1, nTotalNum + nLost] pResult points to a result integer array containing nLost nodes, the code used to receive returned results is successfully debugged under vs2010.

# Include <iostream>
# Include <math. h>
Using namespace std;
 
# Define N (4000)
# Define LOST <span style = "white-space: pre"> </span> (3)
 
Typedef struct _ NODE_NUM
{
Bool bUsed; // whether it has been used
} NODE_NUM, * PNODE_NUM;
 
// Generate a number table in disorder of [1, n. Parameter: Header pointer, total number, lost Number
Bool GenerateRandomNumSet (PNODE_NUM pData, int nTotalNum, int nLostNum );
 
// Find the missing number
Void FindLostNum (int * pData, int nTotalNum, int * pResult, int nLost );
 
Bool GenerateRandomNumSet (int * pData, int nTotalNum, int nLostNum)
{
PNODE_NUM pList = NULL;
Int index = 0;
Int temp = 0;
Int loc = 0;
 
If (pData = NULL | nTotalNum <= 0)
Return false;

// Initialize the 1-n digital sequence table
PList = new NODE_NUM [nTotalNum];
If (pList = NULL)
Return false;
For (index = 0; index <nTotalNum; index ++)
{
(PList + index)-> bUsed = false;
}
 
/* Initialize the [1, n-lost] disordered number table */
Try
{
For (index = 0; index <nTotalNum-nLostNum; index ++)
{

Loc = rand () % nTotalNum;
While (1)
{
If (pList + loc)-> bUsed)
{
Loc ++;
If (loc> = nTotalNum)
Loc = 0;
}
Else
Break;
}
* (PData + index) = loc;
(PList + loc)-> bUsed = true;
}
}
Catch (exception e)
{
Cout <"parameter error ";
}

Delete pList;
Return true;
}
 
Void FindLostNum (int * pData, int nTotalNum, int * pResult, int nLost)
{
PNODE_NUM pList = NULL;
Int index = 0;
Int count = 0;
 
If (pData = NULL | nTotalNum <= 0 | pResult = NULL | nLost <= 0)
Return;
 
// Initialize the 1-n digital sequence table
PList = new NODE_NUM [nTotalNum + nLost];
If (pList = NULL)
Return;
For (index = 0; index <nTotalNum + nLost; index ++)
{
(PList + index)-> bUsed = false;
}
 
// Scan the given sequence and mark it in pList
For (index = 0; index <nTotalNum; index ++)
{
(PList + * (pData + index)-> bUsed = true;
}
 
For (index = 0; index <nTotalNum + nLost; index ++)
{
If (! (PList + index)-> bUsed)
{
* (PResult + count) = index;
Count ++;
}
}
}
Int main ()
{
Int * pData = NULL;
Int * pResult = NULL;
 
PData = new int [N];
If (pData = NULL)
Return-1;
 
PResult = new int [LOST];
If (pResult = NULL)
{
Delete pData;
Return-1;
}
 
GenerateRandomNumSet (pData, N, LOST );
 
FindLostNum (pData, N-LOST, pResult, LOST );
 
// Output result
For (int index = 0; index <LOST; index ++)
Cout <* (pResult + index) <"\ t ";
 
// Test Result
For (int index = 0; index <N-LOST; index ++)
{
For (int c = 0; c <LOST; c ++)
{
If (* (pData + index) = * (pResult + c ))
Cout <endl <"result check error ";
}
}
 
Delete pData;
Delete pResult;
Return 1;
}


From the boundless ocean of shyandsy

Related Article

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.