Prefix Product Array Last Updated : 26 Mar, 2021 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N integers the task is to generate a prefix product array from the given array. In a prefix product array, ith term pref[i] = arr[i] * arr[i - 1] * ...... * arr[0] Examples: Input: {1, 2, 3, 4, 5} Output: {1, 2, 6, 24, 120} Explanation: The Prefix Product Array will be {1, 2*1, 3*2*1, 4*3*2*1, 5*4*3*2*1} = {1, 2, 6, 24, 120}Input: {2, 4, 6, 5, 10} Output: {2, 8, 48, 240, 2400} Approach: Follow the steps below to solve the problem: Iterate over the given array from indices 1 to N - 1. Calculate arr[i] = arr[i] * arr[i-1] for every ith index. Finally, print the prefix product array. Below is the implementation of the above approach. C++ // C++ Program to generate // Prefix Product Array #include <bits/stdc++.h> using namespace std; // Function to generate // prefix product array int prefixProduct(int a[], int n) { // Update the array // with the product of // prefixes for (int i = 1; i < n; i++) { a[i] = a[i] * a[i - 1]; } // Print the array for (int j = 0; j < n; j++) { cout << a[j] << ", "; } return 0; } // Driver Code int main() { int arr[] = { 2, 4, 6, 5, 10 }; int N = sizeof(arr) / sizeof(arr[0]); prefixProduct(arr, N); return 0; } Java // Java program to generate // Prefix Product Array class GFG{ // Function to generate // prefix product array static int prefixProduct(int []a, int n) { // Update the array // with the product of // prefixes for(int i = 1; i < n; i++) { a[i] = a[i] * a[i - 1]; } // Print the array for(int j = 0; j < n; j++) { System.out.print(a[j] + ", "); } return 0; } // Driver Code public static void main (String[] args) { int arr[] = new int[]{ 2, 4, 6, 5, 10 }; int N = 5; prefixProduct(arr, N); } } // This code is contributed by Ritik Bansal Python3 # Python3 Program to generate # Prefix Product Array # Function to generate # prefix product array def prefixProduct(a, n): # Update the array # with the product of # prefixes for i in range(1, n): a[i] = a[i] * a[i - 1]; # Print the array for j in range(0, n): print(a[j], end = ", "); return 0; # Driver Code arr = [ 2, 4, 6, 5, 10 ]; N = len(arr); prefixProduct(arr, N); # This code is contributed by Code_Mech C# // C# program to generate // Prefix Product Array using System; class GFG{ // Function to generate // prefix product array static int prefixProduct(int []a, int n) { // Update the array // with the product of // prefixes for(int i = 1; i < n; i++) { a[i] = a[i] * a[i - 1]; } // Print the array for(int j = 0; j < n; j++) { Console.Write(a[j] + ", "); } return 0; } // Driver Code public static void Main (string[] args) { int []arr = new int[]{ 2, 4, 6, 5, 10 }; int N = 5; prefixProduct(arr, N); } } // This code is contributed by rock_cool JavaScript <script> // Javascript Program to generate // Prefix Product Array // Function to generate // prefix product array function prefixProduct(a, n) { // Update the array // with the product of // prefixes for (let i = 1; i < n; i++) { a[i] = a[i] * a[i - 1]; } // Print the array for (let j = 0; j < n; j++) { document.write(a[j] + ", "); } return 0; } let arr = [ 2, 4, 6, 5, 10 ]; let N = arr.length; prefixProduct(arr, N); </script> Output: 2, 8, 48, 240, 2400 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Prefix Product Array devro3014 Follow Improve Article Tags : Mathematical DSA Arrays prefix Practice Tags : ArraysMathematical Similar Reads Prefix Xor Array Given an array arr[] of size N, find the Prefix Xor of the array. A prefix xor array is another array prefixXor[] of the same size, such that the value of prefixXor[i] is arr[0] ^ arr[1] ^ arr[2] . . . arr[i]. Examples: Input: arr[] = {10, 20, 10, 5, 15}Output: prefixXor[] = {10, 30, 20, 17, 30 }Exp 4 min read Suffix Product Array Given an array nums[] of N integers the task is to generate a suffix product array from the given array. A Suffix Product Array is an array where each element at index i contains the product of all elements to the right of i (including the element at index i). Examples: Input: nums[] = {1, 2, 3, 4, 4 min read Suffix Sum Array Suffix Sum ArrayGiven an array arr[] of size N, the task is to compute and return its suffix sum array. Suffix Sum is a precomputation technique in which the sum of all the elements of the original array from an index i till the end of the array is computed. Therefore, this suffix sum array will be 10 min read Array Data Structure Complete Guide to ArraysLearn more about Array in DSA Self Paced CoursePractice Problems on ArraysTop Quizzes on Arrays What is Array?An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calcul 3 min read What is Array? Array is a linear data structure where all elements are arranged sequentially. It is a collection of elements of same data type stored at contiguous memory locations. For simplicity, we can think of an array as a flight of stairs where on each step is placed a value (let's say one of your friends). 2 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Suffix Array | Set 1 (Introduction) We strongly recommend to read following post on suffix trees as a pre-requisite for this post.Pattern Searching | Set 8 (Suffix Tree Introduction)A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suffix Tree which is compressed trie of all suffixes of t 15+ min read Java Array Exercise An array is a group of elements with similar data types that are bound together as one. The array allows us to store and manipulate a collection of elements together. Mastering arrays is essential for any Java developer, as it provides a solid foundation for more complex data structures and algorith 7 min read Array Rearrangement Arrays are fundamental data structures in programming, allowing us to store and manipulate collections of related data. One common operation we may need to perform on arrays is rearranging their elements. Array rearrangement can serve various purposes, such as sorting the elements, reversing the ord 3 min read Original Array from given Prefix Sums You are given a prefix sum array presum[] of an array arr[]. The task is to find the original array arr[] whose prefix sum is presum[].Examples: Input: presum[] = {5, 7, 10, 11, 18}Output: [5, 2, 3, 1, 7]Explanation: Original array {5, 2, 3, 1, 7} Prefix sum array = {5, 5+2, 5+2+3, 5+2+3+1, 5+2+3+1+ 4 min read Like