2017-09-18 11 views
0

3つの要素の配列が与えられます: - 2,4,5 と与えられた番号n = 10 1からnの範囲内のすべての数値の数が、すべての配列要素。すべての配列要素で割り切れない数

出力: - 4 (1,3,7,9)

任意のより良いアプローチブルートフォース? nは1〜10の範囲にあります。

答えて

0

ハッシュセットをとり、arrayElementsのすべての倍数をnより小さくして、セットサイズを減算します。

int n = 10; 
    int k = 3; 
    int[] jump = { 2, 5, 4 }; 
    Set<Integer> jumpSet = new HashSet<Integer>(); 
    for (int i = 0; i < jump.length; i++) { 
     if (!jumpSet.contains(jump[i])) { 
      for (int j = 1; j <= n/jump[i]; j++) 
       jumpSet.add(jump[i] * j); 
     } 
    } 
    System.out.println(n - jumpSet.size()); 
関連する問題