使用较早出现的较小整数查找数组中可能存在的最大差异

时间:2012-08-08 06:25:11

标签: java algorithm

这是一个采访问题:

查找整数数组中可能存在的最大差异,以便较小的整数出现在数组的前面。

约束: 数字不是唯一的。 范围是java的整数范围。 (或任何其他语言)

示例:

输入1:{1,100,2,105,-10,30,100}

最大差异在-10到100之间 - > 110(此处-10表示第5个索引,100表示​​第7个索引)

输入2:{1,100,2,105,-10,30,80}

最大差异在1到105之间 - > 104(这里1是第1指数,105是第4指数)

可能的解决方案:

一种方法是检查所有可能的差异,并跟踪迄今为止发现的最大差异O(n ^ 2)复杂度。

这可以在比O(n ^ 2)时间更好的时间内完成吗?

11 个答案:

答案 0 :(得分:11)

Dhandeep的算法很好,Vivek将代码转换为Java工作! 此外,我们还可以正常扫描阵列而不是反向扫描:

int seed[] = {1, 100, 2, 105, -10, 30, 100};
int maxDiff=Integer.MIN_VALUE, minNumber = Integer.MAX_VALUE;

for (int i = 0; i < seed.length ; i++){
    if(minNumber > seed[i]) 
       minNumber = seed[i];

    maxDiff = Math.max(maxDiff, (seed[i]-minNumber));
}
System.out.println(maxDiff);

答案 1 :(得分:10)

从最后一个元素开始向后移动。记住迄今为止发生的最大元素。 对于每个元素从最大值中减去并存储在相应位置。

此外,您可以保留一个元素来存储最大差异并直接输出。 O(n)时间,O(1)空间。

int max = INT_MIN;
int maxdiff = 0;

for (i = sizeof(arr) / sizeof(int) - 1; i >= 0; i--) {
  if (max < arr[i]) {
    max = arr[i];
  }
  int diff = max - arr[i];
  if (maxdiff < diff) {
    maxdiff = diff;
  }
}

print maxdiff;

答案 2 :(得分:4)

感谢@Dhandeep Jain的回答。有java版本:

//int seed[] = {1, 100, 2, 105, -10, 30, 100};
        int seed[] = {1, 100, 2, 105, -10, 30, 80};
        int maxDiff=Integer.MIN_VALUE, maxNumber = Integer.MIN_VALUE;

        for (int i = (seed.length-1); i >=0 ; i--){
            if(maxNumber < seed[i]) 
                maxNumber = seed[i];

            maxDiff = Math.max(maxDiff, (maxNumber - seed[i]));
        }
        System.out.println(maxDiff);

答案 3 :(得分:0)

我建议最大的差异总是在此之前的最大数字和最小数字之间,或之后的最小数字和最大数字之间。这些可以在线性时间内确定。

答案 4 :(得分:0)

public class Test{

    public static void main(String[] args){

        int arr1[] = {1,2,5,7,9};
        int arr2[] = {20,25,26,35};
        int diff = 0;
        int max = 0;

        for(int i=0;i<arr1.length;i++){
            for(int j=0;j<arr2.length;j++){

                diff =  Math.abs(arr1[i]-arr2[j]);
                if(diff > max){
                    max = diff;
                }
            }
        }
    System.out.println(max);
    }   
}

答案 5 :(得分:0)

    // Solution Complexity : O(n)   
    int maxDiff(int a[], int n){
        //  Find difference of adjacent elements
        int diff[n+1];
        for (int i=0; i < n-1; i++)
            diff[i] = a[i+1] - a[i];

        // Now find the maximum sum sub array in diff array
        int max_diff = diff[0];
        for (int i = 1 ; i < n-1 ; i++ ) {
            if( diff[i-1] > 0 ) diff[i] += diff[i-1];
            if( max_diff < diff[i] ) max_diff = diff[i];
        }
        return max_diff;
    }

答案 6 :(得分:0)

首先找到数组的相邻元素之间的差异,并将所有差异存储在大小为n-1的辅助数组diff []中。现在这个问题变成找到这个差异数组的最大和子数。

答案 7 :(得分:0)

Ruby解决方案:

a = [3, 6, 8, 1, 5]
min = 10**6
max_diff = -10**6
a.each do |x|
  min = x if x < min
  diff = x - min
  max_diff = diff if diff > max_diff
end
puts max_diff

答案 8 :(得分:-1)

public static void findDifference(Integer arr[]) {
    int indexStart = 0;
    int indexMin = 0;
    int indexEnd = 1;
    int min = arr[0];
    int diff = arr[1] - arr[0];
    for (int counter = 1; counter < arr.length; counter++) {
        if (arr[counter] - min > diff) {
            diff = arr[counter] - min;
            indexEnd = counter;
            indexStart = indexMin;
        }
        if (arr[counter] < min) {
            min = arr[counter];
            indexMin = counter;
        }
    }
    System.out.println("indexStart = " + indexStart);
    System.out.println("indexEnd = " + indexEnd);
    System.out.println("diff = " + diff);
}

答案 9 :(得分:-1)

public static void findlargestDifference(int arr[]){

    int max_diff=0;     
    int min_value=Integer.MIN_VALUE;        
    for(int i=0;i<arr.length;i++){

        if(min_value<arr[i]){               
            min_value=arr[i];               
        }           
        int diff=min_value-arr[i];

        if(max_diff<diff){
            max_diff=diff;
        }                   
    }       
    System.out.println("Max Difference is  "+ max_diff);    
}

答案 10 :(得分:-2)

我很确定这可以解决你的问题:

    int largestNumber = Integer.MIN_VALUE;
    int smallestNumber = Integer.MAX_VALUE; 

    for(int i = 0; i < yourArray.Length; i++)
    {
        if(yourArray[i] > largestNumber)
            largestNumber = yourArray[i];

        if(yourArray[i] < smallestNumber)
            smallestNumber = yourArray[i];

    }

    int biggestDifference = largestNumber - smallestNumber ;