import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

public class Arraz {

    public int sumValuesOfArray(Integer[] array){
        int sum = 0;
        for (int i = 0; i< array.length; i++){
            sum+= array[i];
        }
        return sum;
    }

    public double sumDoublesOfArray(Double[] array){
        double sum = 0;
        for (int i = 0; i < array.length; i++){
            sum += array[i];
        }
        return sum;
    }

    public int averageOfArray(Integer[] array){
        return sumValuesOfArray(array)/array.length;

    }

    public double doubleAverageOfArray(Double[] array){
        return sumDoublesOfArray(array)/ array.length;
    }

    public boolean containsValue(Integer[] array, Integer value){
        boolean contains = false;
        for (int i = 0; i <array.length; i++){
            if(array[i].equals(value)){
                contains = true;
                break;
            }
        }
        return contains;
    }

    public Integer[] reverseArray(Integer[] array){
        Integer[] reversedArray = new Integer[array.length];
        int j = 0;

        for (int i = array.length-1; i >=0; i--){
            reversedArray[j] = array[i];
            j++;
        }
        return reversedArray;
    }

    public OddEven getOddEvensOfArray(Integer[] array){
        int odd = 0;
        int even = 0;
        OddEven oe;
        for (int i = 0; i < array.length; i++){
            if (array[i] % 2 == 0){
                even++;
            }else {
                odd++;
            }
        }
        oe = new OddEven(odd, even);
        return oe;
    }

    public int findIndexOf(Integer[] array, Integer value){
        int index = -1;
        for (int i  = 0; i < array.length; i++){
            if(array[i].equals(value)){
                index = i;
                break;
            }
        }
        return index;
    }

    public Integer[] copyArrayByIterator(Integer[] array) {
        Integer[] newArray = new Integer[array.length];
        int i = 0;
        Iterator<Integer> bs = Arrays.asList(array).iterator();
        while (bs.hasNext()){
            newArray[i] = bs.next();
            i++;
        }
       return newArray;

    }

    public Integer[] copyArrayByLoop(Integer[] array){
        Integer[] newArray = new Integer[array.length];
        int j = 0;

        for (int i = 0; i < array.length; i++){
            newArray[j] = array[i];
            j++;
        }
        return newArray;

    }


    public Integer[] removeElementFromArray(Integer[] array, Integer value){
        Integer[] newArray = new Integer[array.length-1];
        int index = 0;
        for (int i = 0; i < array.length; i++){
            if(!array[i].equals(value)){
                newArray[index] = array[i];
                index++;
            }

        }
        return newArray;
    }

    public Integer[] insertIntoArrayAt(Integer[] array, Integer value, int position){
        Integer[] newArray = new Integer[array.length+1];
        int index = 0;
        for (int i = 0; i <= array.length; i++){
            if(i < position){
                newArray[index] = array[i];
                index++;
            }else if(i == position){
                newArray[index] = value;
                index++;
            }else if(i > position){
                newArray[index] = array[i-1];
                index++;
            }
        }
        return newArray;
    }

    public MaxMin findMaxMinOfArray(Integer[] array){
        int min = array[0];
        int max = array[0];

        for (int i = 0; i < array.length; i++){
            if (array[i] > max){
                max = array[i];
            }else if (array[i] < min){
                min = array[i];
            }

        }

        return new MaxMin(max, min);

    }

    public Integer[] removeDupesFromArray(Integer[] array){
        HashSet<Integer> hs = new HashSet<Integer>();
        ArrayList<Integer> al = new ArrayList<Integer>();
        for (int i = 0; i < array.length; i++) {
            if (hs.add(array[i])){
                al.add(array[i]);
            }
        }

         return makeMeAnArrayFromArrayList(al);

    }

   public double find2ndLargestValueFromArray(double[] array){
        Arrays.sort(array);
        return array[array.length-2];
   }

   public ArrayList<Integer> makeMeAnArrayListFromArray(Integer[] array){
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.addAll(Arrays.asList(array));
        return list;
   }

   public Integer[] makeMeAnArrayFromArrayList(ArrayList<Integer> list){
        Integer[] array = new Integer[list.size()];
        int i = 0;
        for (Integer integer : list){
            array[i] = integer;
            i++;
        }
        return array;
   }

   public boolean check2ArraysForEqual(Integer[] array1, Integer[] array2){
        boolean equal = false;

        for (int i = 0; i < array1.length; i++) {
                if (array1[i].equals(array2[i])) {
                    equal = true;
                }else{
                    equal = false;
                }
        }

        return equal;
   }

   public int averageArrayWithoutMaxMin(Integer[] array){
       Integer min = array[0];
       Integer max = array[0];

       for (int i = 0; i < array.length; i++){
           if (array[i] > max){
               max = array[i];
           }else if (array[i] < min){
               min = array[i];
           }

       }

       return averageOfArray(removeElementFromArray(removeElementFromArray(array, min), max));



   }

   public boolean arrayHas65and77(Integer[] array){
        boolean contains = false;
        if (containsValue(array, 65) && containsValue(array, 77)){
            contains = true;
        }

        return contains;
   }

   public boolean theTotalofTensIs30(Integer[] array){
        int total = 0;
        boolean total30 = false;
        for (int i = 0; i < array.length; i++){
            if (array[i] == 10){
                total += array[i];
            }
        }
        if(total == 30){
            total30 = true;
        }
        return total30;
   }

   public Integer[] findSmallestAndSecondSmallest(Integer[] array){
        Integer[] newArray = new Integer[2];
        Arrays.sort(array);
        newArray[0] = array[0];
        newArray[1] = array[1];

        return newArray;

   }

   public Integer[] makeMeACopyPlease(Integer[] array){
        return reverseArray(copyArrayByLoop(array));
   }

   public Integer[] removeLastItemAndCopy(Integer[] array){

        return Arrays.copyOfRange(array, 0, array.length-1);
   }

   public Integer[] removeFirstItemAndCopy(Integer[] array){
       return Arrays.copyOfRange(array, 1, array.length);
   }

   public Integer[] insertAtStartAndCopy(Integer[] array, Integer value){
        return copyArrayByLoop(insertIntoArrayAt(array, value, 0));
   }

   public Integer[] insertAtEndAndCopy(Integer[] array, Integer value){
       return copyArrayByLoop(insertIntoArrayAt(array, value, array.length));
   }

   public Integer[] sortArrayIntoEvensThenOdds(Integer[] array){
        Integer[] newArray = new Integer[array.length];
        int newArrayIndex = 0;
       for (int i = 0; i < array.length; i++) {
           if (array[i] % 2 == 0) {
               newArray[newArrayIndex] = array[i];
                newArrayIndex++;
           }
       }

       for (int i = 0; i < array.length; i++){
           if (array[i] % 2 != 0) {
               newArray[newArrayIndex] = array[i];
               newArrayIndex++;
           }

       }

       return newArray;
   }






}
