import java.util.*;

public class Arraz {

    public int sumValuesOfArray(int[] array) {

        int sum = 0;

        for (int i = 0; i < array.length; i++) {

            sum += array[i];
        }
        return sum;

    }

    public double sumDoublesOfArray(double[] newArray) {

        double sum = 0;

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

        return sum;
    }


    public Integer averageOfArray(Integer[] newArray) {

        Integer result = 0;
        Integer finalResult = 0;

        for (int i = 0; i < newArray.length; i++) {

            result += newArray[i];
            finalResult = result / newArray.length;
        }

        return finalResult;
    }


    public double doubleAverageOfArray(double[] exampleArray) {
        double result = 0.0;
        double finalResult = 0.0;

        for (int i = 0; i < exampleArray.length; i++) {
            result += exampleArray[i];
            finalResult = result / exampleArray.length;
        }


        return finalResult;
    }

    public boolean containsValue(Integer[] x, int num) {

        for (int i = 0; i < x.length; i++) {
            if (x[i] == num)
                return true;

        }
        return false;
    }

    public Integer[] reverseArray(Integer[] x) {

        Integer[] newArray = new Integer[x.length];
        int counter = 0;
        for (int i = x.length - 1; i >= 0; i--) {
            newArray[counter] = x[i];
            counter++;


        }
        return newArray;

    }

    public OddEven getOddEvens(Integer[] array) {
        OddEven oddEven = new OddEven();
        for (int i = 0; i < array.length; i++)
            if (array[i] % 2 == 0) {
                oddEven.evens++;
            } else {
                oddEven.odds++;
            }
        return oddEven;
    }

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

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

    public Integer[] copyArrayByLoop(Integer[] array2) {
        Integer[] newArray = new Integer[array2.length];

        int counter = 0;

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


    }

    public Integer[] removeElementFromArray(Integer[] practArray, Integer valueToRemove) {
        if (containsValue(practArray, valueToRemove)) {

            Integer[] newArray = new Integer[practArray.length - 1];
            int counter = 0;
            for (int i = 0; i < practArray.length; i++) {
                if (practArray[i] != valueToRemove) {
                    newArray[counter] = practArray[i];
                    counter++;
                }
            }

            return newArray;
        } else {
            return practArray;
        }

    }

    public Integer[] insertIntoArray(Integer[] practicalArray, Integer index, Integer valueToAdd) {

        Integer[] newArray = new Integer[practicalArray.length + 1];

        for (int i = 0; i <= practicalArray.length; i++) {
            if (i < index) {
                newArray[i] = practicalArray[i];
            } else if (i == index) {
                newArray[i] = valueToAdd;
            } else {
                newArray[i] = practicalArray[i - 1];
            }
        }

        return newArray;


    }

    public Integer getMinValue(Integer[] array) {

        Integer minValue = array[0];

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


    }

    public Integer getMaxValue(Integer[] array) {

        Integer maxValue = array[0];

        for (int i = 0; i < array.length; i++) {
            if (array[i] > maxValue)
                maxValue = array[i];
        }
        return maxValue;

    }

    public MaxMin findMaxMinOfArray(Integer[] array) {


        MaxMin maxMin = new MaxMin(0,0);
        maxMin.max = getMaxValue(array);
        maxMin.min = getMinValue(array);
        return maxMin;

    }


    public Integer[] removeDuplicatesFromArray(Integer[] array) {
        Set<Integer> result = new LinkedHashSet<Integer>();
        for (int i = 0; i < array.length; i++) {
            result.add(array[i]);
        }
        //Copy elements from set back to the new Array
        Integer[] finalResult = new Integer[result.size()];
        int i = 0;
        for (Integer var : result) {
            finalResult[i++] = var;
        }
        return finalResult;
    }


    public Double find2ndLargestValueFromArray(Double[] array1) {
        Double largest = array1[0];
        Double secondLargest = array1[0];
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] > largest) {
                secondLargest = largest;
                largest = array1[i];
            } else if (array1[i] > secondLargest) {
                secondLargest = array1[i];
            }
        }
        return secondLargest;
    }

    public List<Integer> turnArrayToArrayList(Integer[] example) {
        List<Integer> finalResult = Arrays.asList(example);
        return finalResult;


    }

    public Integer[] makeMeAnArrayFromArrayList(List<Integer> arrayList) {

        Integer[] finalResult = arrayList.toArray(new Integer[arrayList.size()]);


        return finalResult;
    }

    public boolean check2ArraysForEqual(Integer[] example1, Integer[] example2) {

        for (int i = 0; i < example1.length; i++)
            if (example1[i] != example2[i]) {
                return false;
            }
        return true;
    }




    public Integer averageArratWithoutMaxMin(Integer[] arrayProvided) {
        MaxMin maxmin = new MaxMin(0,0);
        maxmin.findMaxMinOfArray(arrayProvided);
        int max = maxmin.getMax();
        int min = maxmin.getMin();
        Integer[] finalArray = new Integer[arrayProvided.length - 2];
        for (int i = 0, j = 0; i < arrayProvided.length; i++, j++)

            if (arrayProvided[i] != max && arrayProvided[i] != min)
                finalArray[i] = arrayProvided[i];
            else {
                j--;
            }
        return averageOfArray(arrayProvided);
    }

    public boolean arrayHas65and77(Integer[] example1) {

        for(int i =0; i<example1.length; i++){
            if(example1[i] == 65 && example1[i]==77)
                return true;
        }
            return false;
    }


    public boolean theTotalofTensIs30(Integer[] example1) {

            int counter=0;
        for( int i =0; i< example1.length; i++){
            if(example1[i] == 10)
                counter+=i;
                if(counter == 30)
                    return true;
            }
            return false;
        }

    public Integer[] findSmallestAndSecondSmallestOfArray(Integer[] example1) {

        Arrays.sort(example1);
        return new Integer[]{example1[0],example1[1]};
    }

    public Integer[] reverseACopyOfArray(Integer[] array1) {
        Integer[] finalArray = new Integer[array1.length];
        finalArray = reverseArray(array1);
        return finalArray;
    }

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

        return removeElementFromArray(array1, array1[array1.length-1]);
    }

    public Integer[] removeFirstItemAndCopy(Integer[] array1) {

        return removeElementFromArray(array1, array1[0]);

    }

    public Integer[] insertAtStartAndCopy(Integer[] array1, Integer num) {

        return insertIntoArray(array1, 0,  num);


    }

    public Integer[] insertAtEndAndCopy(Integer[] array1, Integer num) {

        return insertIntoArray(array1, array1.length, num);
    }


    public Integer[] sortArrayIntoEvensThenOdds(Integer[] array1) {
        Integer [] newArray = new Integer[array1.length];
        Arrays.sort(array1);
        int counter =0;

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

        return newArray;
    }
}


