I have an array of n characters. To calculate the sum of the array I have used the following code snippet:
var arr = [1,2,3,4,5,6]
var i = 0;
var j = arr.length - 1;
var sum = 0;
while(i<j || i==j){
sum = sum + ((i==j) ? arr[i] : arr[i] + arr[j])
i++;
j--;
}
I have used two pointers i and j to traverse the array from both directions and it finds the sum. I wanted to know whether this is a correct approach and does it run in O(log n) time complexity?
Thanks