Question
import java.util.ArrayList; import java.util.Arrays; /* * Write a method removeBadPairs that accepts an ArrayList of integers and removes any adjacent pair * of integers in
import java.util.ArrayList; import java.util.Arrays;
/* * Write a method removeBadPairs that accepts an ArrayList of integers and removes any adjacent pair * of integers in the list if the left element of the pair is larger than the right element of the pair. * Every pair's left element is an even-numbered index in the list, and every pair's right element is an odd index in the list. * For example, suppose a variable called list stores the following element values: * [3, 7, 9, 2, 5, 5, 8, 5, 6, 3, 4, 7, 3, 1] * We can think of this list as a sequence of pairs: (3, 7), (9, 2), (5, 5), (8, 5), (6, 3), (4, 7), (3, 1). * The pairs (9,2), (8, 5), (6, 3), and (3, 1) are "bad" because the left element is larger than the right one, so these pairs * should be removed. So the call of removeBadPairs(list); would change the list to store the following element values: * [3, 7, 5, 5, 4, 7] * If the list has an odd length, the last element is not part of a pair and is also considered "bad;" it should * therefore be removed by your method. * If an empty list is passed in, the list should still be empty at the end of the call. You may assume that the * list passed is not null. * * You may not use any additional arrays or arraylists to solve this problem!! */
public class RemoveBadPairs2 { public static void main(String[] args) { int[] values = {3, 7, 9, 2, 5, 5, 8, 5, 6, 3, 4, 7, 3, 1}; ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started