logo Practice-It logo


Language/Type: Java ArrayList Collections
Related Links:
Author: Allison Obourn (on 2012/08/11)

Write a method makeConsecutiveByN that could be added to the ArrayIntList class that takes an integer n as a parameter, removes any list element that does not differ from the previous element by exactly n. For example, both 8 and 2 differ from 5 by exactly 3. Your method should return true if the list was changed and false if not. For example, suppose elementData stores the following element values: [1, 3, -6, 1]. We need to compare every set of pairs. If we remove an element we need to compare the next element in the list to the last non-removed element. If n were 2 and we made a call on the previous list we would compare 1 and 3, keep 3 and then compare 3 and -6. We would discard -6 and so then compare 3 and the last 1. Our call would return true. You may not use any other arrays, lists, or other data structures to help you solve this problem, though you can create as many simple variables as you like.

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.