Problem 1: Write a method called removeLessThanAverage that accepts a stack of i
ID: 3557997 • Letter: P
Question
Problem 1: Write a method called removeLessThanAverage that accepts a stack of integers as a parameter and removes all values that are less than the average of values in the stack. For example, if the stack stores [2, 8, 3, 19, 2, 3, 2, 7], your method should remove all values less than 5.75 (average), leaving the stack storing [8, 19, 7, 12]. Use one queue as auxiliary storage. Problem 2: Add a method to reverseQueue a Queue. Once reversed, the order of the elements on the queue are reversed. For example, if the queue contains: ['one', 'two', 'three', 'four'], the reversed queue will be: ['four', 'three', 'two', 'one']
Explanation / Answer
public static void removeLessThanAverage(ArrayList<Integer> list) {
for (int i = list.size() - 1; i > 0; i--) {
if (list.get(i) < list.get(i - 1)) {
int element = list.get(i);
list.remove(i);
list.add(0, element);
}
}
System.out.println(list);
}