在二进制搜索中决定

时间:2018-06-11 17:32:26

标签: algorithm binary-search

我正在练习关于hackerearth的问题:

https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/monk-and-special-integer-code-monk/

在这个问题中,我编写了一个二进制搜索代码,我曾用过:

int mid =(低+高)/ 2

我的循环卡在这里,所以我在某些情况下得到了TLE。 意识到这个问题(选择了反复低的问题) 我改变了mid到low +(high-low + 1)/ 2并改变了整个测试用例 通过。 (代码1)

我在使用(低+高)/ 2时也遇到了类似的问题,并且还通过了所有测试用例。

我的问题是我们如何决定如何选择中期?

PS:这些是练习题,现在(我)解决了

代码1

public static boolean subarray(int mid,long x,long[] sum,int[] a){
    int n=a.length;
    for(int i=0;i<n-mid+1;i++){
    if(sum[mid+i-1]-sum[i]+a[i]>x){
        return false;
    }

    }
    return true;

}

public static void binarysearch(long[] sum,int [] a,long x){
    int low=1;
    int high=a.length;

    while(low!=high){
        int mid=low+ (high-low+1)/2; //passed
        //int mid=(low+high)/2;    did n't PASS

        if(!subarray(mid,x,sum,a)){//if some greater then x
            high=mid-1;              
        }
        else{
             //if some less then x okay but can go for more
            low=mid;

        }
    }
    System.out.println(low);

}

代码2

    public static long binarysearch(long[] a,long x,long[] L,long[] R){
    //find first index >= x
    BufferedOutputStream out=new BufferedOutputStream(System.out);
    int low=0;
    int high=a.length;
    while(low!=high){
        int mid=(low+high)/2;
        if(a[mid]<x){
            low=mid+1;
        }
        else{
            high=mid;
        }
    }
    long ans=L[low]+x-1;   
    if(low!=0){
    ans=L[low]+x-a[low-1]-1;
    }

     return ans;



}

1 个答案:

答案 0 :(得分:2)

这项技术:

low + (high - low) /2

主要用于避免整数溢出。

由于mid是数字类型的实例,因此它具有可容纳的值的上限。低值和高值之和可能超过此最大值,从而导致溢出和不可预测的结果。即使低和高都是合法值(即正和高&gt; =低),也会发生这种情况。

表达式mid = low +(high - low)/ 2对于高和低的合法值永远不会溢出,并且总是会给出所需的结果。

示例:

int low = 1170105034
int high = 1347855270
(low + high) / 2 //outputs  -888503496
low + (high - low) / 2 //outputs 1258980152
相关问题