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

I need help writing code for this assignment (java): 1. X104: fix45 - Given an a

ID: 3715571 • Letter: I

Question

I need help writing code for this assignment (java):

1. X104: fix45 - Given an array, return an array that contains exactly the same numbers as the original array, but rearranged so that every 4 is immediately followed by a 5. Do not move the 4's, but every other number may move. The array contains the same number of 4's and 5's, and every 4 has a number after it that is not a 4. In this version, 5's may appear anywhere in the original array. (This is a slightly harder version of the fix34 problem.)

2. X103: countClumps - Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.

3. X100: zeroMax - Write a function in Java that implements the following logic: Return a version of the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array. If there is no odd value to the right of the zero, leave the zero as a zero. Hint: in solving this: you may use a second helper function if you want, say to find the largest odd value to the right of a specified position.

Explanation / Answer

public static void fix45(int[] data){

     for (int i = 0; i<data.length; i++){
         if (data[i] == 4 && i != data.length-1){
            if (data[i+1] == 4){
               for (int j = 1; j<data.length; j++){
                  if (data[j-1] != 4 && data[j] != 4){
                     int temp = data[i+1];
                     data[i+1] = data[j];
                     data[j] = temp;
                     break;
                  }
               }
            }
         }
     }
}

public static int countClumps(int[] data){

    int i = 0;
    int countSlumps = 0;
    while (i < data.length){
       j = i+1;
       while (data[j] == data[i]){
          j++;
       }
       if (j-1-i >= 1)
           countSlumps++;
       i = j;     
    }
    return countSlumps;
}
public static int[] zeroMax(int[] data){

    for (int i = 0; i<data.length; i++){
       if (data[i] == 0){
           int max = -1;
           int found = 0;
           for (int j = i+1; j<data.length; j++){
              if (data[j] % 2 != 0){
                 if (data[j] > max){
                    found = 1;
                    max = data[j];
                 }
              }
           }
           if (found == 1){
              data[i] = max;
           }
          
       }
    }
    return data;
}