Answers for "Find Missing And Repeating"

C++
0

Find Missing And Repeating

// { Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

 // } Driver Code Ends
class Solution{
public:
    long long *findTwoElement(int *arr, int n) {
        long long N=n;
         long long s=(N*(N+1))/2;
        long long sm=(N*(N+1)*(2*N +1))/6;
    
        for(int i=0;i<n;i++){
            s-=arr[i];
            long long k=arr[i];
            sm-=k*k;
        }
        
       long long flag=sm/s;
        long long mis=(flag+s)/2;
        long long db=flag-mis;
        long long*a=new long long (2);
        a[0]=db;
        a[1]=mis;
    return a;
    }
};


// { Driver Code Starts.

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a[n];
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        Solution ob;
        auto ans = ob.findTwoElement(a, n);
        cout << ans[0] << " " << ans[1] << "\n";
    }
    return 0;
}  // } Driver Code Ends
Posted by: Guest on May-21-2021

Browse Popular Code Answers by Language