【问题标题】:How to find if number is factor of any number in sorted array in (logn) time [duplicate]如何在(logn)时间内查找数字是否是排序数组中任何数字的因子[重复]
【发布时间】:2017-07-21 09:36:22
【问题描述】:

给定输入 x 和数组 [a1,a2,a3..an],是否可以在 log(n) 时间中找到 x 的最大数量。

例如 x = 2,

数组已排序,[1,2,4,4,9,13,18],

这里 x 是因子的最大数是 18。是否有可能在 log(n) 时间内找到 18。

【问题讨论】:

  • 哇,我从来没有见过在原始问题之后这么快就发布了重复的问题
  • 谢谢。这是重复的。

标签: algorithm binary-search


【解决方案1】:
import java.util.ArrayList;
import java.util.Collections;
public class HelloWorld{

     public static void main(String []args){

          ArrayList<Integer> al2 = new ArrayList<Integer>();
         int a =4;
         int[] b={40,2,12,8,24,13,18};
        for(int j=0;j<b.length;j++)
         {
             if(b[j]%a==0)
             {
                al2.add(b[j]);
               Collections.sort(al2);
               }
         }
     System.out.println(al2.get(al2.size()-1));
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2012-06-22
    • 2014-03-01
    • 1970-01-01
    • 2011-04-04
    • 2015-09-14
    相关资源
    最近更新 更多