Advertisement
yarin0600

Untitled

Nov 15th, 2023
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. // Find the index of first 1 in a sorted array of 0’s and 1’s
  2.  
  3. /**
  4.  * Given a sorted array consisting 0’s and 1’s. The problem is to find the index of first ‘1’ in the sorted array.
  5.  * It could be possible that the array consists of only 0’s or only 1’s. If 1’s are not present in the array then print “-1”.
  6.  *
  7.  * Examples:
  8.  *    Input : arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
  9.  *    Output : 6
  10.  *    Explanation: The index of first 1 in the array is 6.
  11.  
  12.  *    Input : arr[] = {0, 0, 0, 0}
  13.  *    Output : -1
  14.  *    Explanation: 1’s are not present in the array.
  15.  *
  16.  * Source: Asked in Amazon Interview
  17.  */
  18.  
  19. #include <iostream>
  20. #include <chrono>
  21.  
  22. int firstIdxOfFirstOne(int *arr, int n);
  23. int firstIdxOfFirstOne2(int *arr, int n);
  24.  
  25. int main()
  26. {
  27.    int arr[1000000]{};
  28.    arr[999999] = 1;
  29.  
  30.    int n = sizeof(arr) / sizeof(*arr);
  31.    auto start = std::chrono::steady_clock::now();
  32.    printf("%d\n", firstIdxOfFirstOne(arr, n));
  33.    // printf("%d\n", firstIdxOfFirstOne2(arr, n));
  34.    auto end = std::chrono::steady_clock::now();
  35.    auto totalDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  36.    std::cout << "Operation took: " << totalDuration << " miliseconds\n";
  37. }
  38.  
  39. int firstIdxOfFirstOne(int *arr, int n)
  40. {
  41.    if (arr[0] == 1)
  42.       return 0;
  43.  
  44.    int i = 1;
  45.    for (; i < n && arr[i] == 0; i *= 2)
  46.    {
  47.    }
  48.  
  49.    int left = (i / 2) + 1;
  50.    int right = i >= n ? n - 1 : i;
  51.    int middle = -1;
  52.    while (left <= right)
  53.    {
  54.       middle = (right - left) / 2 + left;
  55.       if (arr[middle] == 1 && arr[middle - 1] == 0)
  56.          return middle;
  57.       else if (arr[middle] == 0)
  58.          left = middle + 1;
  59.       else
  60.          right = middle - 1;
  61.    }
  62.    return -1;
  63. }
  64.  
  65. int firstIdxOfFirstOne2(int *arr, int n)
  66. {
  67.    if (arr[0] == 1)
  68.       return 0;
  69.  
  70.    int left = 0;
  71.    int right = n - 1;
  72.    int middle = -1;
  73.  
  74.    while (left <= right)
  75.    {
  76.       middle = (right - left) / 2 + left;
  77.       if (arr[middle] == 1 && arr[middle - 1] == 0)
  78.          return middle;
  79.       else if (arr[middle] == 0)
  80.          left = middle + 1;
  81.       else
  82.          right = middle - 1;
  83.    }
  84.    return -1;
  85. }
  86.  
  87. /*
  88. if (arr[0] == 1)
  89.       return 0;
  90.  
  91.    int left, right, mid;
  92.    left = 0, right = n - 1;
  93.    while (left <= right)
  94.    {
  95.       mid = (right - left) / 2 + left;
  96.  
  97.       if (arr[mid] == 1 && arr[mid - 1] == 0)
  98.          return mid;
  99.       else if (arr[mid] == 0)
  100.          left = mid + 1;
  101.       else
  102.          right = mid - 1;
  103.    }
  104.    return -1;
  105.  
  106. */
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement