Missing element of AP | Practice | GeeksforGeeks

Find the missing element from an ordered array arr[], consisting of N elements representing an Arithmetic Progression(AP).
Note: There always exists an element which upon inserting into sequence forms Arithmetic progression.

Example 1:
Input:
N = 6
Arr[] = {2, 4, 8, 10, 12, 14}
Output: 6
Explanation: Actual AP should be 
2, 4, 6, 8, 10, 12, 14.


Example 2:
Input:
N = 6
Arr[] = {1, 6, 11, 16, 21, 31}
Output: 26
Explanation: Actual AP should be 
1, 6, 11, 16, 21, 26, 31.

Your Task:
You don't need to read input or print anything. Your task is to complete the function findMissing() which takes the array of integers arr[] and its size n as input parameters and returns an integer denoting the answer.
 
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)

Constraints:
2 <= N <= 105
0 <= |Arr[i]| <= 107
There will always be only one missing element.
0
gupta2411sumit2 months ago
 int findMissing(int arr[], int n) {       // code here              if(n==2)       {           return (arr[0]+arr[1])/2 ;       }              int d1 = arr[1] - arr[0]  ;       int d2 = arr[2] - arr[1] ;       int d ;              if(d1==d2)       {           d = d1 ;       }       else{           d = arr[n-1] - arr[n-2]  ;       }                     for( int i = 1 ; i<n ; i++)       {           if( arr[i]-arr[i-1] != d)           {               return (arr[i-1] + d) ;            }       }   }
0
pragati233 months ago
C++
class Solution{   
public:
    int findMissing(int arr[], int n) {
        int d=(arr[n-1]- arr[0])/(n);
        for(int i=0; i<n; i++){
            int element= arr[0]+ i*d;
            if(element!=arr[i]){
                return element;
            }
        }
    }
};
0
rajnish243 months ago
class Solution {   int findMissing(int[] arr, int n) {       // code here       int d1 = arr[1]-arr[0];       int d2 = arr[n-1]-arr[n-2];       int d = Math.min(d1,d2);       if(n==2)       {           d=(arr[0]+arr[1])/2;           return d;       }       int a=arr[0];       for(int i=0; i<n; i++)       {           if(arr[i]==a)           {               a=a+d;           }           else           {               break;           }       }       return a;          }}
0
ankitparashxr3 months ago
java
if(n==2)       {           return (arr[0]+arr[1])/2;       }              int d = Math.min(arr[1]-arr[0],arr[2]-arr[1]);              for(int i=0;i<n-1;i++)       {           if(arr[i]+d != arr[i+1])           {               return arr[i]+d;           }       }       return 0;
0
chessnoobdj4 months ago
C++
int findMissing(int arr[], int n) {
        if(n == 2)
            return (arr[0]+arr[1])/2;
        int d = min(arr[1]-arr[0],arr[2]-arr[1]);
        int left=0, right=n-1, mid, ans;
        while(left <= right) {
            mid = left+(right-left)/2;
            int term = arr[0]+(mid)*d;
            if(arr[mid] != term) {
                ans = mid;
                right = mid-1;
            }
            else
                left = mid+1;
        }
        return arr[0]+(ans*d);
    }
0
VickySam19017 months ago
VickySam1901
Time Complexity: O(logN)Auxiliary Space: O(1) https://uploads.disquscdn.c...
0
Din Djarin9 months ago
Din Djarin
int findMissing(int arr[], int n) {        if( n == 2 )            return (arr[0] + arr[1])/2 ;
        int d = ( arr[n-1] - arr[n-2] ) < ( arr[1] - arr[0] ) ? ( arr[n-1] - arr[n-2] ) : ( arr[1] - arr[0] ) ;        int a = arr[0] ;        int count = 0 ;        for ( int i = 0 ; i < n ; i++ )        {            if( arr[i] != ( a + i*d ) )                return ( a + i*d ) ;        }
        return -1 ;    }
0
Rajat Chauhan10 months ago
Rajat Chauhan
int findMissing(int arr[], int n) {
        int d = (arr[n-1] - arr[0])/n;        int l = 0, r = n-1, mid = 0;
        while(l <= r)        {            mid = l + (r - l)/2;
            if(arr[mid] > (arr[0] + mid * d) && d > 0)               r = mid - 1;            else if(arr[mid] < (arr[0] + mid * d) && d < 0)                    r = mid - 1;                 else                    l = mid + 1;        }
        if(arr[mid] > (arr[0] + mid*d) && d>0 || arr[mid] < (arr[0] + mid * d) && d < 0)           return arr[0] + mid * d;        else           return arr[0] + (mid + 1) * d;    }//
0
Rajat Chauhan
This comment was deleted.
0
Aniket patel11 months ago
Aniket patel
class Solution{public:    int findMissing(int arr[], int n)     {        int d=(arr[n-1]-arr[0])/n,hi=n-1,lo=0,mid;        while(hi-lo>1)        {            mid=lo+(hi-lo)/2;            int R=(hi-mid)*d,L=(mid-lo)*d;            int l=arr[mid]-arr[lo],r=arr[hi]-arr[mid];
            if(arr[mid]+d!=arr[mid+1])                return arr[mid]+d;
            else if(arr[mid]!=arr[mid-1]+d)                return arr[mid]-d;
            else if(l>L)            hi=mid-1;
            else //r>R            lo=mid+1;        }        if(arr[lo]+d!=arr[lo+1])            return arr[lo]+d;
        else if(arr[hi]!=arr[hi-1]+d)            return arr[hi]-d;
        else if(arr[lo]!=arr[lo-1]+d)            return arr[lo]-d;
        else //(arr[l]+d!=arr[lo+1])            return arr[hi]+d;    }};https://practice.geeksforge...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.