Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given a sorted integer array of length n with possible duplicate elements. Find the number of occurrences of an integer k using binary search.
  3.  
  4. Input format
  5. There are 2 lines of input.
  6.  
  7. First line contains 2 space separated integers n, k - Number of elements, the integer k.
  8.  
  9. Second line contains n space separated integers - The integer array.
  10.  
  11. Output format
  12. Print the number of occurrences of the integer k.
  13.  
  14. Sample Input 1
  15. 5 2
  16.  
  17. -1 2 2 4 7
  18.  
  19. Sample Output 1
  20. 2
  21.  
  22. Explanation 1
  23. The integer 2 occurs 2 times in the given array.
  24.  
  25. Constraints
  26. 1 <= n <= 10^6
  27.  
  28. -10^9 <= k <= 10^9
  29.  
  30. -10^9 <= A[i] <= 10^9
  31. */
  32.  
  33. /**
  34.  * @param {number} n
  35.  * @param {number} target
  36.  * @param {number[]} arr
  37.  * @return {number}
  38.  */
  39. function countOccurrences(n, target, arr) {
  40.     //  let n = arr.length;
  41.       let low=0;
  42.       let high=n-1;
  43.       let foundIndexOfTarget=-1;
  44.       let startingIndex=-1;
  45.       let endingIndex=-1;
  46.       while(low<=high){
  47.             let mid=Math.floor((low+high)/2);
  48.             if(target==arr[mid]){
  49.                   foundIndexOfTarget=mid;
  50.                   break;
  51.             }
  52.             else if(target>arr[mid]){
  53.                   low=mid+1;
  54.             }
  55.             else{
  56.                   high=mid-1;
  57.             }
  58.       }
  59.       // console.log(`foundIndexOfTarget ${foundIndexOfTarget}`);
  60.       if(foundIndexOfTarget==-1)
  61.       return 0;
  62.  
  63.       // getting startting Left index
  64.       low=0;
  65.       high=foundIndexOfTarget;
  66.       while(low<=high){
  67.             let mid=Math.floor((low+high)/2);
  68.             // condition
  69.             if((mid!=0&&arr[mid]==target&&arr[mid]>arr[mid-1])
  70.             ||(mid==0&&arr[mid]==target)
  71.             ){
  72.                   startingIndex=mid;
  73.                   break;
  74.             }
  75.             if(arr[mid]<target){
  76.                   low=mid+1;
  77.             }
  78.             else{
  79.                   high=mid-1;
  80.             }
  81.       }
  82.  
  83.       // getting endingIndex
  84.       low=foundIndexOfTarget;
  85.       high=n-1;
  86.       while(low<=high){
  87.             let mid=Math.floor((low+high)/2);
  88.             if((mid!=n-1&&arr[mid]==target&&arr[mid]<arr[mid+1])
  89.             ||(mid==n-1&&arr[mid]==target)
  90.             ){
  91.                   endingIndex=mid;
  92.                   break;
  93.             }
  94.             if(target<arr[mid]){
  95.                   high=mid-1;
  96.             }
  97.             else{
  98.                   low=mid+1;
  99.             }
  100.  
  101.       }
  102.  
  103.  
  104.       return endingIndex-startingIndex+1;
  105.  
  106. }
  107.  
  108. function main() {
  109.     let [n, k] = readIntArr();
  110.     let arr = readIntArr();
  111.  
  112.     let ans = countOccurrences(n, k, arr);
  113.     print(ans);
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement