Product of all but one number in a sequence - Follow Up This question is a follow up question to the Product of all but one number in a sequence.
I am posting a new code here with taking into consideration the suggestions of [Edward], [CiaPan], [chux], [superb rain] and others. Please advise how the code efficiency can be improved.
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;    

//without division, with O(n) time, but extra space complexity as suggested
//return new array on the heap 
int *find_product_arr(const int *nums, int arr_size)
{
    int *new_arr = (int *)malloc(sizeof(int)*arr_size);

    int mult_prefix=1; //product of prefix elements
    int mult_suffix=1; //product of suffix elements
    
    //left most element special handling
    new_arr[0]=1;
    
    //swipe up 
    for(int i=1; i&lt;arr_size; i++) {
        mult_prefix *= nums[i-1];
        new_arr[i] = mult_prefix;
    }
    
    //swipe down
    for(int j=arr_size-2; j&gt;=0; j--) {
        mult_suffix *= nums[j+1];
        new_arr[j] *= mult_suffix;
    }
        
    return new_arr;
}


int main(void)
{
    /*Given an array of integers, return a new array such that each element at index i of the 
    new array is the product of all the numbers in the original array except the one at i.
    For example, if our input was [1, 2, 3, 4, 5], the expected output would be 
    [120, 60, 40, 30, 24] */
    int nums[] = {1, 2, 2, 4, 6};    
    int size = sizeof(nums)/sizeof(nums[0]);
    
    int *products = find_product_arr(nums, size); //get a new array
    
    for (int i = 0; i &lt; size; i++) 
        printf(&quot;%d &quot;, *(products+i) ); 
    
    free(products); //release heap memory
   
    return 0;
}
You could eliminate the special case here:

//left most element special handling
new_arr[0]=1;

//swipe up 
for(int i=1; i&lt;arr_size; i++) {
    mult_prefix *= nums[i-1];
    new_arr[i] = mult_prefix;
}


by assigning before multiplying, and bringing index 0 into the loop:
//swipe up 
for(int i=0; i&lt;arr_size; i++) {
    new_arr[i] = mult_prefix;
    mult_prefix *= nums[i];
}

A similar transformation also applies to the downward sweep (so that each iteration only accesses nums[i], making it easier to reason about).
There is a cost associated with the simplification: an extra multiply, and risk of overflow (technically Undefined behaviour, even though we don't use the final value).