[Classic Interview Questions] Statistical array, classic exam statistical Array

Source: Internet
Author: User

[Classic Interview Questions] Statistical array, classic exam statistical Array
[Question]

Given array A, the size is n, and the array element is A number ranging from 1 to n. However, some numbers appear multiple times, while some do not. Provide algorithms and programs to calculate which numbers do not appear and which ones appear for how many times. Can it be completed with O (n) time complexity and O (1) space complexity requirements?

[Analysis]

We know that the original array is not sorted. If it is sorted, it is very simple. The space meaning of O (1) can use variables, but it cannot open up arrays or map to count.

The direct solution to this question is two-layer traversal, the complexity of O (n ^ 2) and the space of O (1. The space is sufficient, but the time is not.

Many similar questions use XOR. Can you think about this question carefully? Or what is the difference between this question and the question that can be used XOR? The most direct one is that the number of repetitions for each number is different.

Another method is to change the space time, for example, to count with hash map or array. The time is enough, but the space is not enough.

So how can we have the time complexity O (n) and space complexity O (1) algorithms? There is no way to open up new space, so there is only one left, and array A is reused. So how can we use array?

(1) first, we will introduce a method for Traversing arrays three times. We will consider the array starting from 0:

First traversal: For each A [I] = A [I] * n
The second traversal: For each I, A [A [I]/n] ++
The third traversal: For each I, A [I] % n indicates the number of occurrences.

A [I] should appear at the position of A [I] In A, multiplied by n and divided by n, which can easily be converted back and forth. The second traversal, for the position where A [I] is originally located is increasing by 1, but it is definitely not greater than n, then the number of times each I appears is that A [I] returns n.

(2) There is also a method of traversing twice, which is also the above idea: the array in the question is 1 to n. To facilitate algorithm consideration and the convenience of array storage, we consider 0-n-1, the results are the same. Considering A [I], the current position is I. If we use A to count, it should be A [I] % n. How can we deal with this position to find the counting position? Add 1? Obviously not. Here is a trick, that is, adding n for two reasons.

Adding n ensures that A [I] % n remains unchanged.
In array A, each element is represented as A [I] = x + k * n, where x <n, and k is the frequency of statistics.

In this way, you can understand why the position of A [I] In A is expressed as A [I] % n.

[Code]

# Include <iostream> # include <malloc. h> # include <stdio. h> using namespace std; // count the number of repeated elements in the array void ArrayNumCount (int array [], int n) {int I; if (n <= 0 | array = NULL) {return ;}for (I = 0; I <n; I ++) {array [I] * = n ;}for (I = 0; I <n; I ++) {array [array [I]/n] ++ ;} for (I = 0; I <n; I ++) {array [I] % = n ;} // output // array [0] The number of int elements whose storage value is n; for (I = 0; I <n; I ++) {if (I = 0) {element = n ;}else {element = I;} printf ("Element: % d Count: % d \ n", element, array [I]);} int main () {int array [] = {2, 3, 1, 4, 3, 5, 8}; ArrayNumCount (array, 10); return 0 ;}


Code 2:

# Include <iostream> # include <malloc. h> # include <stdio. h> using namespace std; // count the number of repeated elements in the array void ArrayNumCount (int array [], int n) {int I; if (n <= 0 | array = NULL) {return ;}for (I = 0; I <n; I ++) {array [array [I] % n] + = n;} // output // array [0] The number of int elements whose storage value is n, count; for (I = 0; I <n; I ++) {count = array [I]/n; element = I; if (I = 0) {element = n;} printf ("Element: % d Count: % d \ n", element, count) ;}} int main () {int array [] = {,}; ArrayNumCount (array, 11); return 0 ;}




Questions about the array of C ++ interview questions

0
 
To solve a C language question, use arrays and pointers to calculate the scores of all subjects in a given class.

The following method is used to calculate students' scores using pointer variables. If there are any nonconformities that do not fully comply with the requirements, ask the students to think about the program and modify it. # Include <stdio. h> void main ()
{Void average (float * p, int n );
Void search (float (* p) [4], int n );
Float score [3] [4] = {65,67, 70,60}, {80, 87, 90,81}, {90,99, 100,98}; // assume that three students have questions, each student has four courses.
Average (* score, 12); // calculate the total average score of 12 scores.
Search (score, 2); // score of the student whose serial number is 2
}
Void average (float * p, int n)
{Float * p_end; float sum = 0, aver; p_end = p + n-1;
For (; p <= p_end; p ++) sum = sum + (* p); aver = sum/n;
Printf ("average = % 5.2f \ n", aver );
}
Void search (float (* p) [4], int n) // p is a pointer to a one-dimensional array with four elements
{Int I; printf ("the score of No. % d are: \ n", n );
For (I = 0; I <4; I ++) printf ("% 6.1f", * (p + n) + I ));}
 

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.