SHARE
TWEET

Untitled

a guest Feb 19th, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int test(int* arr,int size);
  6.  
  7. void main()
  8. {
  9.    
  10. //    int arr[] = {-3,-2,-1,0,1,2,3,3,4,5,6,7,8,9};
  11. //     int arr[] = {0,0,1,2,3,4,5,6,7,8,9,10,11,12};
  12.     int arr[] ={-3,-2,-1,0,1,2,3,4,5,6,7,8,8};
  13.  
  14.  
  15.    
  16.     printf("%d \n",test(arr, 14));
  17.    
  18. }
  19.  
  20.  
  21. int test(int* arr,int size)
  22. {
  23.     int mid,low =0,hidg = size-1;
  24.    
  25.     while (low <= hidg)
  26.     {
  27.         mid = (hidg + low) / 2;
  28.        
  29.         if (arr[mid] == arr[mid +1] || arr[mid] == arr[mid -1] )
  30.         {
  31.             return arr[mid];
  32.         }
  33.        
  34.         if (arr[mid] - arr[0] == mid)
  35.             low = mid +1;
  36.         else
  37.             hidg = mid -1;
  38.        
  39.     }
  40.    
  41.    
  42.     return -1;
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top