简体   繁体   中英

Array values greater than the right?

I am making a method that takes in an ArrayList and returns an ArrayList of integers that are greater than all of the values to the right of them.

For example, greaterRight([1, 4, 2, 3, 6, 1, 0]) should output [6, 1]

Here is what I have so far:

    public static ArrayList<Integer> 
greaterRight(ArrayList<Integer> arr)
{
//values must be larger than ALL of the right
int test = 0;

ArrayList<Integer> newArr = new ArrayList<Integer>();
for(int i = 1; i < arr.size()-1; i++){
    if(arr.get(0 + test) > arr.get(i)){
        newArr.add(arr.get(i));
    }
    test++;
}

return newArr;


}

I am not sure why it is not working as intended.

You need to have two for loops, you are using one, try this code:

public static List<Integer> greaterRight(List<Integer> list) {
    List<Integer> returnedList = new ArrayList<>();

    for (int i = 0; i < list.size(); i++) {
        Integer n = list.get(i);
        for (int j = i + 1; j < list.size(); j++) {
            if (n < list.get(j)) {
                //Number failed
                break;
            } else {
                if (j == list.size() - 1) {
                    //last element reached and the number is still bigger than anyone to the right
                    returnedList.add(list.get(j));
                }
            }
        }
    }

    return returnedList;
}

Notice your cycle starts at i=1 and you include elements in the result array by doing arr.get(i) . This alone makes the algorithm incorrect since arr.get(0) is never tested for inclusion in the result. Also it seems to me that in the cycle you are only testing an element and the one that's right before it and not all the are to the right.

The simpler solution to this i think is to have 2 nested for cycles. The outer cycle iterates over each element in the array to check its possible inclusion in the result. The inner cycle actually checks if all the values after are greater (or equal) to the one being checked:

public static ArrayList<Integer>greaterRight(ArrayList<Integer> arr){

//values must be larger than ALL of the right
int size = arr.size()
ArrayList<Integer> newArr = new ArrayList<Integer>();

// In each iteration of the outer cycle we check if arr.get(i) must be included
// in the result
for(int i = 0; i < size-1; i++){

    // Helper variable to check if all values are greater
    boolean allGreater = true;

    // Check all values after 
    for(j=i+1; j < (size-1) && allGreater; j++){
        if(arr.get(j) < arr.get(i)) {
            // We found a value after arr.get(i) that is lesser
            // Setting all greater to false will cause the inner cycle
            // to exit and for the element to not be included
            allGreater = false;
        }
    }

    // If the previous cycle ended because all values after arr.get(i)
    // are bigger, add arr.get(i) to the final result
    if(allGreater) {
        newArr.add(arr.get(i));
    }
}

return newArr;


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM