Reverse of an Array

 1) Reverse of an Array 











Meaning












=> Approach 1













Dry Run:

i = arr.length -1

= 5-1

=4 ( last index)


i=> 4 = arr[4] = 10

i=> 3= arr[3] = 3

i=> 2 = arr[2] = 7

i=> 1 = arr[1] = 8

i=> 0 = arr[0] = 5

ans.push(arr[i])

arr[i] = [10, 3, 7, 8, 5]

T.C = o(n)

S.C = o(n) { used an extra array of n elements}


=> Approach 2









* the unshift operation takes o(n) time complexity 

T.C = n^2 (n*n)

S. c = o(n)

This code takes more time than the previous approach.








Comments