2016-02-06 80 views
0

誰能請提供並說明了解決這一問題的找到連續整數數組的第二大和

我有一個整數

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

我想找到的第二大和的數組數組中的連續整數並且還顯示總和爲sum的元素對產生第二大數

例如從上面的數組中,連續整數的總和是

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

的程序的輸出是

8 by elements 6,2 

這個問題的條件是

  1. 必須在一個循環中完成
  2. 不能使用新的數組
  3. 切不可給定的數組排序
  4. 不得使用回收體系

回答

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]); 
    } 
} 
+0

您必須在每個階段存儲變量中max和secondmax的位置,以便能夠在最後顯示元素。 –

+0

你爲什麼使用計數器變量? – Nishant123

+0

對於int [] {Integer.MIN_VALUE,Integer.MIN_VALUE + 1,Integer.MIN_VALUE + 2}' – MT0

-1

迭代通過每個連續的數組並跟蹤最高連續總和在哪裏以及什麼位置以及第二高連續總和在哪裏以及什麼位置,並隨着它們獲得更高值而更新它們。

如果使用int來存儲總和,則需要使用long將連續的總和存儲爲加上兩個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 
+0

*任何人都可以提供和解釋這個問題的解決方案*,不能說你涵蓋解釋部分。 – Emz

+0

添加說明 - 該算法很簡單,因爲它只是遍歷列表跟蹤,其中最大和第二個最大總和是什麼以及它們的值是什麼,在找到更高值時更新它們。 – MT0

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; 
    } 
} 
0

下面是一個attempt.One循環,儲存和操作數和金額兩個最高和第二高的總和。

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);