

import java.util.*;

public class Arraz {
    public   Integer  sumValuesOfArray(Integer [] arr){
        Integer sum = 0 ;
        for (Integer i: arr) {
            sum += i;

        }
        return sum;

    }
    public  double sumDoublesOfArray(double [] arr){
        double sum = 0;
        for (double i: arr) {
            sum += i;
        }
            return sum;

    }
    public  Integer averageOfArray( Integer [] arr){


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

    public  boolean containValue( Integer [] arr, int number){
        for (int i : arr) {

            if (i == number ){
                return true;
            }
        }

        return false;
    }

    public  Integer [] reverseArray (Integer [] arr){
       List <Integer> list = Arrays.asList(arr);
        Collections.reverse(list);


        return makeMeAnArrayFromArrayList(list);
    }

    public  OddEven getOddEvensOfArray(int [] arr){

        int odd = 0;
        int even = 0;

        for (int i:arr) {
            if (i % 2 == 0){
                odd ++;
            }else {
                even ++;
            }

        }
        return new OddEven(odd,even) ;

    }

    public  Integer findIndexOf( Integer [] arr, Integer value){

     int num = 0;
        for (int i = 0; i <arr.length-1 ; i++) {


            if (value == arr[i]){
                num = i;

            }

        }
        return num;
    }
    public  Integer findValueAt( Integer [] arr, int index){

        int num = 0;
        for (int i = 0; i <arr.length-1 ; i++) {


            if (index == i){
                num = arr[i];

            }

        }
        return num;
    }

    public  Integer [] copyArrayByInterator( Integer [] arr){
        Iterator <Integer> integerIterator = Arrays.asList(arr).iterator();
        Integer [] copyArr = new Integer[arr.length];
        int index=0;

        while (integerIterator.hasNext()){
            copyArr [index] = integerIterator.next();
            index++;


        }

   return copyArr;
    }

    public int [] copyArrayByloop( int [] arr){
        int [] copyArr = new int [arr.length];

        int index = 0;
        for (int i = 0; i <arr.length ; i++) {
            copyArr[index] = arr [i];
            index++;


        }
        return copyArr;
    }

    public  Integer [] removeElemetFromArray( Integer [] arr, int value){
        int size = arr.length;

        int index = findIndexOf(arr,value);

        for (int i = index; i < size -1; i++) {

            arr[i] = arr[i+1];
        }
        size = size - 1;

        Integer[] copyarr = Arrays.copyOf(arr,size);
        return copyarr;

    }

    public  Integer [] insertIntoArrayAt(Integer [] arr, Integer num, int index){
   Integer [] result = new Integer[arr.length+1];
        for (int i = 0; i <result.length ; i++) {
            if (i < index){
                result [i] =arr[i];
            }else if (i ==index){
                result [i]=  num;

            }else {
                result [i]= arr[i-1];
            }

        }
   return result ;
    }

   public MaxMin findMaxMinOfArray(Integer [] arr ){
     int  max = arr[0];
     int min = arr [0];
       for (Integer i: arr) {
           if (arr[i]> max){
               max = arr[i];
           } else if (arr[i]< min){
               min = arr[i];
           }

       }

        return new MaxMin(max,min);
   }


    public  Integer [] removeDupesFromArray( Integer [] arr) {
        List <Integer> list = new ArrayList<Integer>();
        list.add(arr[0]);
        for (Integer i: arr) {
            if(!list.contains(i)){
                list.add(i);
            }

        }
        return makeMeAnArrayFromArrayList(list);
    }


 public  double find2ndLargestValueFromArrayDouble( double [] arr){
        double largest = arr[0];
        double secondLargest = arr[0];

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

         if(arr[i]> largest){
             secondLargest = largest;
             largest = arr[i];
         } else if (arr[i]> secondLargest){
             secondLargest = arr [i];
         }

     }


        return secondLargest ;
   }


   public   List<Integer> makeMeAnArrayListFromArray( Integer [] arr){

        return Arrays.asList(arr);

    }

    public  Integer [] makeMeAnArrayFromArrayList(List <Integer> list){
        return list.toArray(new Integer[list.size()]);




       }

    public  boolean check2ArraysForEqual( int [] array , int [] arr){


        return Arrays.equals(array,arr);
    }
    public Integer averageArrayWithoutMaxMin(Integer [] arr){
        MaxMin maxMin = findMaxMinOfArray(arr);
        Integer[] removeMax = removeElemetFromArray(arr, maxMin.getMax());
        Integer [] result = removeElemetFromArray(removeMax, maxMin.getMin());

        return averageOfArray(result);
    }

    public  boolean arrayHas65and77( Integer [] arr) {

            if (containValue(arr,65) &&
                    containValue(arr, 77)) {
                return true;
            }


        return false;
    }

    public  boolean theTotalofTensIs30(int []arr){
       int sum = 0;
       for(int i: arr){

       if(i == 10) sum +=i;

       if (sum ==30)return true;

       }
       return false;
    }


    public  int  findSmallerAndSecondSmaller( Integer [] arr){
        MaxMin maxMin = findMaxMinOfArray(arr);
        Integer [] removeMin = removeElemetFromArray( arr,maxMin.getMin());
         MaxMin secondSmaller = findMaxMinOfArray(removeMin);


        return   maxMin.getMin() + secondSmaller.getMin() ;
    }



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

        return Arrays.copyOf(arr, arr.length-1);
        }

    public  Integer[] removeFirstItemAndCopy(Integer [] arr) {
        return removeElemetFromArray(arr, arr[0]);
    }



    public  Integer [] insertAtStartAndCopy(Integer [] arr, int value){

            return insertIntoArrayAt(arr,value,0);
        }




    public Integer [] insertAtEndAndCopy (Integer [] arr, Integer value){


        return insertIntoArrayAt(arr,value , arr.length);
    }
    public Integer [] sortArrayIntoEvensThenOdds(Integer [] arr){

      List <Integer> even = new ArrayList<Integer>();
      List <Integer> odd = new ArrayList<Integer>();
        for ( Integer i: arr) {
            if (i %2 ==0){
                even.add(i);
            } else {
                odd.add(i);

            }

        }
        Collections.sort(even);
        Collections.sort(odd);
        even.addAll(odd);
        return even.toArray(new  Integer[even.size()]);
    }


}

