Array statistics of interview questions

Source: Internet
Author: User

Question: Given array A, the size is n, and the array element is A number ranging from 0 to n-1. 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. It must be completed with the time complexity of O (n) and the space complexity of O (1. Solution 1: directly traverse through two layers, the time complexity of O (n ^ 2) and the space complexity of O (1)

#include <stdio.h>  #include <stdlib.h>    int main()  {      int n, i, j, count = 0;     //n is The length of the Array      while (scanf("%d", &n) != EOF)      {             int *a = malloc(sizeof(int) * n);           for (i = 0; i < n; i++)              scanf("%d", &a[i]);            for (i = 0; i < n; i++)          {                 count = 0;              for (j = 0; j < n; j++)              {                     if (i == a[j])                  {                         count++;                  }                 }                 if (count == 0)                  printf("%d does not appear in the array!\n", i);               else                  printf("%d appear in the array for %d times\n", i, count);          }         }     }  

 

Solution 2: Use a map array to store the element count by changing the space time. The time complexity is O (n), and the space complexity is O (n)
#include <stdio.h>  #include <stdlib.h>  #include <string.h>    int main()  {      int n, i, j, count = 0;     //n is The length of the Array      while (scanf("%d", &n) != EOF)      {             int *a = malloc(sizeof(int) * n);           int *map = malloc(sizeof(int) *n);          memset(map, 0, sizeof(map));            for (i = 0; i < n; i++)              scanf("%d", &a[i]);            for (i = 0; i < n; i++)          {                 map[a[i]]++;          }               for (i = 0; i < n; i++)          {                 if (map[i] == 0)                  printf("%d does not appear in the array!\n", i);               else                  printf("%d appear in the array for %d times\n", i, map[i]);          }         }     }  

 

However, none of the above solutions meet the time and space complexity requirements of the questions. Therefore, we would like to reuse array. Solution 3: three-way traversal of the array: First traversal: For each A [I] = A [I] * n Second traversal: For each I, A [A [I]/n] ++'s third traversal: For each I, A [I] % n is the number of times I appears. Explanation: A [I] should appear at the position of A [I] In A, multiplied by n and divided by n, which is easy to change back and forth. The second traversal, for A [I], the original position increases by 1, but it never exceeds n. Then, the number of times each I occurs is that A [I] returns n.
#include <stdio.h>  #include <stdlib.h>    int main()  {      int n, i;       //n is The length of the Array      while (scanf("%d", &n) != EOF)      {             int *a = malloc(sizeof(int) * n);           for (i = 0; i < n; i++)              scanf("%d", &a[i]);            for (i = 0; i < n; i++)              a[i] = a[i] * n;            for (i = 0; i < n; i++)              a[a[i]/n]++;            for (i = 0; i < n; i++)          {                 if (a[i] % n == 0)                  printf("%d does not appear in the array!\n", i);               else                  printf("%d appear in the array for %d times\n", i, a[i]%n);          }         }     }  

 

Solution 4: The method of traversing the array twice: consider A [I]. The current position is I. If A is used for counting, its position should be A [I] % n, find the counting position, and add n to this counting position. first traversal: add n to the computing position of A [I], and add n to ensure that A [I] % n is A constant second traversal: array, each element is represented as A [I] = x + k * n. x <n, and k is the frequency of statistics.
#include <stdio.h>  #include <stdlib.h>    int main()  {      int n, i;       //n is The length of the Array      while (scanf("%d", &n) != EOF)      {             int *a = malloc(sizeof(int) * n);           for (i = 0; i < n; i++)              scanf("%d", &a[i]);            for (i = 0; i < n; i++)              a[a[i] % n] += n;            for (i = 0; i < n; i++)          {                 if (a[i] / n == 0)                  printf("%d does not appear in the array!\n", i);               else                  printf("%d appear in the array for %d times\n", i, a[i]/n);          }         }     }  

 

 

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.