如何从偶数{2,4,6,8,10,14}中查找缺失的数字

时间:2019-08-20 11:15:54

标签: java arrays

我想从偶数中找到丢失的数字

例如 {2,4,6,8,10,14}; //输出应为12

我尝试过:

public class MissingNumber {
    public static void main(String[] args) {

    int a[] = {2,4,6,8,10,14};
    int sum = 0;
    for (int i = 0; i<a.length; i++) {
        sum = sum + a[i];
    } 


    int sum1 = 0;
    for(int j=1; j<=7; j++) {
        sum1 = sum1 + j;
    } 
    System.out.println("missing number is:"+(sum1-sum));
    }
}

7 个答案:

答案 0 :(得分:3)

您可以使用a[i+1]必须等于a[i]+2的想法,以使您的数字流有效。如果a[i]+2 != a[i+1],则a[i]+2是偶数流中缺少的数字:

public static void main(String[] args) {
  int[] a = {2,4,6,8,10,14};            // create Array
  Arrays.sort(a);                       // Sort Array (Just in case)
  int missing = -1;                     // Default value if no missing int is found
  for (int i = 0; i<a.length-1; i++) {  // loop through Array
    if(a[i]+2 != a[i+1]) {              // check if expected next number exists
      missing = a[i]+2;                 // sets missing to the missing number
      break;                            // stops the for loop
    }
  }
  System.out.println("missing number is: " + missing);
}

答案 1 :(得分:2)

不确定您为什么要查看总和,除非您确定一系列数字始终只缺少一个数字。

否则,这样的事情怎么样:

    int a[] = {2,4,6,8,10,14};

    int expected = 2;
    for (int val : a) {
        if (expected != val) {
            System.out.println("Missing number is " + expected);
        }
        expected = expected +2;
    }

答案 2 :(得分:1)

您可以尝试对数组元素进行偶校验。

public static void main(String[] args) {
    int a[] = {2, 4, 6, 8, 10, 14};

    int sum = 2;
    for (int i = 0; i < a.length; i++) {
        if (a[i] != sum) {
            System.out.println("missing number is: " + sum);
            break;
        }
        sum = sum + 2;
    }
}

答案 3 :(得分:1)

我对您的代码进行了一些更改。通过这些更改,您将得到答案。

public class MissingNumber {
    public static void main(String[] args) {

        int a[] = {2,4,6,8,10,14};
        int sum = 0;
        for (int i = 0; i<a.length; i++) {
            sum = sum + a[i];
        } 

        int sum1 = 0;
        int even = 2;
        for (int j=0; j<=a.length; j++) {
            sum1 = sum1 + even;
            even = even + 2;
        }

        System.out.println("missing number is:"+(sum1-sum));
    }
}

答案 4 :(得分:1)

上面有很多解决方案,但是所有解决方案都具有O(N)复杂性。 我相信您可以使用https://en.wikipedia.org/wiki/Binary_search_algorithm使用O(log N)解决此问题。

原始代码:

public class MissingNumber {
    public static void main(String[] args) {
        int a[] = {2,4,8,10, 12, 14};
        int start = 0;
        int end = a.length;
        int pointer = 0;
        while (end - start > 1) {
            if (a[pointer] == (pointer + 1) * 2) {
                start = pointer;
            } else {
                end = pointer;
            }
            pointer = (start + end) / 2;
        }
        System.out.println("Missing element: " + (pointer + 2) * 2);
    }
}

您可以添加更多条件。

答案 5 :(得分:0)

您可以执行以下操作:

tns platform update android

这可能太过工程化了。但这并不取决于给定输入的顺序。

答案 6 :(得分:0)

//快速缺少号码

view.setTag(index)
index = view.getTag()