jayati

Find Missing And Repeating

Nov 2nd, 2023
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. // } Driver Code Ends
  7. class Solution{
  8. public:
  9.     vector<int> findTwoElement(vector<int> arr, int n) {
  10.         // code here
  11.         int ans=-1,ans1=-1;
  12.        
  13.        
  14.         int a[n+1]={0};
  15.         for(int i=0;i<arr.size();i++)
  16.         {
  17.             a[arr[i]]+=1;
  18.         }
  19.         for(int i=1;i<=n;i++)
  20.         {
  21.             if(a[i]==0)
  22.             {
  23.                 if(ans!=-1)
  24.                 {
  25.                     continue;
  26.                 }
  27.                 ans=i;
  28.             }
  29.             if(a[i]==2)
  30.             {
  31.                 if(ans1!=-1)
  32.                 {
  33.                     continue;
  34.                 }
  35.                 ans1=i;
  36.             }
  37.             if(ans!=-1 && ans1!=-1)
  38.             {
  39.                 break;
  40.             }
  41.         }
  42.         return {ans1,ans};
  43.     }
  44. };
  45.  
  46. //{ Driver Code Starts.
  47.  
  48. int main() {
  49.     int t;
  50.     cin >> t;
  51.     while (t--) {
  52.         int n;
  53.         cin >> n;
  54.         vector<int> a(n);
  55.         for (int i = 0; i < n; i++) {
  56.             cin >> a[i];
  57.         }
  58.         Solution ob;
  59.         auto ans = ob.findTwoElement(a, n);
  60.         cout << ans[0] << " " << ans[1] << "\n";
  61.     }
  62.     return 0;
  63. }
  64. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment