/*************************************** * ******************************* // 10. in the array, find the number of times a given number appears *//******************************* **************************************** * /// binary search, binary Search for the location where the key appears for the first time, binary search for the key that appears for the last time // returns the subtraction of the two + 1 or find the location where the first appears, Backward Search for int binarysearchfirstpos (int * iarr, int L, int H, int key) {While (L <= h) {int mid = (L + H)/2; If (iarr [Mid] <key) L = Mid + 1; else if (iarr [Mid]> key) H = mid-1; Else {If (mid = L | iarr [Mid-1]! = Key) return mid; else h = mid-1 ;}} return-1 ;}int binarysearchlastpos (int * iarr, int L, int H, int key) {While (L <= h) {int mid = (L + H)/2; If (iarr [Mid] <key) L = Mid + 1; else if (iarr [Mid]> key) H = mid-1; else {If (mid = H | iarr [Mid + 1]! = Key) return mid; elsel = Mid + 1 ;}} return-1 ;}int numofkey (int * iarr, int length, int key) {int firstpos = binarysearchfirstpos (iarr, 0, length-1, key); int lastpos = binarysearchlastpos (iarr, 0, length-1, key ); cout <firstpos <"\ t" <lastpos <Endl; If (firstpos =-1) return 0; elsereturn lastpos-firstpos + 1 ;}