O(n) Solution in C


  • 0
    B
    int pivotIndex(int* nums, int numsSize) {
        int i = 0, sum = 0, result = -1, left = 0, right = 0;
        int *arrsum = (int *)malloc(sizeof(int) * numsSize);
        for(; i < numsSize; i++)
        {
            sum += *(nums + i);
            *(arrsum + i) = sum;
        }
        for(i = 0; i < numsSize; i++)
        {
            left = *(arrsum + i) - *(nums + i);
            right = sum - *(arrsum + i);
            if(left == right)
            {
                result = i;
                break;
            }
        }
        return result;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.