Question: Python A list of integer numbers is called exchanged if the second element is bigger than the first element, the third element is smaller than




Python A list of integer numbers is called exchanged if the second element is bigger than the first element, the third element is smaller than the second element, the fourth element is bigger than the third element and so on. Example : [20, 30, -10,-5, -15, 25, 20, 30, 28] We can see that the second element goes up. Starting with third element, each element goes opposite direction than the previous element. A list is called almost exchanged if one of the elements (only one) does not give the correct direction regarding the element that comes before it. For example, if it is the second element, then it is more small than the first instead of being more big than the first. If it is the third element, then it is more big than the second instead of being more small than the second. And so on. Example : [20, 30,-10, -15, -10,-20, 25, 20, 30,28] We can see : the incorrect element is -15 which is more small than -10 whereas it is supposed to be more big than-10. Write a function almost_exchanged(a) a is almost_exchanged list that has at least 2 elements. The function returns the index of the improper element in a. Examples: almost_exchanged([20, 30,-10,-15,-10,-20,25,20,30,28]) == 3 The element -15 in index 3 is improper. almost_exchanged([20, 10]) == 1 The second element (in index 1) should be more big than the first element). Requirements : Let n be the length of a Time == O(logn) Space == 0(1) Must NOT use recursion. Remark : if you do not manage to solve this in time O(logn), then try to solve this in time O(n) and get partial points in this case
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
