Last active
August 16, 2016 02:08
-
-
Save raunaqbn/2ed004774bd1b43aef583142b7ef5511 to your computer and use it in GitHub Desktop.
Arrays 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vecvector<int> removeDuplicates(vector<int> A) | |
{ | |
int j = 0; | |
for(int i = 1; i < A.size()-1; i++) | |
{ | |
if (A[i] != A[i-1]) | |
{ | |
swap(A[j],A[i]); | |
j++; | |
} | |
} | |
while (j < (A.size()-1)) | |
{ | |
A.erase(j+1,A.size()-1); | |
} | |
return A; | |
} | |
vector<bool> listPrimes(int num) | |
{ | |
// write a function isPrime which checks if a number is prime or not. | |
// If it is prime then check for its multiples which are less than num and mark as false | |
vector<bool> primes(num+1,true); | |
vector<int> result; | |
int check = 2; | |
while (check < num) | |
{ | |
if(primes[check]) | |
{ | |
result.emplace_back(check); | |
int j = check*2; | |
while (j < num) | |
{ | |
j += check; | |
result[j] = false; | |
} | |
} | |
} | |
return result; | |
} | |
//possible optimization: Start seiving and remvoing possibilities from the primes square because all the values before have already been set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment