Open In App

Suffix Product Array

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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, 5}
Output: {120, 120, 60, 20, 5}
Explanation: {1 * 2 * 3 * 4 * 5, 2 * 3 * 4 * 5, 3 * 4 * 5, 4 * 5, 5} = {120, 120, 60, 20, 5}

Input: nums[] = {3, 2, 1}
Output: {6, 2, 1}
Explanation: {3 * 2 * 1, 2 * 1, 1} = {6, 2, 1}

Approach: To solve the problem, follow the below idea:

The approach involves creating a suffix product array where each element at index i contains the product of all elements to the right of (and including) index i in the original array.

Step-by-step algorithm:

  • Initialize a result vector suffixProduct[] with the same size as the input array and set each element to 1 initially.
  • It then iterates from the second-last element to the first element of the input array, updating each element in suffixProduct by multiplying it with the next element to the right.
  • Print the suffixProduct array as the suffix product array.

Below is the implementation of the approach:

C++




#include <bits/stdc++.h>
 
using namespace std;
 
vector<int> calculateSuffixProduct(const vector<int>& nums)
{
    int n = nums.size();
    vector<int> suffixProduct(n, 1);
 
    for (int i = n - 2; i >= 0; i--) {
        suffixProduct[i]
            = suffixProduct[i + 1] * nums[i + 1];
    }
 
    return suffixProduct;
}
 
int main()
{
    vector<int> inputArray = { 1, 2, 3, 4, 5 };
 
    vector<int> suffixProductArray
        = calculateSuffixProduct(inputArray);
 
    // Adjusting each element to include the element at
    // index i
    for (int i = 0; i < inputArray.size(); i++) {
        suffixProductArray[i] *= inputArray[i];
    }
 
    for (int product : suffixProductArray) {
        cout << product << " ";
    }
 
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
 
public class SuffixProduct {
 
    static List<Integer> calculateSuffixProduct(List<Integer> nums) {
        int n = nums.size();
        List<Integer> suffixProduct = new ArrayList<>(n);
 
        for (int i = 0; i < n; i++) {
            suffixProduct.add(1);
        }
 
        for (int i = n - 2; i >= 0; i--) {
            suffixProduct.set(i, suffixProduct.get(i + 1) * nums.get(i + 1));
        }
 
        return suffixProduct;
    }
 
    public static void main(String[] args) {
        List<Integer> inputArray = List.of(1, 2, 3, 4, 5);
 
        List<Integer> suffixProductArray = calculateSuffixProduct(inputArray);
 
        for (int i = 0; i < inputArray.size(); i++) {
            suffixProductArray.set(i, suffixProductArray.get(i) * inputArray.get(i));
        }
 
        for (int product : suffixProductArray) {
            System.out.print(product + " ");
        }
    }
}


Python3




def calculate_suffix_product(nums):
    n = len(nums)
    suffix_product = [1] * n
 
    for i in range(n - 2, -1, -1):
        suffix_product[i] = suffix_product[i + 1] * nums[i + 1]
 
    return suffix_product
 
if __name__ == "__main__":
    input_array = [1, 2, 3, 4, 5]
 
    suffix_product_array = calculate_suffix_product(input_array)
 
    # Adjusting each element to include the element at
    # index i
    for i in range(len(input_array)):
        suffix_product_array[i] *= input_array[i]
 
    for product in suffix_product_array:
        print(product, end=" ")
         
# This code is contributed by shivamgupta0987654321


C#




using System;
using System.Collections.Generic;
 
class Program
{
    // Function to calculate the suffix product of an array
    static List<int> CalculateSuffixProduct(List<int> nums)
    {
        int n = nums.Count;
        List<int> suffixProduct = new List<int>(n);
 
        // Initialize the last element of the suffix product array
        suffixProduct.Add(1);
 
        // Calculate suffix product for each element in the array
        for (int i = n - 1; i > 0; i--)
        {
            // Multiply the current suffix product by the next element
            suffixProduct.Insert(0, suffixProduct[0] * nums[i]);
        }
 
        return suffixProduct;
    }
 
    static void Main()
    {
        // Input array
        List<int> inputArray = new List<int> { 1, 2, 3, 4, 5 };
 
        // Calculate the suffix product for each element in the array
        List<int> suffixProductArray = CalculateSuffixProduct(inputArray);
 
        // Adjust each element to include the element at its index
        for (int i = 0; i < inputArray.Count; i++)
        {
            suffixProductArray[i] *= inputArray[i];
        }
 
        // Print the final suffix product array
        Console.WriteLine("Resulting Suffix Product Array:");
        foreach (int product in suffixProductArray)
        {
            Console.Write(product + " ");
        }
 
        Console.ReadLine(); // To keep the console window open
    }
}


Javascript




function calculateSuffixProduct(nums) {
    const n = nums.length;
    const suffixProduct = new Array(n).fill(1);
 
    for (let i = n - 2; i >= 0; i--) {
        suffixProduct[i] = suffixProduct[i + 1] * nums[i + 1];
    }
 
    return suffixProduct;
}
 
function main() {
    const inputArray = [1, 2, 3, 4, 5];
 
    const suffixProductArray = calculateSuffixProduct(inputArray);
 
    // Adjusting each element to include the element at
    // index i
    for (let i = 0; i < inputArray.length; i++) {
        suffixProductArray[i] *= inputArray[i];
    }
 
    for (const product of suffixProductArray) {
        console.log(product);
    }
}
 
main();


Output

120 120 60 20 5 

Time Complexity: O(N), where N is the size of input array nums[].
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads