InterviewSolution
Saved Bookmarks
| 1. |
Implement a function that receives an array of integers "arr" and an integer "int", which returns the number of occurrences of element "int" in array "arr". For instance, given arr = [2,3,4,3,2,1] and int=3, the function should return 2. |
|
Answer» Given a sorted ARRAY arr[] and a number x, WRITE a function that counts the occurrences of x in arr[]. EXPECTED time complexity is O(LOGN)Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 TIMES in arr[] Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 3 Output: 1 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 1 Output: 2 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 4 Output: -1 // 4 doesn't occur in arr[] |
|