So I took this practice lesson called 'permMissingElement' on www.codility.com, and here is my code:
#include <iostream>
using namespace std;
int solution(vector<int> &A)
{
int N = A.size();
double Nf = double(N);
int n;
double missingf;
int missing;
double sumN1, sumN2;
int sum = 0;
double sumf;
double two = 2.0;
for (n = 0; n < N; n++)
{
sum = sum + A[n];
}
sumf = double(sum);
sumN1 = (Nf+double(1.0))/double(2.0);
sumN2 = sumN1*(Nf+double(2.0));
missingf = sumN2 - sumf;
missing = int(missingf);
return missing;
}
Now, here is the problem. My algorithm works, but for some reason I cannot figure out, gives wrong answers on "large" values of N. (N can be a maximum of 100,000 in the lesson).
Anyway, I initially wrote the program using all ints, and then realized maybe I was getting an overflow because of that, so I changed them to doubles, but I still get wrong answers on large values of N... why is that?
Thanks