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

public class Arraz {

    public int sumValuesOfArray(int[] arr) {
        int output = 0;

        for (int anArr : arr) {
            output += anArr;
        }

        return output;
    }

    public double sumDoublesOfArray(double[] arr) {
        double output = 0;

        for (double anArr : arr) {
            output += anArr;
        }

        return output;
    }

    public int averageOfArray(int[] arr) {
        return sumValuesOfArray(arr) / arr.length;
    }

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

    public boolean containsValue(int[] arr, int n) {
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == n){
                return true;
            }
        }
        return false;
    }

    public int[] reverseArray(int[] arr) {
        int[] output = new int[arr.length];
        int counter = 0;
        for(int i = arr.length - 1; i >= 0; i--){
            output[counter] = arr[i];
            if(i != 0) counter++;
        }
        return output;
    }

    public String getOddEvensOfArray(int[] arr){
        int even = 0;
        int odd = 0;

        for (int anArr : arr) {
            if (anArr % 2 == 0) {
                even++;
            } else {
                odd++;
            }
        }
        return "odds: " + odd + " evens: " + even;
    }

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

    public int[] copyArrayByLoop(int[] arr) {
        int[] output = arr;
        return output;
    }

    public Integer[] copyArrayByInterator(Integer[] arr){
        Integer[] output = new Integer[arr.length];
        int count = 0;
        Iterator iterator = Arrays.asList(arr).iterator();
        while(iterator.hasNext()){
            output[count] = Integer.parseInt(iterator.next().toString());
            count++;
        }
        return output;
    }

    public int[] removeElementFromArray(int[] arr, int n) {
        if(arr.length != 0) {
            int count = 0;
            int[] output = new int[arr.length - 1];
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != n) {
                    output[count] = arr[i];
                    count++;
                }
            }
            return output;
        }
        return new int[] {-1};
    }

    public int[] InsertIntoArray(int[] arr, int n) {
        int[] output = new int[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            output[i] = arr[i];
        }
        output[arr.length] = n;
        return output;
    }

    public String findMaxMinOfArray(int[] arr) {
        int min = arr[0];
        int max = arr[0];

        for(int i = 1; i < arr.length; i++){
            if(arr[i] < min){
                min = arr[i];
            }

            if(arr[i] > max){
                max = arr[i];
            }
        }
        return "min: " + min + " max: " + max;
    }

    public int findMax(int[] arr){
        int max = arr[0];
        for(int i = 1; i < arr.length; i++){
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }

    public int findMin(int[] arr){
        int min = arr[0];
        for(int i = 1; i < arr.length; i++){
            if(arr[i] < min){
                min = arr[i];
            }
        }
        return min;
    }

    public int[] removeDupesFromArray(int[] arr) {
        List dupes = collectDupes(arr);
        int count = 0;
        int[] output = new int[arr.length - dupes.size()];
        for(int i = 0; i < arr.length; i++){
            if(!dupes.contains(arr[i])){
                output[count] = arr[i];
                count++;
            }
        }

        for(int j = 0; j < output.length; j++){
            System.out.println(output[j]);
        }

        return output;
    }

    public List collectDupes(int[] arr){
        List dupes = new ArrayList<Integer>();
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr.length; j++){
                if(arr[i] == arr[j] && i != j){
                    dupes.add(arr[i]);
                    break;
                }
            }
        }
        return dupes;
    }

    public int[] sort(int[] arr){
        int temp;
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr.length; j++){
                if(arr[i] < arr[j] && i != j){
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }

    public int find2ndLargestValueFromArray(int[] arr) {
        sort(arr);
        return arr[arr.length - 2];
    }

    public List makeMeAnArrayListFromArray(int[] arr){
        List output = new ArrayList();
        for(int i = 0; i < arr.length; i++){
            output.add(arr[i]);
        }
        return output;
    }


    public Object[] makeMeAnArrayFromArrayList(List<Integer> given) {
        return given.toArray();
    }

    public boolean check2ArraysForEqual(int[] arr1, int[] arr2) {
        if(arr1.length == arr2.length) {
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] != arr2[i]) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public int averageArrayWithoutMaxMin(int[] arr) {
        int[] withOutMax = removeElementFromArray(arr, findMax(arr));
        int[] withOutMin = removeElementFromArray(withOutMax, findMin(arr));
        return averageOfArray(withOutMin);
    }

    public boolean arrayHas65and77(int[] arr) {
        if(containsValue(arr, 65) && containsValue(arr, 77)) return true;
        return false;
    }

    public boolean theTotalofTensIs30(int[] arr) {
        int tens = 0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == 10) tens++;
        }
        if(tens == 3) return true;
        return false;
    }

    public String get2SmallestNumbers(int[] arr) {
        int[] arr2 = sort(arr);
        return "smallest: " + findMin(arr) + ", 2nd smallest: " + arr2[1];
    }

    public int[] makeMeACopyPlease(int[] arr){
        int[] output = new int[arr.length];
        int count = 0;
        for(int i = output.length - 1; i >= 0; i--){
            output[count] = arr[i];
            count++;
        }
        return output;
    }

    public int[] removeLastItemAndCopy(int[] arr) {
        int[] remove = removeElementFromArray(arr, arr[arr.length - 1]);
        int[] output = copyArrayByLoop(remove);
        return output;
    }


    public int[] removeFirstItemAndCopy(int[] arr) {
        int[] arr2 = new int[arr.length - 1];
        for (int i = 1; i < arr.length; i++) {
            arr2[i - 1] = arr[i];
        }
        return copyArrayByLoop(arr2);
    }

    public int[] insertAtStartAndCopy(int[] arr, int n) {
        int[] output = new int[arr.length + 1];
        output[0] = n;
        for(int i = 1; i < output.length; i++){
            output[i] = arr[i - 1];
        }
        return copyArrayByLoop(output);
    }

    public int[] insertAtEndAndCopy(int[] arr, int n) {
        int[] output = new int[arr.length + 1];
        output[arr.length] = n;
        for(int i = 0; i < arr.length; i++){
            output[i] = arr[i];
        }
        return copyArrayByLoop(output);
    }

    public int countEven(int[] arr){
        int even = 0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] % 2 == 0) even++;
        }
        return even;
    }

    public int countOdd(int[] arr){
        int odd = 0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] % 2 != 0) odd++;
        }
        return odd;
    }

    public int[] createEvenArray(int[] arr){
        int[] evens = new int[countEven(arr)];
        int evenCount = 0;
        for (int anArr : arr) {
            if (anArr % 2 == 0) {
                evens[evenCount] = anArr;
                evenCount++;
            }
        }
        return evens;
    }

    public int[] createOddArray(int[] arr){
        int[] odds = new int[countOdd(arr)];
        int oddCount = 0;
        for (int anArr : arr) {
            if (anArr % 2 != 0) {
                odds[oddCount] = anArr;
                oddCount++;
            }
        }
        return odds;
    }

    public int[] mergeIntArrays(int[] arr1, int[] arr2){
        int [] output = new int[arr1.length + arr2.length];
        for(int k = 0; k < output.length; k++){
            if(k < arr1.length){
                output[k] = arr1[k];
            }else{
                output[k] = arr2[k - arr1.length];
            }
        }
        return output;
    }

    public int[] sortArrayIntoEvensThenOdds(int[] arr){
        int[] evens = sort(createEvenArray(arr));
        int[] odds = sort(createOddArray(arr));
        return mergeIntArrays(evens, odds);
    }
}