Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Naveen-Shah01/6e77f34049f32472365dce079095aa61 to your computer and use it in GitHub Desktop.

Select an option

Save Naveen-Shah01/6e77f34049f32472365dce079095aa61 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
int position(int arr[],int n,int target)
{ int start=0;int end=n-1;
while(start<=end)
{ int mid=start+(end-start)/2;
if(arr[mid]==target)
return mid;
else if(arr[mid]>target)
end=mid-1;
else
start=mid+1;
}
return start;
}
void missingFromRange(int arr[], int n, int a, int b){ // n is size of array and range from a to b.
sort(arr, arr + n); // Sort the array
int* position = lower_bound(arr, arr + n, a); // finding index of a
int index = position - arr;
// int index = position(arr,n,a); //uncomment this and comment above two lines to use binary search
int i = index, j = a;
while (i < n && j <= b) { // after finding index, traversing our array.
if (arr[i] != j) // If range elements doesn't matches with current element
cout << j << " ";
else
i++;
j++; // Move to next element in range [a, b]
}
while (j <= b) //printing remaining elements of the range that are not in array
cout << j++ << " ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment