Question: You must only use a single loop inside the function question1. Do not use any number of nested loops. In our implementation, our solution is

You must only use a single loop inside the function question1. Do not use any number of nested loops. In our implementation, our solution is approximately 15 lines of code. You must call the swap function inside question1. You cannot use any kind of sorting to answer this question. Suppose we are given an array, A, of length n such that the indices of the array are from 0 to n  1. You can safely assume that the length of this array is at least 1. This array is further sorted in ascending order but possibly contains duplicates. You are now asked to move the elements around so that all of the values in the original array from index k to n  1 are in ascending order with all duplicates removed You must only use a single loop inside the function question1. Do import java.util.Arrays; public class Question1 { public static void question1(int[] items) { } /* * TODO: You are asked to complete the method * swap. This method takes in as input two ints * and an array of ints. The int i and int j * are the index i and index j in the array items. * You are asked to swap the value at the index i in items * with the value at the index j. You cannot change the function * signature of this method. This method must be called inside question1. */ private static void swap(int i,int j,int[] items) { } /* * Do not write any code inside the main method and expect it to get marked. * Any code that you write inside the main method will be ignored. However, * you are free to edit the main function in any way you like for * your own testing purposes. */ public static void main(String[] args) { //test case 0 int[] items={2,2,4,4,4,5,8,8}; //System.out.println(Arrays.toString(items)); Question1.question1(items); System.out.println(Arrays.toString(items)); //must print [2, 8, 4, 4, 2, 4, 5, 8] System.out.println("--------------------------"); //test case 1 int[] items1={1,2,2,2,2,2,2,2,2}; //System.out.println(Arrays.toString(items1)); Question1.question1(items1); System.out.println(Arrays.toString(items1)); //must print [2, 2, 2, 2, 2, 2, 2, 1, 2] System.out.println("--------------------------"); //test case 2 int[] items2={1,1,1,1,1,1,1,1,1}; //System.out.println(Arrays.toString(items2)); Question1.question1(items2); System.out.println(Arrays.toString(items2)); //must print [1, 1, 1, 1, 1, 1, 1, 1, 1] System.out.println("--------------------------"); //test case 3 int[] items3={1,2,3,4,5,6,7,8,9}; //System.out.println(Arrays.toString(items3)); Question1.question1(items3); System.out.println(Arrays.toString(items3)); //must print [1, 2, 3, 4, 5, 6, 7, 8, 9] System.out.println("--------------------------"); //test case 4 int[] items4={1,2,3,4,5,6,6,7,7,8,8,9,9}; //System.out.println(Arrays.toString(items4)); Question1.question1(items4); System.out.println(Arrays.toString(items4)); //must print [9, 6, 8, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9] System.out.println("--------------------------"); } } 

2 /2 lAdexo 2 2 DoNT CAKE No Dup LAes Figure 1 In the figure 1, the first array shown in black is passed as input into the function question1 of the class Question1.java. This function mutates the array to make it look like the second array. Notice carefully, that every single value in the second array starting from index k to n 1 are sorted and does not contain any duplicates. The values in the second array starting from 0 to k 1, we do not care. You can have slightly different values in index 0 to index k-1, and that is OK. However, it is important that you have the same values as shown in figure 1, starting from index k to index n-1. In figure 1, k = 4 2 /2 lAdexo 2 2 DoNT CAKE No Dup LAes Figure 1 In the figure 1, the first array shown in black is passed as input into the function question1 of the class Question1.java. This function mutates the array to make it look like the second array. Notice carefully, that every single value in the second array starting from index k to n 1 are sorted and does not contain any duplicates. The values in the second array starting from 0 to k 1, we do not care. You can have slightly different values in index 0 to index k-1, and that is OK. However, it is important that you have the same values as shown in figure 1, starting from index k to index n-1. In figure 1, k = 4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!