import help.OddEven;

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

public class Arraz {

    public int sumValuesOfArray(int[] a){
        int sum = 0;
        for (int i : a)
            sum += i;

        return sum;
    }

    public double sumDoubleofArray(double[] a){
        double sum = 0;
        for (double i : a)
            sum += i;

        return sum;
    }

    public int averageOfArray(int[] a){
        int sum = 0;
        for (int i : a)
            sum+=i;
        return sum/a.length;
    }

    public double doubleAverageOfArray(double[] a){
        double sum = 0;
        for (double i : a)
            sum+=i;
        return sum/a.length;
    }

    public boolean containsValue(int[] a, int val){
        for (int i : a)
            if (i == val)
                return true;
        return false;
    }

    public OddEven oddEven(int[] a){
        int odd = 0;
        int even = 0;
        for (int i : a) {
            if (i % 2 == 0)
                even++;
            else odd++;
        }

        return new OddEven(odd,even);
    }

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

    public Integer[] copyArrayByIterator(Integer[] a){
        List<Integer> l = Arrays.asList(a);
        List<Integer> b = new ArrayList<Integer>();
        Iterator<Integer> it = l.iterator();
        while (it.hasNext()){
            b.add(it.next());
        }

        return b.toArray(new Integer[0]);
    }

    public int[] copyArrayByLoop(int[] a){
        int[] b = new int[a.length];
        for (int i=0; i<a.length;i++)
            b[i] = a[i];
        return b;
    }

    public Integer[] removeElementFromArray(Integer[] a, Integer b){
        int idx = findIndexOf(a,b);
        Integer[] copy = new Integer[a.length -1];
        for (int i=0; i<a.length; i++){
            if (i < idx)
                copy[i] = a[i];
            else if (i > idx)
                copy[i-1] = a[i];
        }
        return copy;
    }

    public Integer[] removeDupesFromArray(Integer[] a){
        StringBuilder sb = new StringBuilder();

        for (int i=0; i<a.length; i++)
            for (int j=i+1; j<a.length-1; j++)
                if (a[i].equals(a[j])) {
                    sb.append(a[i]).append("-");
                    break;
                }

        String[] strArr = sb.toString().split("-");
        for (String str : strArr) {
            a = removeElementFromArray(a, Integer.valueOf(str));
        }
        return a;
    }

    public Double find2ndLargestValueFromArray(Double[] d){
        Double max = Double.MIN_VALUE;
        Double min = Double.MIN_VALUE;

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

        return min;
    }

    public List<Integer> makeArrayListFromArray(Integer[] a){
        List<Integer> l = Arrays.asList(a);
        return l;
    }

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

    public boolean arrayHas65and77(Integer[] a){
        boolean has65 = false;
        boolean has77 = false;
        for (Integer i:a){
            if (i == 65) has65 = true;
            else if (i == 77) has77 = true;
        }
        return has65 && has77;
    }

    public boolean theTotalofTensIs30(Integer[] a) {
        int count = 0;
        for (Integer i : a)
            if (i == 10)
                count++;
        return count == 3;
    }

    public Integer[] findTwoSmallest(Integer[] a){
        Integer small = Integer.MAX_VALUE;
        Integer secSmall = Integer.MAX_VALUE;

        for (int i=0; i<a.length; i++){
            if (a[i] < small){
                secSmall = small;
                small = a[i];
            } else if (a[i] < secSmall && a[i] > small)
                secSmall = a[i];
        }

        return new Integer[]{small, secSmall};
    }

    public Integer[] makeMeACopyPlease(Integer[] a){
        Integer[] b = new Integer[a.length];
        int j=0;
        for (int i=a.length-1; i>=0; i--){
            b[j] = a[i];
            j++;
        }
        return b;
    }

    public Integer[] removeLastItemAndCopy(Integer[] a){
        Integer[] b = new Integer[a.length-1];
        for (int i=0; i<a.length-1; i++){
            b[i] = a[i];
        }
        return b;
    }

    public Integer[] removeFirstItemAndCopy(Integer[] a){
        Integer[] b = new Integer[a.length-1];
        for (int i=1; i<a.length; i++){
            b[i-1] = a[i];
        }
        return b;
    }

    public Integer[] insertAtStartAndCopy(Integer[] a, Integer insert){
        Integer[] b = new Integer[a.length+1];
        b[0] = insert;
        for (int i=0; i<a.length; i++)
            b[i+1] = a[i];
        return b;
    }

    public Integer[] insertAtEndAndCopy(Integer[] a, Integer insert){
        Integer[] b = new Integer[a.length+1];
        for (int i=0; i<a.length; i++)
            b[i] = a[i];
        b[b.length-1] = insert;
        return b;
    }

    public Integer[] sortArrayIntoEvensThenOdds(Integer[]a){
        StringBuilder evens = new StringBuilder();
        StringBuilder odds = new StringBuilder();

        for (Integer i : a) {
            if (i % 2 == 0)
                evens.append(i + " ");
            else odds.append(i + " ");
        }

        evens.append(odds);
        String[] s = evens.toString().split(" ");
        Integer[] split = new Integer[s.length];

        for (int i=0; i<s.length; i++)
            split[i] = Integer.valueOf(s[i]);
        return split;
    }


}
