title: Divisors inside an array
body: <p>I need to write a method that takes an array of integers and checks for every element if all its divisors (except the number itself and 1) are present in this array. If yes, the method will return true.</p>
  
  <p>For example, the following array will return true:</p>
  
  <pre><code>4,5,10,2
  </code></pre>
  
  <p>I can't think of something efficient enough to be implemented. Could you guys help me out here?</p>
  
  <p>I've been thinking to iterate through every element in the array, search for all of its divisors, put them on array, return the array and then compare to the elements in the original array.</p>
  
  <p>This is a possible solution and it could work but I want to know of other possible solutions.</p>
  
  <p>EDIT: Here is a code I've came up with but it is super slow. Could you guys help me optimise it a little bit?:</p>
  
  <pre><code>import java.util.Arrays;
  
  public class Divisors {
  
          public static void main(String[] args) {
                  int[] numbers = { 4, 5, 10, 2 };
                  boolean flag = true;
                  for (int num : numbers) {
                          if (num % 2 != 0) {
                                  for (int subNum = 1; subNum &lt; num / 2; num += 2) {
                                          if(num%subNum == 0 &amp;&amp; subNum != 1) {
                                                  if(!Arrays.asList(numbers).contains(subNum)) {
                                                          flag = false;
                                                  }
                                          }
                                  }
                          } else {
                                  for (int subNum = 1; subNum &lt; num / 2; num++) {
                                          if(num%subNum == 0 &amp;&amp; subNum != 1) {
                                                  if(!Arrays.asList(numbers).contains(subNum)) {
                                                          flag = false;
                                                  }
                                          }
                                  }
                          }
                  }
  
                  System.out.println("Result is: "+flag);
          }
  }
  </code></pre>