(array) prefix and prefix product and its application

Source: Internet
Author: User
What is prefix and prefix product?

The prefix and, prefix product are also called prefixes and arrays, prefixed by the plot no group.

Give an array a,

Prefix and: Create a new array B, each item in the array B[i] Save the and of [0...i] in A;

Suffix and: Create a new array B, each item in the array B[i] Save the and of [i...n-1] in A;

Prefix Product: Creates a new array B, each item in the array B[i] saves the product of a [0...i];

Suffix product: Create a new array B, each item in the array B[i] Save the product of [i...n-1] in A;

Application of prefix and prefix product (example): 1, Topic 1:

Given a floating-point group A, an array of b,b[i]=a[0]*a[1]*...*a[i-1]*a[i+1]*...*a[n-1], you cannot use division and do not allow new open arrays.

Ideas:

Ask for "suffix product" first:

for (int i=n-1;i>=0;i-) b[i]=a[i]* ((i==n-1) 1:b[i+1]);

Incidentally, "prefix product":

for (int i=0,j=1;i<n;j*=a[i++]) b[i]=j* ((i==n-1) 1:b[i+1]);

2. Topic 2:

For successive periods in an array and the absolute value of the smallest?

Ideas:

Prefix and nature of: a[i]+a[i+1]+...+a[j]=sum[j]-sum[i-1]

Prefix and sort, take minimum

3. Topic 3:

Separates an array from the middle P position so that the difference between a[0]+...+a[p-1] and a[p]+a[p+1]+...+a[n-1] is minimized?

Ideas:

  • Prefix and-(sum-prefix and) =2* prefix and-sum, are the minimum of the formula;
  • If all are non-negative, you can take the "two sweep" method, and the smaller one more than a number;

(array) prefix and prefix product and its application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.