Advertisement
imashutosh51

Number of occurrence of x in array

Jul 31st, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. /*
  2. Lower bound gives the address of first x exist in array or if doesn't exist
  3. then address of next element just greater than x.if next greater element element
  4. is not present in array,then address just after the array.
  5.  
  6. Upper bound gives the address of just greater element than x.if not present in array,
  7. just next element after the end of array.
  8. */
  9. class Solution{
  10. public:
  11.     /* if x is present in arr[] then returns the count
  12.         of occurrences of x, otherwise returns 0. */
  13.     int count(int arr[], int n, int x) {
  14.          int *low = lower_bound(arr, arr+n, x);  
  15.  
  16.           // If element is not present, return 0
  17.           if (low == (arr + n) || *low != x)
  18.              return 0;
  19.              
  20.           /* Else get the index of last occurrence of x.
  21.              Note that we  are only looking in the
  22.              subarray after first occurrence */  
  23.           int *high = upper_bound(low, arr+n, x);    
  24.              
  25.           return high - low;
  26.     }
  27. };
  28. /*
  29. //Manual code of upper bound and lower bound of array
  30. int lower_bound(int arr[], int N, int X){
  31.     int mid;
  32.     // Initialise starting index and
  33.     // ending index
  34.     int low = 0;
  35.     int high = N;
  36.  
  37.     // Till low is less than high
  38.     while (low < high) {
  39.         mid = low + (high - low) / 2;
  40.  
  41.         // If X is less than or equal
  42.         // to arr[mid], then find in
  43.         // left subarray
  44.         if (X <= arr[mid]) {
  45.             high = mid;
  46.         }
  47.  
  48.         // If X is greater arr[mid]
  49.         // then find in right subarray
  50.         else {
  51.             low = mid + 1;
  52.         }
  53.     }
  54.    
  55.     // if X is greater than arr[n-1]
  56.     if(low < N && arr[low] < X) {
  57.        low++;
  58.     }
  59.        
  60.     // Return the lower_bound index
  61.     return low;
  62. }
  63.  
  64. // Function to implement upper_bound
  65. int upper_bound(int arr[], int N, int X)
  66. {
  67.     int mid;
  68.  
  69.     // Initialise starting index and
  70.     // ending index
  71.     int low = 0;
  72.     int high = N;
  73.  
  74.     // Till low is less than high
  75.     while (low < high) {
  76.         // Find the middle index
  77.         mid = low + (high - low) / 2;
  78.  
  79.         // If X is greater than or equal
  80.         // to arr[mid] then find
  81.         // in right subarray
  82.         if (X >= arr[mid]) {
  83.             low = mid + 1;
  84.         }
  85.  
  86.         // If X is less than arr[mid]
  87.         // then find in left subarray
  88.         else {
  89.             high = mid;
  90.         }
  91.     }
  92.    
  93.     // if X is greater than arr[n-1]
  94.     if(low < N && arr[low] <= X) {
  95.        low++;
  96.     }
  97.  
  98.     // Return the upper_bound index
  99.     return low;
  100. }
  101. */
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement