Number of occurrences of a number in a sorted array

Source: Internet
Author: User
Tags include integer first row

Topic Description:

Counts the number of occurrences of a number in a sorted array.

Input:

Each test case consists of two lines:

The first row has 1 integer n, which represents the size of the array. 1<=n <= 10^6.

The second row has n integers, representing the array elements, each of which is int.

The third row has 1 integer m, which indicates that there is a M-second query next. 1<=m<=10^3.

There are m lines, each with an integer k, representing the number to query.

Output:

For each test case, there is an M row output, 1 integers per line, representing the number of times the number appears in the array.

Sample input:

8

1 2 3 3 3 3 4 5

1

3

Sample output:

4

I do this problem, is to use a binary search to find the number, and then iterate over the same number, the number of statistics. The average time complexity of this practice is O (logn), the worst case is O (n), the sword refers to an offer on the train of thought is two times with a binary search to find the number of the first and last occurrence of the position, such as the time complexity of the average and the worst is O (Logn), slightly better "

Here's the code I wrote in My own mind:

#include <stdio.h> #include <stdlib.h>/* Two-point lookup method to calculate the number of key occurrences this column more highlights: http://www.bianceng.cnhttp://w  
        ww.bianceng.cn/programming/sjjg/*/int counttimesinarrays (int *arr,int len,int key) {if (Arr==null | | len<1)  
      
    return 0;  
    int start = 0;  
    int end = Len-1;  
    int mid;  
        while (start <= end) {mid = (start+end) >>1;  
        if (arr[mid] = = key) break;  
        else if (Arr[mid] > key) end = Mid-1;  
    else start = mid+1;  
    //contains 0 occurrences of int times = 0;  
        if (start <= end) {int i;  
        times = 1;  
        for (i=mid+1;i<=end;i++) if (arr[i] = = key) times++;  
    for (i=mid-1;i>=start;i--) if (arr[i] = = key) times++;  
return times;  
    int main () {int n;  while (scanf ("%d", &n)!= EOF)
    {int *arr = (int *) malloc (n*sizeof (int));  
      
        if (arr = NULL) exit (exit_failure);  
        int i;  
        for (i=0;i<n;i++) scanf ("%d", arr+i);  
        int m;  
        scanf ("%d", &m);  
            for (i=0;i<m;i++) {int k;  
            scanf ("%d", &k);  
        printf ("%d\n", Counttimesinarrays (arr,n,k));  
        Free (arr);  
    arr = NULL;  
return 0; }

Author: csdn Blog Lan pavilion Wind and Rain

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.