How can be array elements multiplied with big O(n)

user3507770

I have an array which it has N number.I would like to multiply array element as the following rule;

arr[n]={5,7,2,3,4....}

the first row:A[0]*A[2]*A[3]*A[4]....*A[n]
the second row:A[0]*A[1]*A[3]*A[4]....*A[n]
the third row:A[0]*A[1]*A[2]*A[4]...*A[n]
...........
the n row:A[0]*A[1]*A[2]*A[3]*A[4]....*A[n-1]

i did it with O(n^2) but i couldn't solve it O(n) how can i do that without division row element ?

AlexD

Assuming no zeros in the array, a possible approach could be

product = A[0]*...*A[n]
first = product / A[1]
second = product / A[2]
....

If division is not allowed, you can utilize left and right running products and do something like this:

int P[N], Q[N];
P[0] = A[0];
for(int i = 1; i < N; ++i)
    P[i] = P[i - 1] * A[i];
Q[N-1] = A[N-1]; 
for(int i = N-2; i >= 0; --i)
    Q[i] = Q[i+1] * A[i];

for(int i = 1; ....)
    R[i] = P[i-1] * Q[i+1];

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how can i use split() with a big number of elements, java

分類Dev

Merging the elements of nested arrays into one big array

分類Dev

How can I find the duplicated elements in a array and replace them?

分類Dev

Javascript: How can I swap elements of an array of objects (by reference, not index)?

分類Dev

How can I print multiple elements from an array inJavaSCript?

分類Dev

How can I remove all but the last N elements from an array?

分類Dev

How can I push the elements into array in a desired format

分類Dev

How can I repeat elements in place in an array in Python?

分類Dev

How big can CMS database be?

分類Dev

How can I create array elements dynamically and assign values (from variable with the same names) to the elements

分類Dev

how can i add all elements of a struct array into another struct array?

分類Dev

How to populate an array with same elements?

分類Dev

How to exchange elements in swift array?

分類Dev

Count how many elements in array

分類Dev

How to add an array elements in swift?

分類Dev

How can I select a non-sequential subset elements from an array using Scala and Spark?

分類Dev

How can I remove elements with even no of re-occurrence in an array with javascript?

分類Dev

As data is added to the firebase, the elements of the array increase. How can I prevent it?

分類Dev

First n elements of an array (which can be nil)

分類Dev

Multiplying 3 arrays to form a 3D array with entries multiplied

分類Dev

How can live without inheritance in closure templates in big project?

分類Dev

How can I join a df made in iteration, to build a big df?

分類Dev

How can I put the column values as column headers in Big Query

分類Dev

How can I treat my disk like one big file?

分類Dev

How to compress a 32 bit array elements into minimum required bit elements?

分類Dev

How to compare two object elements in a mongodb array

分類Dev

How to search an Array containing struct elements in Swift?

分類Dev

How to get the five least repeating elements in an array

分類Dev

How to find the location for all repeated elements in array?

Related 関連記事

  1. 1

    how can i use split() with a big number of elements, java

  2. 2

    Merging the elements of nested arrays into one big array

  3. 3

    How can I find the duplicated elements in a array and replace them?

  4. 4

    Javascript: How can I swap elements of an array of objects (by reference, not index)?

  5. 5

    How can I print multiple elements from an array inJavaSCript?

  6. 6

    How can I remove all but the last N elements from an array?

  7. 7

    How can I push the elements into array in a desired format

  8. 8

    How can I repeat elements in place in an array in Python?

  9. 9

    How big can CMS database be?

  10. 10

    How can I create array elements dynamically and assign values (from variable with the same names) to the elements

  11. 11

    how can i add all elements of a struct array into another struct array?

  12. 12

    How to populate an array with same elements?

  13. 13

    How to exchange elements in swift array?

  14. 14

    Count how many elements in array

  15. 15

    How to add an array elements in swift?

  16. 16

    How can I select a non-sequential subset elements from an array using Scala and Spark?

  17. 17

    How can I remove elements with even no of re-occurrence in an array with javascript?

  18. 18

    As data is added to the firebase, the elements of the array increase. How can I prevent it?

  19. 19

    First n elements of an array (which can be nil)

  20. 20

    Multiplying 3 arrays to form a 3D array with entries multiplied

  21. 21

    How can live without inheritance in closure templates in big project?

  22. 22

    How can I join a df made in iteration, to build a big df?

  23. 23

    How can I put the column values as column headers in Big Query

  24. 24

    How can I treat my disk like one big file?

  25. 25

    How to compress a 32 bit array elements into minimum required bit elements?

  26. 26

    How to compare two object elements in a mongodb array

  27. 27

    How to search an Array containing struct elements in Swift?

  28. 28

    How to get the five least repeating elements in an array

  29. 29

    How to find the location for all repeated elements in array?

ホットタグ

アーカイブ