#include "vector"
#include "iterator"
#include "algorithm"
#include "iostream"
#include "stdlib.h"
using namespace std;
/*
* Pass 1
--->
1 2 3 4 5
^
Pass 2
<--
1 2 3 4 5
^
*/
void ArrayProduct(vector& array)
{
const int n = array.size();
vectorproduct(n);
int prod = 1;
for ( int i=0; i{
product[i] = prod;
prod *= array[i];
}
prod = 1;
for ( int i=n-1; i>=0; --i)
{
product[i] *= prod;
prod *= array[i];
}
copy ( product.begin(), product.end(),
ostream_iterator(cout, " ")
);
}
int main ( int argc, char* argv[])
{
const int n = atoi(argv[1]);
vectorarray(n);
for ( int i = 1; i<= n; ++i )
array[i-1] = i;
ArrayProduct(array);
return 0;
}
Product Array
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).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment