Thursday, July 30, 2009

Help on arrays in c?

There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].





Solve it without division operator and in O(n). ie (time complexity n)

Help on arrays in c?
one loop must be used to increment output array and other for number so it is for sure that you need double loop sits next to impossible to use it without double loops
Reply:Input: int A[N].


Output: Populated Output[N], assuming space for the array "Output" is already allocated in memory.


======================================...





for( int iCnt = 0; iCnt %26lt; N; iCnt++ )


{


int iProduct = 1;


for( int jCnt = 0; jCnt %26lt; N; jCnt++ )


{


if( jCnt == iCnt )


continue;


iProduct = iProduct * A[ jCnt ];


}


Output[ iCnt ] = iProduct;


}

narcissus

No comments:

Post a Comment