Question: A jagged list is a list where the values go up and down as we traverse the list of unique elements. For example, the list
A jagged list is a list where the values go up and down as we traverse the list of unique elements. For example, the list below has values go up and down at every step. Jagged list: 3, 7, 4, 8, 2, 6, 1 (as 3 < 7 > 4 < 8 > 2 < 6 > 1). a. Describe an efficient algorithm that converts a given list into a jagged list. You need to solve this problem in O(n) time and in O(1) space. Note that your algorithm must minimize the number of changes to the position of the elements in the original list. In other words, you need to output a jagged list with minimal distance between the original list and the jagged list as shown below (b section)
b. Develop a program in Java to convert a normal list of unique numbers into a jagged list. For example: Given a list: 4, 3, 7, 8, 6, 2, 1 Output the jagged list: 4, 3, 7, 6, 8, 1, 2 Distance between the 2 lists: element 4: 0->0 = 0; element 3: 1->1 = 0; element 7: 2->2 = 0; element 8: 3->4 = 1; element 6: 4->3 = 1; element 2: 5->6 = 1; element 1: 6->5 = 1; distance = sum(0,0,0,1,1,1,1) = 4
Please help me with this question in Java, thank you
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
