Find Missing And Repeating | Practice | GeeksforGeeks

Given an unsorted array Arr of size N of positive integers. One number 'A' from set {1, 2, ...N} is missing and one number 'B' occurs twice in array. Find these two numbers.
Example 1:
Input:
N = 2
Arr[] = {2, 2}
Output: 2 1
Explanation: Repeating number is 2 and 
smallest positive missing number is 1.

Example 2:
Input:
N = 3
Arr[] = {1, 3, 3}
Output: 3 2
Explanation: Repeating number is 3 and 
smallest positive missing number is 2.

Your Task:
You don't need to read input or print anything. Your task is to complete the function findTwoElement() which takes the array of integers arr and n as parameters and returns an array of integers of size 2 denoting the answer ( The first index contains B and second index contains A.)
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ N
0
adityamurali28 hours ago
 int *findTwoElement(int *arr, int n) {       
       sort(arr, arr+n);              int* temp = new int[2];       int count = 0;          map<int,int> m;              for(int i = 0; i < n; i++)       {           m[arr[i]]++;       }       for(int i = 1; i <= n; i++)       {           if(m.find(i) == m.end()){               temp[1] = i;               break;                          }       }       for(auto it : m)       {           if(it.second > count)           {               count = it.second;               temp[0] = it.first;           }       }       return temp;          }
+1
lakshaysaini22119 hours ago
EASY C++ SOLUTION:-
----------------------
int * ans = new int[2];     unordered_map<int,int> mp;     for(int i=0;i<n;i++)      mp[arr[i]]++;          for(int i=0;i<n;i++)         if(mp[arr[i]]>1){           ans[0]=arr[i];           break;         }      sort(arr, arr+n); for(int i=0;i<n;i++){     if(arr[i]==arr[i+1])     continue;     if(arr[i]+1!=arr[i+1]) {         ans[1]=arr[i]+1;         break;     } } if(arr[n-1]==n and ans[1]==n+1) ans[1]=1;   return ans;
 
+1
aryansharma09181 day ago
class Solution{public:   int *findTwoElement(int *arr, int n) {       int actualSum=0,sum=0;              int *ans=new int[2];              sort(arr,arr+n);              for(int i=0;i<n;i++){           if(arr[i] == arr[i+1]){               ans[0]=arr[i];           }           sum+=arr[i];       }              for(int i=1;i<=n;i++){           actualSum+=i;       }              ans[1]=actualSum-sum+ans[0];              return ans;   }};
0
adamyasharma4931 day ago
O(n)-→Time and Space
int *findTwoElement(int *arr, int n) {
         
        unordered_set<int> s; 
        int R,M;
        int* ans = new int[2];
        for(int i=0;i<n;i++){
            if(s.find(arr[i])!=s.end()){
                R = arr[i];
            }
            s.insert(arr[i]);
        }
        for(int i=1;i<=n;i++){
            if(s.find(i)==s.end()){
                M = i;
                break;
            }
        }
        ans[0]=R;
        ans[1]=M;
        return ans;
    }
0
ankit2908011 day ago
JAVA solution with TC = O(N) and SC = O(N)
 
int[] findTwoElement(int arr[], int n) {       // code here       int r[] = new int[2];       HashSet<Integer> set = new HashSet<>();       int i = 1;       while(i<=n){           set.add(i);           i++;       }       for(int e : arr){           if(!set.contains(e)){               r[0] = e;           }           set.remove(e);       }       for(int e : set){           r[1] = e;       }       return r;   }
-1
siddhesh_patil
This comment was deleted.
0
swetkrmis2 days ago
int *findTwoElement(int *arr, int n) {       map<int,int>mp;             int *p = new int[2];       for(int i=1;i<=n;i++){           mp[i]=0;       }       for(int i=0;i<n;i++){           mp[arr[i]]++;       }       for(auto i:mp){           if(i.second>1) p[0]=i.first;           if(i.second==0) p[1]=i.first;       }       return p;   }
0
kaganesh122 days ago
easiest way to this i think:)
   int *findTwoElement(int *arr, int n)   {       sort(arr,arr+n);       int missing=1,repeat;       int i;       for (i=0;i<n;i++)       {           if (arr[i]==missing)           missing++;       }              for (i=0;i<n;i++)       {           if (arr[i]==arr[i+1])           {           repeat=arr[i];           break;           }       }       int ans[2];       int *result = (int *)malloc(2*sizeof(int));       result[1]=missing;       result[0]=repeat;       return result;   }
0
mohitkumarrajbadi3 days ago
class Solution{
public:
    int *findTwoElement(int *arr, int n) {
        // code here
        long long len = n;
        
        long long S = len * (len + 1) / 2;
        long long P = len * (len + 1) * (2 * len + 1) / 6;
        
        for(long long i = 0; i < len; i++){
            S -= (long long) arr[i];
            P -= (long long) arr[i] * (long long) arr[i];
        }
        
        int missing = (S+P/S)/2;
        int repeating = missing - S;
        
        int *ans = new int[2];
        ans[0] = repeating;
        ans[1] = missing;
        
        return ans;
    }
};
0
akyadav133984 days ago
  int *findTwoElement(int *a, int n) {       // code here       int *ans=new int[2];       sort(a,a+n);       int j=1;       int m;       for(int i=0;i<n;i++){           if(a[i]<=0) continue;            if(a[i]==a[i+1])m=i;           if(j==a[i]){               ++j;           }                     }        //for(int i=0;i<n;i++)       //if(a[i]==a[i+1])m=i;       ans[0]=a[m];       ans[1]=j;       return ans;   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.