fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int countBinarySearchableIndex(int Arr[], int l, int r,
  7.  
  8. int LR, int RL)
  9. {
  10.  
  11. // Invalid indexes
  12.  
  13. if (l > r)
  14.  
  15. return 0;
  16.  
  17. int ans = 0;
  18.  
  19.  
  20. // Finding the middle element of the current array
  21.  
  22. // (arr[l], ... arr[r]) Similar to as we do in binary
  23.  
  24. // search
  25.  
  26. int m = (l + r) / 2;
  27.  
  28.  
  29. // If these conditions follow that means Arr[m] is
  30.  
  31. // binary searchable.
  32.  
  33. //cout<<LR<<" "<<m<<" "<<Arr[m]<<" "<<RL<<endl;
  34.  
  35. if (LR < Arr[m] && Arr[m] < RL) {
  36.  
  37. cout<<Arr[m]<<endl;
  38.  
  39. ans = 1;
  40. }
  41.  
  42.  
  43. // Finding the binary searchable elements to the left of
  44.  
  45. // middle(m) element
  46.  
  47. int l_ans = countBinarySearchableIndex(
  48.  
  49. Arr, l, m - 1, LR, min(RL, Arr[m]));
  50.  
  51.  
  52. // Finding the binary searchable elements to the right
  53.  
  54. // of middle(m) element
  55.  
  56. int r_ans = countBinarySearchableIndex(
  57.  
  58. Arr, m + 1, r, max(LR, Arr[m]), RL);
  59.  
  60.  
  61. return ans + l_ans + r_ans;
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67.  
  68. int Arr[] = { 2, 3, 1, 5, 8, 7, 9 };
  69.  
  70. int n = 7;
  71.  
  72. cout << "Number of Binary Searchable Indexes: ";
  73.  
  74. cout << countBinarySearchableIndex(Arr, 0, n - 1, -1e9,
  75.  
  76. 1e9)
  77.  
  78. << endl;
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Number of Binary Searchable Indexes: 5
3
2
7
9
5