Im trying to do some pratice problems in the book and here i

I\'m trying to do some pratice problems in the book and here is one of them.

THIS IS FOR JAVA.

Write a method cleanCorruptData that accepts an ArrayList 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.

THIS IS FOR JAVA.

Solution

/**************************ArrayListOperation.java***************************/

import java.util.ArrayList;

public class ArrayListOperation {

   public static ArrayList<Integer> cleanCorruptData(ArrayList<Integer> al) {
       // ArrayList instance creation
       ArrayList<Integer> temp = new ArrayList<Integer>();
       temp.add(Integer.MAX_VALUE);
       // if ArrayList is not empty
       if (!al.isEmpty()) {
           // if size of ArrayList is not even then remove last element
           if (al.size() % 2 != 0) {
               al.remove(al.size() - 1);
           }
           /**
           * loop for checking that if the left element of the pair is smaller
           * than the right element of the pair then remove that pair from
           * list
           */
           for (int i = 0; i < al.size(); i = i + 2) {
               int val1 = (int) al.get(i);
               int val2 = (int) al.get(i + 1);
               // if first value is less then next value then set value as max
               // value of integer
               // and end of the loop remove max value from Arraylist
               if (val1 < val2) {
                   al.set(i, Integer.MAX_VALUE);
                   al.set(i + 1, Integer.MAX_VALUE);
               }
           }
           // re
           al.removeAll(temp);
           return al;
       } else {
           return al;
       }

   }

   public static void main(String[] args) {
       // creating instance of ArrayList 3, 7, 5, 5, 8, 5, 6, 3, 4, 7
       ArrayList<Integer> al = new ArrayList<Integer>();
       al.add(3);
       al.add(7);
       al.add(5);
       al.add(5);
       al.add(8);
       al.add(5);
       al.add(6);
       al.add(3);
       al.add(4);
       al.add(7);

       // calling method
       ArrayList<Integer> result = ArrayListOperation.cleanCorruptData(al);
       System.out.println(result);
   }

}

/*************************output*************************/

[5, 5, 8, 5, 6, 3]

Thanks a lot

I\'m trying to do some pratice problems in the book and here is one of them. THIS IS FOR JAVA. Write a method cleanCorruptData that accepts an ArrayList of inte
I\'m trying to do some pratice problems in the book and here is one of them. THIS IS FOR JAVA. Write a method cleanCorruptData that accepts an ArrayList of inte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site