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

a) Given an array of integers of any size, n 1, write an algorithm as a pseudo c

ID: 3887242 • Letter: A

Question

a) Given an array of integers of any size, n 1, write an algorithm as a pseudo code (not a program!) that would reverse every two consecutive elements of the left half of the array (i.e. reverse elements at index 0 &1, at index 2 & 3, etc.). Additionally, for the right half of the array, the algorithm must change the second element of every two consecutive elements to have to the sum of the two elements. For instance, if the right half starts at index 14, and the values at index 14 & 15 are 72 and 11, then the value at index 15 is changed to 83. If the given array is of an odd size, then the exact middle element should not be manipulated by the algorithm in any way. Finally, your algorithm must use the smallest auxiliary/additional storage to perform what is needed.

b) What is the time complexity of your algorithm, in terms of Big-O?

c) What is the space complexity of your algorithm, in terms of Big-O?

Explanation / Answer

arr[n] //given array

procedure swap(int &a,int &b)

tmp = a

a = b

b = tmp

if n%2 != 0

//1 2 3 4 5 6 7 8 9

for i : 0 to n/2-1

swap(arr[i],swap[i+1])

i = i+ 2;

for i: n/2+1 to n-1

arr[i+1] = a[i]+arr[i+1]

i = i+2;

else if n%2 == 0

//1 2 3 4 5 6

for i : 0 to n/2

swap(arr[i],swap[i+1])

i = i+ 2;

for i: n/2+1 to n-1

arr[i+1] = a[i]+arr[i+1]

i = i+2;

(a) As we are running two for loop but they not inside each other they run one after other

so time complexity is O(n)

(b)As we not using any runtime auxilary space

so space complexity is O(1)