There are two arrays A and B with the length of m and n, respectively. Find the same elements in the two arrays with the number of comparisons not greater than m + n, and delete the same elements in the array.

Source: Internet
Author: User

There are two arrays A and B with the length of m and n, respectively. Find the same elements in the two arrays with the number of comparisons not greater than m + n, and delete the same elements in the array.
There are two arrays A and B with the length of m and n, respectively. Find the same element in the two arrays with the number of comparisons not greater than m + n

Today I encountered A pen question: there are two arrays A and B, with the length of m and n respectively. Find the same element in the two arrays with the number of comparisons not greater than m + n. I didn't do it at the time. Now I am giving C #, which makes up a little regret.

Using System; using System. collections. generic; using System. linq; using System. text; namespace SortAB {class Program {static void Main (string [] args) {int [] A = RandomIntArray (10 ); // m = 10 int [] B = RandomIntArray (5); // n = 5 // outputs two arrays of Console. write ("array A:"); foreach (int m in A) Console. write ("{0}", m. toString (); Console. write ("\ n Array B:"); foreach (int n in B) Console. write ("{0}", n. toString ()); // Find the same element in the array StringBuilder list = new StringBuilder (); foreach (int m in A) {int getInt = find (m, B); if (getInt! =-1) list. append (B [getInt]);} Console. writeLine (); Console. write ("repeated elements in the two arrays include: {0}", list); // use the intersection of the two arrays to verify IEnumerable <int> intersect =. intersect (B); Console. write ("\ n intersection of two Arrays:"); foreach (int vars in intersect) Console. write ("{0}", vars); Console. readLine ();} // binary search. N must be an ordered array; otherwise, the public static int find (int key, int [] N) {int lb = 0; int ub = N. length-1; int temp; while (true) {temp = (lb + ub)/2; if (N [temp] = key) return temp; else if (lb> ub) return-1; else {if (N [temp] <key) lb = temp + 1; else ub = temp-1 ;}}} public static int [] RandomIntArray (int count) {int [] array = new int [count]; Random r = new Random (unchecked (int) DateTime. now. ticks); for (int I = 0; I <count; I ++) {array [I] = r. next (100) ;}return array ;}}}

However, I am still confused: the number of comparisons may be greater than m + n.


We know two ordered arrays, AB. array a has the length of m, array B has the length of n, and can be found at most in the cycle of m n times. The same elements in array B are solved using the merge algorithm.
I = j = 0
While (I <= m & j <= n)
If (a [I]> B [j])
J ++;
Else if (a [I] <B [j])
I ++;
Pringf ("a [% d] = B [% d]", I, j );
There are two arrays A and B of N (1 ≤ N ≤ 100) elements, where A comes from the input and assigns it "value" to B (that is, the elements analyze the true questions of recent years one by one, frequently checked complex algorithms, data structures, stacks, binary tree traversal, and the test of the binary method. readers find the key concepts of this part. Learning Points

A: Concept algorithms, time complexity and space complexity of Algorithms

Two. Data structure definition, data logical structure and physical structure definition

. Stack definition and business, storing linear tables

4. Concepts of trees and binary, binary tree, full Binary Tree, and basic properties of binary tree traversal

5. Binary Search

6. Bubble Sorting Algorithm


Basic concepts of the test site Algorithm

Exam session:

The written test site, with a probability assessment of 30%, primarily aims to fill in the blank form of scores for 2 minutes, the content of the Memory Test Center, the reader should know the basic operations of algorithms in the data.

A computer solves a problem by running an algorithm. It is called a computer algorithm.

A: The basic features of the algorithm are feasibility, uncertainty, limitations, and sufficient information.

II. Basic elements of the algorithm:

(1) algorithm for calculating and Calculating data

Algorithms include two basic elements: computing and manipulating data objects, and controlling structure algorithms.

In computer systems, the basic computation and operation of the following four types: arithmetic, logical operators, Relational operators and data transmission.

(2) It is an administrative command operation called an algorithm control structure between the control structures of the algorithm.

The algorithm described, which is usually a tool for traditional technology? In the figure, the NS structure chart and the description language of the algorithm. General algorithms can be used for selection. The three basic control structures are cyclically combined.

Complexity of two algorithms in the Test Center

Exam session:

The two test centers perform regular checks. During the test, there is a 70% chance, mainly in the form of a 2-Point Scoring memory test center key content. Readers should remember, the concept of time complexity and space complexity.


The time complexity of an algorithm is the computing work required to implement the algorithm. /> The same algorithms in different languages, or different compilers are used to compile or run on different computers, with different efficiency, this indicates that the efficiency of the algorithm measured by absolute time units is inappropriate. The computer hardware, software, and workload of running a specific algorithm depends only on the size (usually) of the integer n, which is a function size problem. Yes, this algorithm works

Volume = F (N)

(2) algorithms

The spatial complexity of an algorithm is the memory space required by the algorithm.

Storage space algorithms, including algorithms, input initial data, and extra storage space for Algorithm Execution, space occupied, the Work Plan Implementation Unit during this period, and some extra space data structure algorithms require additional storage space. If the relative size of the number of extra space problems is constant, the algorithm is the storage space of the workplace, in this way, many actual problems are solved to reduce algorithms, compress and use the storage technology to reduce unnecessary extra space.

Troubleshooting: Is this algorithm a computing workload?

The workload of basic operations of an algorithm is a function. The workload of the algorithm is F (N), where n is the size.

1.2 Basic Concept Data Structure

Detection Center three data structure definitions

Exam session:

The three test sites in writing, the content of the examination appear in the test opportunity, 70%, mainly based on the two-score point, which is stored in the form selected by the test center, readers should also know, the concept of logical structure and data storage structure.

Data structure as a computer research and discussion of the following three aspects:/> (1) internal logic between data elements collected, which is the logical structure of data;/> (2) the processed data elements are stored in the computer and the relationship between each data element in the data storage structure;

(3) calculate various data structures.

Data: objective objects are dealing with symbols. They can be divided into computers and computer programs. in computer science, they are all symbols. <BR/data element: basic unit of data, usually used as a computer program for overall consideration and processing.

A Data Object is essentially a subset of data in a group of data units. /> The logical structure is a data element, which can be used to represent the relationship between quantities. The logical description of the logical structure of the two elements between the data elements in this set, defines a group: first, the collection is usually composed of R. when the data element is usually composed of D, D, it reflects the data elements before and after, the relationship between the data structure can be expressed as = (4)

Where, the data structure represented by B is reflected in every data element in D, a common tuples.

The relationship between the logical structure of the data stored in the data storage structure in the computer's storage space is known (also known as the pre-and post-physical data structure ). /> The memory space may be different in the logic of the location relationship between the computer as a data element. Therefore, to indicate the logical relationship (context) Data Storage Structure between data elements stored in the computer's memory space, data elements not only need to store the relationship between various data elements required before information storage.

Yes. Multiple common storage structures, links, indexes, and other storage sequential structures are required. Data processing efficiency varies with storage structures. Therefore, it is important to select an appropriate storage structure for data processing.

Test Site 4 linear and nonlinear structure> test period:

The written test center regularly checks and tests, but the test center reader understands the written test, the main form, 30% of the opportunity, and the score of the memory content filling test center.

Back and forth the data structure between the various elements of the complex relationship data, the general data structure is divided into two types: linear and non-linear structure, if a non-empty data structure, to meet the following two conditions:/> (1), and only one root;

(2) Each node before and after the project.

The linear structure of the data structure of the claim. A linear structure is also called a linear table that inserts or deletes a linear structure of any item in a node. It should be a linear structure. If a data structure is a linear structure, it is called a non-linear structure.

? Troubleshooting: Is the empty data structure linear or non-linear?

An empty data structure, what is a straight-chain structure or a straight-chain structure, based on the specific situation, to determine whether the data structure algorithm is linear structure rules processing, it is a linear and non-linear structure.

1.3 stack and linear list

Basic operations on the test center stack

Exam link:

The written test center, 5, is a mandatory content that may appear in the test, 100%, the main form, scored two points, this test center focuses on the content, readers should choose stack operations.

A. the basic concept stack limits only the linear form of insert and delete operations at one end. It is usually called "insert in". Deleting a stack is at the bottom of the other side at the top of the stack, when no element in the table is called an empty stack. The top-level element is always the inserted element. This is the first element to be deleted, and the element is always the first insert unit at the bottom of the stack. This is also the last deleted element. Stack is the principle of "first" or "first-in-first-out" to organize data.

Storage and Operation Sequence of two stacks

The sequential storage stack space of one-dimensional arrays (section 1st: meters), with the maximum capacity of m.

The top-level elements of the stack sequence (1 M) of Bucket S. The next element is at the bottom (bottom) of the stack. (S) = 0 indicates that the stack is empty = M. The stack is full.

Three types of stacks: basic stack operations, retired stacks, and top-level Elements read.

Stack operation: (1) Insert a stack pointer plus one (first 1) at the stack operation position, and then insert a new element at the stack pointer position, the stack pointer points to the last location of the bucket. The stack space is full and there are no new elements. A long stack operation is called a stack overflow error.

(2) Retirement stack operation: the top element of the Retirement stack is deleted and allocated to the specified variable. The stack pointer of the first top element (element at the top) is assigned to a specified variable, and then the stack pointer is reduced by one (that is, the top is reduced by 1 ). When the top stack pointer is 0, it indicates that the stack is empty and cannot be returned. Call stack overflow error condition. /> (3) read the top element of the stack: The top element is the top element allocated to a specified variable. This operation does not delete the top of the elements in the stack and only assigns it to one variable? So the stack pointer has not changed. When the top stack pointer is 0, it indicates that the stack is empty and the best element is read.

Tip: the stack is an "advanced" or "back-to-first-out" principle to organize data. However, there are multiple options for the stack method, which are often used for examination and check various stack modes.

6 basic concepts of the test site, linear table

Exam session:

The place of the test is 30%. The test result is based on the selected form and the score is 2 points. This memory test content center. Centralized storage nodes. /> For chained storage, each node consists of two parts: one memory, used to store a part of the value of the data element, called a data field, and the other is used to store pointers, it is called a pointer field. Use a pointer to point to a node or node (that is, the first segment ). /> A shop chain is used to represent a linear structure or a non-linear structure. /> (1) linear list

The storage structure of a chain linear table is called a linear list.

In some applications, set two pointers. each node in the linear list is called the first node of the Left pointer, and the other right Pointer Points to a so-called subsequent member node. This table is called a two-way linked list.

(2) link Stack

Stack is a linear form and can also adopt a chained storage structure. Stack chains can be used to collect all available memory called stacks for storing nodes in computers.

Troubleshooting :? What is the relationship between the chain structure, spatial relationship, and logical storage? /> The structure of the chain store. The storage space of the data storage structure is discontinuous. The order of the logical relationships between the stored data nodes and data elements may be inconsistent, to determine the logical relationship between data elements, it is a pointer field.


Tree and binary tree and binary test site 7. Its basic attributes

Exam time:

The test site is a mandatory content that may appear in the test 100% mainly based on the form of choice, sometimes in the filling, the score is 2 points, this test center focuses on the content. Focus on the nature of memory and Binary Trees.

Misunderstanding warning:

Full Binary Trees are full binary trees, and full Binary Trees are full Binary Trees. It should be noted that there is a difference between the two.

The basic concept tree (tree) is a simple nonlinear structure. In the tree structure, each node has only one precondition known as the parent node, and only the first node exists, called the root of the tree. Each node can have multiple subnodes called nodes. After some so-called leaf nodes. /> The number of levels in the node tree structure after the node. The leaf node is 0. In the tree, all nodes in the highest level tree are known.

2. Binary Tree and its basic attributes

(1) binary tree Definition

A binary tree is a useful nonlinear structure with the following two features:

(1) A non-null binary tree has only one root;

② The two largest Subtrees of each node are called left and right subtree nodes.

In addition to the above features, in a binary tree, the degree of each node is 2, which is the binary tree of all sub-trees (left or right subtree, the degree of each node in the tree structure can be arbitrary. In addition, each node in the binary subtree is divided into left and right Subtrees. A binary tree. A node can have only the left and right Subtrees, but the right subtree does not have the left subtree. When a node does not have any leaf node of the Left or right subtree.

(2) binary

The basic attributes of a binary tree include the following:

Attribute 1: In the binary coefficient layer, up to 2K-1 (K ≥1) nodes;/> two attributes: m, up to the depth of 2s-1 Binary Tree nodes;

Nature: Any binary system zero node (leaf node) is always two or more nodes to a certain extent.

Attribute 4: N-vertex binary tree. Its depth must be at least an integer portion of [log2n] + [log2n] log2n.

? Tip: in the forward traversal of a binary tree, the traversal of a binary tree does not change.

3. Full binary tree and full Binary Tree

Full binary tree, a binary tree has two subnodes: All nodes in each layer except the last layer. In the Complete Binary Tree, the maximum value of each layer, 2K-1 nodes in A Complete Binary Tree Index layer, and a Complete Binary Tree depth of 2 meters, number of nodes on the node. /> The Complete Binary Tree is a binary tree: apart from the last layer, the number of nodes in each layer is the largest; the last layer of the correct node is lacking.

The leaf node of A Complete Binary Tree may only appear at the highest two levels: Any node. If the descendant of the right branch node p is at the highest level, the left branch node of the descendant or the highest level P or P + 1.

The following two attributes are completely Binary Trees:

Nature: depth of the Complete Binary Tree of n nodes [log2n] +.

6. Nature: Give A Complete Binary Tree with n nodes. Start at the root level (from left to right for each natural number 1, 2 ),..., N is the number of nodes k (k = 1, 2 ,......, n is a number.) The node has the following conclusions:

① If k = 1, the node is the root node, and there is no parent node. If k> 1, the parent node number is INT (K/2 ).

② If 2 K is less than or equal to N, the number of nodes in K is 2 K on the left. Otherwise, the left child of the node (apparently there is no correct subnode ).

③ If the number is 2 K + 1 ≤ N and K is 2 K + 1, the number of right subnodes is correct.

Test Center 8 binary tree traversal

Exam time:

When the probability of evaluation is 30% and the score is written at two points, the reader should be familiar with the result of two traversal results that can be deduced by various Traversal Algorithms.

During the traversal of a binary tree, the left subtree is typically traversed and then the right subtree is traversed. In the left-and right-Rear principle, to access the root node, there are three types: pre-order traversal of binary tree traversal and post-order traversal.

(1) Forward traversal: access the root directory, then traverse the left subtree, and finally traverse the right subtree; and horizontal left and right subtree is the first root directory to be accessed, then, the left subtree is traversed, And the right subtree is traversed.

(2) traverse the left subtree in the forward direction, then access the root node, then traverse the right subtree, traverse the left and right subtree, and then access the root node, finally, the right subtree is traversed.

(3) Post-order traversal: traverses the left subtree, then traverses the right subtree, and finally the access root traverses the left subtree. The right subtree still traverses the left subtree first, then traverse the root node of the last access time of the right subtree.

Troubleshooting :? What is the difference between a tree and a binary tree? /> In the tree, each node is 2, that is, all sub-trees (Left or Right sub-trees), Binary Trees, each node in the tree can be any level.

1.5 Technical Test Center 9-forward search

Exam session:

At a probability of 30%, the score of a choice is usually 2 points. For the test site evaluated in writing, the reader should grasp the sequential search algorithm.

Find the specified element in a given data structure. The element in the first linear table of a linear table starts to be the same as that in the search for a comparable element. If all the elements in the linear form are not the same as those found in the element, the query fails. /> Ordered search can only be used in the following two cases: (1) if it is an ordered storage structure of the unordered list, chained Storage Structure/> linear form, only sequential search is used. It can only be used for the/> (2) or even linear table structure to locate the chain.

Link to the test center 10 test bipartite search:

10 test points, 30% probability assessment, usually appear in two points, evaluation, fill in the number of comparisons to find the gap, the reader should be a specific main binary search algorithm.

The bipartite method is only applicable to sequential storage and is arranged in the list of non-descending order, as follows:

Set an ordered linear table with a length of n to search for elements,

(1) I and linear table;

(2) I and intermediate products have the same value and are successfully found;

(3) If I am less than the middle, find the table on the Internet in the same way in the first half of the year; BR/> (4) if I am greater than the middle, the table is found on the Internet in the second half of the year in the same way.

? Troubleshooting: Is this binary search method suitable for you?

The binary search method is applicable to the unique store order. Is this element ordered in a non-linear order of values from large to small? (That is, from small to large, but allow the values of Adjacent Elements to be equal) list.

This process continues until the search length is successful or a sub-table is 0.

The ordered linear meter length is N. In the worst case, binary search only needs to compare log2n times.

1.6 sequencing technology

Detection Center sorts the 11-to-switch classes

Exam link:

The 11 test sites are relatively difficult. Generally, check the selection opportunity of 30% to evaluate about two different forms. Readers should be familiar with the basic process of several sorting algorithms.

Sorting and quick sorting of foam exchange classes.

(1) Bubble Sorting

First, the two adjacent elements in the form of a scanning line in the header are compared in size. If the preceding elements are greater than the following elements, their next action will exchange the adjacent elements, the final maximum linear form.

Then, scan the linear table from the remaining parts and compare two adjacent elements one by one. The following values are smaller than the previous one, and then they exchange the small forward of two adjacent elements, the lowest linear form at the top last year.

Repeat this process until the remaining linear tables are empty and then sorted in other linear forms.

In the worst case, the Bubble Sorting requires the N (N-1)/2 to be compared.

(2) Fast sorting

The basic idea is: Take any benchmark (usually the first element), sort the elements in a sort sequence, and divide them into two sub-Row Element Sequences, the sorting code sequence of elements in the Code is smaller than or equal to the benchmark component, and the correct class code of the sub-sequence is the basic element that is greater than the sorting code, then, the two sub-sequences that are sorted until the whole sequence is sorted.

? Troubleshooting: What is the average execution time of Bubble sorting and quick sorting?

Bubble Sorting: the average execution time is O (N2), and the quick sorting is the average execution time of O (nlog2n.


For a detailed example, select multiple

The time complexity of the algorithm depends on ______. (Test Site 2)

1) scale issues) initial status of the data to be processed

3) difficult issues 4) and B)

Resolution: not only issues related to the same size, but the time complexity of the algorithm and the size of the input data. The probability range of all possible input data, input data or dataset.

Answer: D)

From the logical data structure, the data structure is divided ______. (Test Center)

One) internal structure and external structure) linear structure and non-linear structure

) A compact, non-tight structure 4) dynamic and static structure

Analysis: A logical structure reflects the relationship between data elements in a linear structure. It is a logical relationship between many-to-many data elements in one or more non-linear structures, so the answer is B ).

Answer: B)

, _______ Basic operations that cannot be superimposed.

A) (Test Center) to determine that the stack element is empty) Stack is set to an empty stack C) Remove the top element 4) unload the bottom stack Element

Analysis: Stack, basic stack operations, top-level elements to be deleted, initialization, and setting to null to determine the top-level element stack that is extracted if the stack is empty or fully put into operation.

Answer: D)

Which of the following are the characteristics of a list _____. (Test site 6)

), But does not move any elements inserted or removed.

3) The length of the required space is directly proportional to the storage space D)

Analysis: Can Sequential tables be randomly accessed? Any node, the first data node in the list, one by one to find each node. Therefore, the answer is)


The post-order traversal sequence is a binary tree's traversal sequence DEBAC called DACBE. Its pre-order traversal sequence is _______. (Test Center)



Resolution: the descending order of the Left subtree-right subtree-root traversal is that the first order of the Left subtree-root-right subtree is "Root-left subtree-right subtree. According to the traversal algorithm, it is not difficult to traverse the sequence EDBAC. Therefore, the answer is D ).

Answer: D)

The value of each element (length greater than 2) and K value are equal to the Elements A and K found in the sequential search method and binary search respectively, compare the number S and number B linear tables for sorting. If the search fails, which of the following values is between S and B _______. (Test Site 9)

) = BB)> BC) <BD) S ≥ B

Analysis: sequential search. The search is unsuccessful. Compare N + 1 for a given keyword. Half of the number of unsuccessful search keywords [log2n] +. When n is greater than or equal to 2, it is clear that N + 1> [log2n] + 1.

Answer: B)

In the quick sorting process, each department divides it into two sub-tables (sub-tables), which are left and right. We can consider two sub-tables and draw the following conclusion, which must be corrected ,______. (11 test sites)
) Two sub-tables on the left and right, each sorted

B) leaving the sub-element table is not greater than the sub-table element on the right.

C) The length of the left sub-table is smaller than that of the right sub-table.

D) The left and right sub-table elements are equal to the average value.

: The basic idea of fast sorting: sorting a table or as a reference element (usually the first element), the rows of the elements sorted by this itinerary are divided into two sub-tables? The child code on the left is the basic element of the element smaller than or equal to the sorting code table. The child table's sorting code is greater than the basic element of the sorting code, and then continues to sort the two child tables, until the entire table command.

Answer: B)

2. Enter

Correct and complete descriptions of troubleshooting are called _______. (Test Center)

Analysis: The computer problem solving process is actually a running algorithm, called a computer algorithm.

Answer: Algorithm

The linear structure processed based on an empty data structure is ______. (Test Center)/> analysis: An Empty data structure is a linear or non-linear structure, depending on the situation. Operations on data structures with linear structures, linear structures, or linear structures. /> A: linear structure/> Number of leaf nodes with tree T, node 2, 3, and node 4, respectively, which are, and T is called _______. (Test site 7)

Resolution: The nature of a tree is different: the tree node is equal to all nodes of the corresponding digital product.

1x4 2x2 3X1 4x1 + 1 = 16 nodes in the tree. The total number of leaf nodes in the tree is equal to that of the node minus 0, which is 16-(4 + 2 + 1 + 1 + 1) = 8.

Answer: 8

The storage structure of binary lookup is limited _______ and ordered. (Test Center)

Resolution: Binary Search, also known as binary search, is an efficient search method. But there are two search criteria: You must order the storage structure of the table and the table element based on the order of keywords (ascending or descending ).
Answer: sequential Storage Structure

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: 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.