/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem72;

/**
 * An array A contains all the integers from 0 through N, except for one number which is missing.
 * Can you write a method to find that number?
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class FindTheMissingNumber {

}

@@ -878,10 +878,1255 @@
 mber %7B%0A%0A
+  /**%0A   * Iterative algorithm to solve this problem. This solution is based on the summation definition,%0A   * if the array has every element from 0 to N except one, then we can calculate the sum of every%0A   * element in the summation and the real sum to subtract both values and find the missing number.%0A   * The complexity order in time terms is O(N) where N is the number o elements in the array. In%0A   * space terms, the complexity order is O(1) because we are not using any auxiliary data%0A   * structure with a size related to the input data to resolve this problem.%0A   */%0A  public int find(int%5B%5D numbers) %7B%0A    validateInput(numbers);%0A%0A    int length = numbers.length;%0A    int idealSum = length * (length + 1) / 2;%0A    int sum = calculateSum(numbers);%0A%0A    int missingNumber = idealSum - sum;%0A    return missingNumber;%0A  %7D%0A%0A  private int calculateSum(int%5B%5D numbers) %7B%0A    int sum = 0;%0A    for (int n : numbers) %7B%0A      sum += n;%0A    %7D%0A    return sum;%0A  %7D%0A%0A  private void validateInput(int%5B%5D numbers) %7B%0A    if (numbers == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass a null instance of numbers.%22);%0A    %7D%0A    if (numbers.length == 0) %7B%0A      throw new IllegalArgumentException(%22You can't pass an empty array%22);%0A    %7D%0A  %7D%0A
 %7D%0A

Add solution to problem 72