找到数组

时间:2016-02-06 10:16:49

标签: java arrays

任何人都可以提供并解释这个问题的解决方案

我有一个整数数组

int[] arr = {1,6,2,3,8};

我想在数组中找到第二大连续整数的总和,并且还显示总和产生第二大数字的元素对

例如,从上面的数组中,连续整数的总和是

  • 1 + 6 = 7
  • 6 + 2 = 8
  • 2 + 3 = 5
  • 3 + 8 = 11

程序的输出是

8 by elements 6,2

此问题的条件是

  1. 必须在一个循环中完成
  2. 不得使用新阵列
  3. 不得对给定数组进行排序
  4. 不得使用Collection Framework

4 个答案:

答案 0 :(得分:0)

class Solution {

    public static void main(String[] args) throws IOException {
        long max = Long.MIN_VALUE + 1, secondMax = Long.MIN_VALUE;
        int positionMax = -1, positionSecondMax = -1;
        int[] arr = {1,6,2,3,8};

        for(int i = 0; i < arr.length - 1; i++){
            if(arr[i] + arr[i + 1] > max){
                secondMax = max;
                positionSecondMax = positionMax;
                max = (long)arr[i] + (long)arr[i + 1];
                positionMax = i;
            }
            else if(arr[i] + arr[i + 1] < max && arr[i] + arr[i + 1] > secondMax){
                secondMax = (long)arr[i] + (long)arr[i + 1];
                positionSecondMax = i;
            }
        }

        System.out.println(secondMax + " by elements " + arr[positionSecondMax] + ", " + arr[positionSecondMax + 1]);
    }
}

答案 1 :(得分:0)

public class Addition{
  public int leftOperand, rightOperand;
  public int sum;
  public Addition(int left, int right){
    sum = (this.leftOperand = left) + (this.rightOperand = right);
  }



  public static Addition get(int[] array){
    // initialize top two memory
    Addition max = null, secMax = null;
    for(int i = 0; i < array.length - 1; $i++){
      Addition add = new Addition(array[i], array[i + 1]);
      if(max == null){
        max = secMax = add; // initialize them for the first time
        continue;
      }
      if(secMax.sum < add.sum){
        secMax = add; // displaces second max
        if(max.sum < add.sum){ // add is already second max. This doesn't need to be outside the former if block.
          secMax = max;
          max = add;
        }
      }
    }
    return secMax;
  }
}

答案 2 :(得分:0)

这是一个尝试。一个循环,存储总和操作数和最高和第二高总和的总和。

    int[] arr = {1,6,2,3,8};

    int highestFirstOperand = -1;
    int highestSecondOperand = -1;
    int secondHighestFirstOperand = -1;
    int secondHighestSecondOperand = -1;
    int highestSum = -1;
    int secondHighestSum = -1;
    for (int i=0; i<arr.length; i++) {
        if (i<arr.length-1) {
            int thisSum = arr[i] + arr[i + 1];
            if (thisSum > highestSum) {
                secondHighestSum = highestSum;
                secondHighestFirstOperand = highestFirstOperand;
                secondHighestSecondOperand = highestSecondOperand;
                highestSum = thisSum;
                highestFirstOperand = arr[i];
                highestSecondOperand = arr[i+1];
            } else if (thisSum > secondHighestSum) {
                secondHighestSum = thisSum;
                secondHighestFirstOperand = arr[i];
                secondHighestSecondOperand = arr[i + 1];
            }
        }
    }
    System.out.println(secondHighestSum + " by elements " + secondHighestFirstOperand + "," + secondHighestSecondOperand);

答案 3 :(得分:-1)

遍历每个连续的数组对,并跟踪最高连续和的位置和位置以及第二个最高连续和的位置,并在它获得更高值时更新它们。

如果long用于存储总和,则需要使用int来存储连续总和,因为添加两个int可能会溢出值。

public class SecondLargest {
    public static int findSecondLargestConsecutiveSum( final int[] array ){
        if ( array == null )
          throw new IllegalArgumentException( "Cannot find consecutive sum in a null array" );
        if ( array.length < 3 )
          return -1;

        long largest         = Long.MIN_VALUE;
        long secondLargest   = Long.MIN_VALUE;
        int largestPos       = -1;
        int secondLargestPos = -1;
        long sum;
        for ( int i = 0; i < array.length - 1; ++i )
        {
            sum = array[i] + array[i+1];
            if ( sum > largest ) {
                secondLargest = largest;
                secondLargestPos = largestPos; 
                largest = sum;
                largestPos = i;
            }
            else if ( sum < largest && sum > secondLargest ){
                secondLargest = sum;
                secondLargestPos = i;
            }
        }
        return secondLargestPos;
    }

    public static String formatSecondLargest( final int[] array ){
        final int pos = findSecondLargestConsecutiveSum( array );
        if ( pos == -1 )
            return "Array does not have a second largest consecutive sum.";

        return String.format( "%d by elements %d,%d at position %d", (long) array[pos] + (long) array[pos+1], array[pos], array[pos+1], pos );
    }

    public static void main( final String[] args ){
        System.out.println(formatSecondLargest( new int[]{ 1,6,2,3,8 } ) );
        System.out.println(formatSecondLargest( new int[]{ 1,6,2,3,8,3 } ) );
        System.out.println(formatSecondLargest( new int[]{ Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE } ) );
        System.out.println(formatSecondLargest( new int[]{ Integer.MIN_VALUE+1, Integer.MIN_VALUE, Integer.MIN_VALUE+2 } ) );
    }
}

输出:

8 by elements 6,2 at position 1
8 by elements 6,2 at position 1
Array does not have a second largest consecutive sum.
-4294967295 by elements -2147483647,-2147483648 at position 0