Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  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,high = size-1;
  24.    
  25.     while (low <= high)
  26.     {
  27.         mid = (high + 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.             high = mid -1;
  38.        
  39.     }
  40.    
  41.    
  42.     return -1;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement