John wants to unlock an electrical lock and needs a password to gain access. He is given a set of N numbers and some of these numbers generate the password. The numbers that are selected have equal distance to 2 prime numbers. Also, the password consists of unique numbers, there can't be two 6s for example in the final answer.
So I thought of handling this with creating an array that has the positions of all the prime numbers, and then, if the distance between two positions is not even, divide it by two and then input the initial array's (all the numbers) matching the distance/2 pushed in the front of a list. The use unique() on the lists and just output it.
#include<iostream>
#include<list>
usingnamespace std;
bool compare_sort (constint& first, constint& second) //used to sort a list
{
if (first>second) returntrue;
elseif (first<second) returnfalse;
}
bool prime(int a){ //function for checking if a number is prime or not
bool primebool=true;
if(a==1){
primebool=false;
}
else{
int half=a/2;
for(int i=2; i<=half; i++){
if(a%i==0){
primebool=false;
break;
}
}
}
return primebool;
}
int main(){
int m; //total number of integers
cin>>m;
int arr[m];
int check;
int primes=0;
for(int i=0;i<m;i++){
cin>>arr[i]; //initial number group
check = arr[i];
if(prime(check)){
primes++; //finds the number of primes
}
}
int pos[primes]; //declaring the positions array
int counter=0; //will be used to input all the positions
for(int i=0;i<m;i++){ //finds all positions of prime numbers
int k=arr[i];
if(prime(k)==true){
pos[counter]=i;
counter++;
}
}
list<int> password(primes); //declaring the list with a size of primes, just in case
int help;
for(int i=0; i<(counter-1); i++){
for(int j=i+1; j<counter; j++){
int distance=pos[j]-pos[i]-1; \\-1 array begins at 0
if(distance%2==1){
int place=(distance/2)+1;
help=arr[pos[i]+place]; //finds a number that is in the answer
password.push_front(help);
}
}
}
password.sort(compare_sort) //here I sort.
password.unique(); //speaks for itself I guess
for(list<int>::iterator it = password.begin(); it!=password.end(); it++){
cout<<*it<<endl;
}
return 0;
}
For example in the row 1 13 4 12 9 7 3 5 the number 12 would be on of the selected numbers because it's distance from 13 (2) is equal to the distance from 7 (2). Both 13 and 7 are prime numbers.
he code works but only in a few test cases. ¿by instance?
In the following it doesn't, and I think there are more:
10
2 3 5 7 9 11 13 17 23 29
The normal output should've been all the numbers except 2 and 29, but it outputs this:
@cire.
Totally forgot about that.... But even if I do the answer would still be wrong because the autocompiler's test cases' answers are by the order that they are found so it gives me 0/100...
I can't actually use your code as posted since it isn't legal C++ and I don't currently have a compiler installed with the gcc vla extension, but if I replace your vlas with a vector I get a noticeable difference in output from what you were reporting.
Here's what I would do, assuming I'm understanding the requirements correctly:
Read the input into a container, c. Store the prime indexes in order in another container, p.
Loop through all elements in c that are higher than p.front() and lower than p.back(), and for every element iterated over determine if there is a pair of indexes in p which satisfy the equidistant requirement for the index of the current element. If so, add it to a third container, r.
The size of the array is a variable that has a value. For example, int arr[m]; will have a certain value while the program runs, since the code: int m; cin>>m; is before the declaration of the array. I didn't declare the array before knowing the size. In my compiler (Code::Blocks) this code runs without error.