unfriendly numbers
<p>There is a problem in interviewstreet which goes like this:</p>

<blockquote>
  <p>There is one friendly number and N unfriendly numbers. We want to find how many numbers are there which exactly divide the friendly number, but does not divide any of the unfriendly numbers.</p>
  
  <p><strong>Input Format:</strong></p>
  
  <p>The first line of input contains two numbers N and K seperated by spaces. N is the number of unfriendly numbers, K is the friendly number.
  The second line of input contains N space separated unfriendly numbers.</p>
  
  <p><strong>Output Format:</strong></p>
  
  <p>Output the answer in a single line.</p>
  
  <p><strong>Sample Input:</strong></p>

<pre><code>8 16
2 5 7 4 3 8 3 18
</code></pre>
  
  <p><strong>Sample Output:</strong></p>

<pre><code>1
</code></pre>
  
  <p><strong>Explanation:</strong></p>
  
  <p>Divisors of the given friendly number 16, are {&nbsp;1, 2, 4, 8, 16&nbsp;} and the unfriendly numbers are {&nbsp;2, 5, 7, 4, 3, 8, 3, 18&nbsp;}. Now 1 divides all unfriendly numbers, 2 divides 2, 4 divides 4, 8 divides 8, but 16 divides none of them. So only one number exists which divides the friendly number but does not divide any of the unfriendly numbers. So the answer is 1.   </p>
</blockquote>

<p>Following is my C++ solution:</p>

<pre><code>#include&lt;iostream.h&gt;
using namespace std;
int check(int i,int a[],int n)
{
   for(int j=0;j&lt;n;j++)
     if(a[j]%i==0)
       return 0;
   return 1;
}

int main()
{
   int n,k,i,*a,count=0;
   cin&gt;&gt;n&gt;&gt;k;
   a=new int[n];
   for(i=0;i&lt;n;i++)
     cin&gt;&gt;a[i];
   if(k!=0){
    for(i=2;i&lt;=(k/2);i++)
      if((k%i)==0)
         count+=check(i,a,n);
    count+=check(k,a,n);
   }
   cout&lt;&lt;count;
}
</code></pre>

<p>Only the first 2 test cases pass. For the 3rd and 4th, my solution gives the wrong answer. For the 5th and 6th, it exceeds the time limit. What is the error in my code?</p>
