Task description
An array A consisting of N different integers is given. The array contains integers in the range [1…(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0…100,000];
the elements of A are all distinct;
each element of array A is an integer within the range [1…(N + 1)].
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
题目本身不难,只是边角问题一定要注意。因为网站这里给出的test case 很奇怪,要是第一次拿不到满分可以尝试按照错误的test case更改,不然最后拿不到满分。自定义的test case 可以尝试写几个。
P.S 这个使用到的包需要自己import 不然会显示错误
解法一
Detected time complexity:O(N) or O(N * log(N))
import java.util.Arrays;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
//empty_and_single
if(A.length==0