/*
 * 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.problem40;

/**
 * Given an array with elements from 1 to N where N is the size of the array operate with the
 * array to put every element in the position marked with the value of the element. You can't use
 * any other data structure and take into account the final result has to reflex the values of
 * the original one, not the already modified one.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class MoveElementsToPositions {

  public void move(int[] input) {

  }
}

@@ -1080,40 +1080,1180 @@
 %0A%0A  
-public void move(int%5B%5D input) %7B%0A
+/**%0A   * Iterative solution to solve this problem. This is a bubble sort algorithm and the complexity%0A   * order of this algorithm is O(N%5E2). In space terms, the complexity order is O(1). I've used%0A   * this algorithm to resolve this problem because in the problem statement says you can't use any%0A   * additional data structure.%0A   */%0A  public void move(int%5B%5D array) %7B%0A    if (array == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass a null array as input.%22);%0A    %7D%0A    int size = array.length;%0A    boolean swap = true;%0A    while (swap) %7B%0A      swap = false;%0A      for (int i = 0; i %3C size - 1; i++) %7B%0A        checkIfElementIsInsideTheValidInterval(array%5Bi%5D, size);%0A        if (array%5Bi%5D %3E array%5Bi + 1%5D) %7B%0A          swap = true;%0A          swapElements(array, i, i + 1);%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  private void checkIfElementIsInsideTheValidInterval(int number, int length) %7B%0A    if (number %3C 0 %7C%7C number %3E= length) %7B%0A      throw new IllegalArgumentException(%22Input array contains an invalid element: %22 + number);%0A    %7D%0A  %7D%0A%0A  private void swapElements(int%5B%5D array, int i, int j) %7B%0A    int temp = array%5Bi%5D;%0A    array%5Bi%5D = array%5Bj%5D;%0A    array%5Bj%5D = temp;
 %0A  %7D

Add iterative solution to problem 40