Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
2.1 kB
2
Indexable
Never
public static ArrayList<Integer> createNewList(ArrayList<Integer> list) {
        ArrayList<Integer> answerList = new ArrayList<>();
        ArrayList<Integer> answerListTemp = new ArrayList<>();
        int i = 1;

        while(i<list.size()){

            if(list.get(i-1)<=list.get(i)) {
                if(answerListTemp.isEmpty())
                    answerListTemp.add(list.get(i-1));
                answerListTemp.add(list.get(i));
            } else {
                sort(answerListTemp, answerList);
            }
            i++;
        }
        if (!answerListTemp.isEmpty()) {
            if (answerListTemp.size() > answerList.size()) {
                return answerListTemp;
            } else {
                if (answerList.size() == answerListTemp.size())
                    compareSum(answerListTemp, answerList);
            }
        }
        return answerList;
    }
    public static void sort(ArrayList<Integer> answerListTemp, ArrayList<Integer> answerList) {
        if(!answerListTemp.isEmpty()) {
            if(!answerList.isEmpty()){
                if(answerList.size()<answerListTemp.size()){
                    answerList.clear();
                    answerList.addAll(answerListTemp);
                    answerListTemp.clear();
                } else if (answerList.size()==answerListTemp.size())
                    compareSum(answerListTemp, answerList);
                answerListTemp.clear();
            } else {
                answerList.addAll(answerListTemp);
                answerListTemp.clear();
            }
        }
    }
    public static void compareSum(ArrayList<Integer> answerListTemp, ArrayList<Integer> answerList) {
        double sum = 0;
        double sumTemp = 0;
        for (int j = 0; j < answerList.size(); j++) {
            sum += answerList.get(j);
            sumTemp += answerListTemp.get(j);
        }
        if(sum<sumTemp) {
            answerList.clear();
            answerList.addAll(answerListTemp);
            answerListTemp.clear();
        }
    }