Saturday, September 6, 2025
HomeData Modelling & AIPrefix Product Array

Prefix Product Array

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)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS