Find Missing And Repeating equation based code code example
Example: Find Missing And Repeating equation based code
#include <bits/stdc++.h>
using namespace std;
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;
}
};
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;
}