Number of times a number appears in the sorting Array

Source: Internet
Author: User

Description:
Count the number of times a number appears in the SORT array.
Input:

Each test case contains two rows:

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

The second row has n integers, indicating the array elements. Each element is an int.

The third row has an integer m, which indicates that there are m queries next. 1 <= m <= 10 ^ 3.

There are m rows below, each row has an integer k, indicating the number of queries.

Output:
Corresponding to each test case, there is m row output, each line is an integer, indicating the number of times this number appears in the array.

Sample input:
81 2 3 3 3 4 513
Sample output:
4
Thought: the question is about sorting arrays, So we naturally think of binary lookup. Solution 1: First, find the number by means of binary search, and then traverse the numbers from left to right based on the position of the number. Then, find all the identical numbers and sum them.

Code:

/* Number of times the number appears in the sorting array by rowandjj2014/8/17 */# include <stdio. h> # include <stdlib. h> int gettimes (INT arr [], int Len, int target) {If (ARR = NULL | Len <= 0) {return 0;} int low = 0, high = len-1; while (low <= high) {int mid = low + (high-low)> 1); If (ARR [Mid] = target) {int COUNT = 1; int I; for (I = mid-1; I> = 0; I --) {If (ARR [I]! = Target) {break;} count ++;} for (I = Mid + 1; I <= Len; I ++) {If (ARR [I]! = Target) {break;} count ++;} return count;} else if (ARR [Mid]> Target) {high = mid-1 ;} else {LOW = Mid + 1 ;}} return 0 ;}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 ", gettimes (ARR, n, k);} Free (ARR); arr = NULL;} return 0 ;}

Solution 2: in fact, binary search can not only locate a number, but also locate the first occurrence and the last occurrence. For example, if we want to locate the first occurrence of K, first, compare the number in the middle of the array with K. If the number in the middle is larger than K, K will only appear in the first half, in the next round, we can only find the First Half of the array. If the number in the middle is smaller than K, K may only appear in the second half of the array, in the next round, we can only find the number in the second half of the array. If the number in the middle is equal to K, we first determine whether the number is the first K, if the first digit in the middle is K, that is to say, the first K must be in the first half of the array. We still need to find the first half of the array in the next round, if the first digit in the middle is not K, it indicates that the current K is the first occurrence. Find the last occurrence location similar to this one. Code:
# Include <stdio. h> # include <stdlib. h> int getfirstn (INT arr [], int Len, int target) {If (ARR = NULL | Len <= 0) {return-1 ;} int low = 0, high = len-1; while (low <= high) {int mid = low + (high-low)> 1 ); if (ARR [Mid] = target) {If (mid = 0 | arr [Mid-1]! = Target) {return mid;} else {high = mid-1 ;}} else if (ARR [Mid]> Target) {high = mid-1 ;} else {LOW = Mid + 1 ;}} return-1 ;}int getlastn (INT arr [], int Len, int target) {If (ARR = NULL | Len <= 0) {return-1;} int low = 0, high = len-1; while (low <= high) {int mid = low + (high-low)> 1); If (ARR [Mid] = target) {If (mid = len-1 | arr [Mid + 1]! = Target) {return mid;} else {LOW = Mid + 1 ;}} else if (ARR [Mid]> Target) {high = mid-1 ;} else {LOW = Mid + 1 ;}} return-1 ;}int gettimes (INT arr [], int Len, int target) {If (ARR = NULL | Len <= 0) {return 0;} int T1 = getfirstn (ARR, Len, target); int t2 = getlastn (ARR, Len, target); If (T1! =-1 & T2! =-1) {return t2-t1 + 1;} else {return 0 ;}} int main () {int N; while (scanf ("% d", & N )! = EOF) {int * arr = (int *) malloc (N * sizeof (INT); If (ARR = NULL) Exit (-1); 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 ", gettimes (ARR, n, k);} Free (ARR); arr = NULL;} return 0 ;}




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.