Index Of an Extra Element | Practice | GeeksforGeeks

Given two sorted arrays of distinct elements. There is only 1 difference between the arrays. First array has one element extra added in between. Find the index of the extra element.
Example 1:
Input:
N = 7
A[] = {2,4,6,8,9,10,12}
B[] = {2,4,6,8,10,12}
Output: 4
Explanation: In the second array, 9 is
missing and it's index in the first array
is 4.
Example 2:
Input:
N = 6
A[] = {3,5,7,9,11,13}
B[] = {3,5,7,11,13}
Output: 3
Your Task:
You don't have to take any input. Just complete the provided function findExtra() that takes array A[], B[] and size of A[] and return the valid index (0 based indexing).
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
2<=N<=104
1<=Ai<=105
0
abhigyanpatek4 days ago
Simple C++ Solution:
int findExtra(int a[], int b[], int n) {       int start = 0, end = n-1;       int result;       while(start <= end){           int mid = start + (end-start)/2;           if(a[mid] == b[mid]){               start = mid+1;           }else{               result = mid;               end = mid-1;           }       }       return result;   }
0
maac_shah5 days ago
JAVA Simplest Code
 
class Solution {
    public int findExtra(int a[], int b[], int n) {
        // add code here.
        for(int i=0;i<n-1;i++)
        {
            if(a[i] != b[i])
            {
                return i;
            }
        }
        
        return n-1;
    }
}
0
pradeepshillare6 days ago
class Solution {   public int findExtra(int a[], int b[], int n)    {       // add code here.
       int i = 0;       while(a.length>b.length)       {
           if (a[i] != b[i])           {               return i;           }           else           {               i++;               if(i==a.length-1)               {                   break;               }           }       }       return i;   }    }
0
rahuljaiswalmzp2 weeks ago
 int findExtra(int a[], int b[], int n) {       // add code here.       int i = 0;       while(a>b){           if(a[i]!=b[i]){               return i;           }           else{               i++;           }       }   }
0
asthana10112 weeks ago
Java simple Binary Search solution
public int findExtra(int a[], int b[], int n) 
    {
        int lb = 0, ub = n - 1;
        while(lb < ub)
        {
            int mid = lb + (ub - lb)/2;
            if(a[mid] >= b[mid])
                lb = mid + 1;
            else if(a[mid] < b[mid])
                ub = mid;
        }
        return lb;
    }
0
mayank180919992 weeks ago
 int findExtra(int a[], int b[], int n) {
        // add code here.
        map<int,int>mp;
        for(int i=0;i<n-1;i++){
            mp[b[i]]++;
        }
        for(int i=0;i<n;i++){
            if(mp.find(a[i])==mp.end()){
                return i;
            }
        }
    return 0;
    }
0
akashmarkad22102 weeks ago
public int findExtra(int a[], int b[], int n) {       // add code here.              if(a[n-1] != b[n-2])       {           return n-1;       }       for(int i=0;i<n;i++)       {           if(a[i] == b[i])           {               continue;           }           else           {               return i;           }       }              return -1;   }
+1
imohdalam3 weeks ago
JAVA | O(log n )
class Solution {
    public int findExtra(int a[], int b[], int n) {
        
        int a1 = 0;
        boolean flag = true;
        
        for(int i=0; i<n-1 && flag; i++){
            if(a[i] != b[i]){
              flag = false;
              a1 = i;
            }
            if(a[n - 1-i] != b[n - 2 - i]){
                flag = false;
                a1 = n - 1 - i;
            }
        }
      return flag ? n-1 : a1;
    }
}
0
shrustis1763 weeks ago
C++ solution using BINARY SEARCH
 
int findExtra(int a[], int b[], int n) {       // add code here.       int l=0, h=n-1, mid;              while(l<=h)       {           mid = l+(h-l)/2;                      // when the last index is the ans           //check eg: a=0,5,8,10 and b=0,5,8
           if(mid == n-1)               return n-1;                          if(a[mid] == b[mid])               l=mid+1;           else if(a[mid] < b[mid])               h=mid-1;       }              //returning mid gives error when u are moving from left to right & again right->left       //eg : a=1,2,3,4 and b=1,3,4
       return l;   }
+1
swarajpawar3 weeks ago
class Solution{ public:     int findExtra(int a[], int b[], int n) {       // add code here.       for(int i=0;i<n;i++){           if(a[i]!=b[i])return i;       }          }};
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.