Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method cleanCorruptData that accepts an Array List of integers and remov

ID: 3784371 • Letter: W

Question

Write a method cleanCorruptData that accepts an Array List of integers and removes any adjacent pair of integers in the list if the left element of the pair is smaller 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, 5, 5, 8, 5, 6, 3, 4, 7] We can think of this list as a sequence of pairs: (3, 7), (5, 5), (8, 5), (6, 3), (4, 7). The pairs (3, 7) and (4, 7) are "bad" because the left element is smaller than the right one, so these pairs should be cleaned (or removed). So the call of cleanCorruptData (list); would change the list to store the following element values: [5, 5, 8, 5, 6, 3] If the list has an odd length, the last element is not part of a pair and is also considered "corrupt;" it should therefore be cleaned 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 other arrays, lists, or other data structures to help you solve this problem.

Explanation / Answer

Hi,

Here's the code for the problem. I have explained each step in the comments.

import java.util.ArrayList;
import java.util.Scanner;
class Sample{
    public static void main(String args[]){
        int n;
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> al = new ArrayList<Integer>();//initializing arraylist
        System.out.println("Enter -1 at the end of the list");
        System.out.print("Enter elements:");
        n = sc.nextInt();
        while(n!=-1){
            al.add(n); //adding elements to arraylist
            n = sc.nextInt();
        }
        Sample c = new Sample();
        System.out.println("After cleaning corrupt data:");
        for(Integer a:c.cleanCorruptData(al)){ //calling cleanCorruptData which returns arraylist and iterating over it
            System.out.println(a); //printing elements of the resulting arraylist
        }
    }
    public ArrayList<Integer> cleanCorruptData(ArrayList<Integer> al){
        if(al.size()==0) //elementary condition, if the size is zero, just return from the method
            return al;
        int i=0;
        while(i<al.size()-1){ //iterating till the size-1 to manipulate between even and odd sizes
            if(al.get(i)<al.get(i+1)){ // checking if the pair is bad i.e. if the left element is less than right element
                al.remove(i); //removing ith element
                al.remove(i); //removing ith element again
                /* why we are removing ith element twice, please note that in arraylist, if we remove
                a element, the arraylist will reindex by moving each element to it's left, let's
                take an example {1,2,3} and we removed 1, then it becomes {2,3} where index of 2
                will be now 0 and 3 will be 1. If we want to remove 2, we should say al.remove(0) */
            }
            else{ //if the elements are not bad we are moving to next pair
                i+=2;
            }
            /*also note here that if the pair is bad, we are not incrementing i, because the
             * arraylist resizes itself. again take an example {5,8,10,6}, in the first iteration,
             * when i=0, we remove 5,8 then arraylist becomes {10,6} now at i=0 and i=1, 10 and 6
             * are present respectively. So, we shouldn't increment i.
             * */
        }
        if(al.size()%2 != 0) //necessary condition if the arraylist is odd, we should remove the alone element also
            al.remove(i);
        return al; // finally returning arraylist
    }
}


Below are the sample test cases that were passed in my system:

Enter -1 at the end of the list
Enter elements:7
5
5
8
5
6
3
4
7
-1
After cleaning corrupt data:
7
5

Enter -1 at the end of the list
Enter elements:4
5
2
6
7
18
24
62
4
1
5
4
2
7
2
4
6
3
7
27
3
5
-1
After cleaning corrupt data:
4
1
5
4
6
3

Enter -1 at the end of the list
Enter elements:8
2
5
2
8
9
1
12
13
10
3
4
5
7
9
-1
After cleaning corrupt data:
8
2
5
2
13
10

Here's the ideon of the code:

http://ideone.com/1gyHpM

Thanks,

Lokesh.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote