fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int first_occurance(vector<int>&v,int j,int n){
  5. int l=0;
  6. int h=n-1;
  7. int ans=0;
  8. while(l<=h){
  9. int mid=(l+h)/2;
  10. if(v[mid]>=j){
  11. ans=mid;
  12. h=mid-1;
  13. }
  14. else{
  15. l=mid+1;
  16. }
  17. }
  18. return ans;
  19. }
  20.  
  21. int last_occurance(vector<int>&v,int j,int n){
  22. int l=0;
  23. int h=n-1;
  24. int ans=0;
  25. while(l<=h){
  26. int mid=(l+h)/2;
  27. if(v[mid]>j){
  28. h=mid-1;
  29. }
  30. else{
  31. l=mid+1;
  32. ans=mid;
  33. }
  34. }
  35. return ans;
  36. }
  37.  
  38.  
  39. int main(){
  40. vector<int>v={2,2,2,3,3,4,4,4,5,5,5};
  41. int n=11;
  42. int k=3;
  43.  
  44.  
  45. int l=0,h=10;
  46. while(l<h){
  47. int mid=(l+h)/2;
  48.  
  49. int l1=first_occurance(v,v[mid],v.size());
  50. int h1=last_occurance(v,v[mid],v.size());
  51.  
  52. if(h1-l1+1<k){
  53. cout<<v[mid]<<endl;
  54. return 0;
  55. }
  56. else{
  57. if((h1+1)%k==0){
  58. l=mid+1;
  59. }
  60. else{
  61. h=mid-1;
  62. }
  63. }
  64.  
  65. }
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
3