Tuesday, December 23, 2025
HomeData Modelling & AIReplace every element of the array by product of all other elements

Replace every element of the array by product of all other elements

Given an array of positive integers. Replace every element by the product of all other elements of the array.

Examples: 

Input : arr[] = { 2, 3, 3, 5, 7 } 
Output : 315 210 210 126 90

Approach : 

  • First, take the product of all the element of the array.
  • Now replace each element by the product divided by that element.
  • Print the modified array.

Below is the implementation of above approach: 

C++




// C++ program to Replace every element
// by the product of all other elements
#include "iostream"
using namespace std;
 
void ReplaceElements(int arr[], int n)
{
    int prod = 1;
 
    // Calculate the product of all the elements
    for (int i = 0; i < n; ++i) {
        prod *= arr[i];
    }
 
    // Replace every element product
    // of all other elements
    for (int i = 0; i < n; ++i) {
        arr[i] = prod / arr[i];
    }
}
 
int main()
{
    int arr[] = { 2, 3, 3, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    ReplaceElements(arr, n);
 
    // Print the modified array.
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    return 0;
}


Java




// Java program to Replace every element
// by the product of all other elements
 
class GFG{
static void ReplaceElements(int arr[], int n)
{
    int prod = 1;
 
    // Calculate the product of all the elements
    for (int i = 0; i < n; ++i) {
        prod *= arr[i];
    }
 
    // Replace every element product
    // of all other elements
    for (int i = 0; i < n; ++i) {
        arr[i] = prod / arr[i];
    }
}
 
public static void main(String[] args)
{
    int arr[] = { 2, 3, 3, 5, 7 };
    int n = arr.length;
 
    ReplaceElements(arr, n);
 
    // Print the modified array.
    for (int i = 0; i < n; ++i) {
        System.out.print(arr[i]+" ");
    }
    System.out.println("");
 
}
}
// This code is contributed by mits


Python 3




# Python 3 program to Replace every
# element by the product of all
# other elements
 
def ReplaceElements(arr, n):
    prod = 1
 
    # Calculate the product of
    # all the elements
    for i in range(n):
        prod *= arr[i]
 
    # Replace every element product
    # of all other elements
    for i in range(n) :
        arr[i] = prod // arr[i]
 
# Driver Code
if __name__ == "__main__":
    arr = [ 2, 3, 3, 5, 7 ]
    n = len(arr)
 
    ReplaceElements(arr, n)
 
    # Print the modified array.
    for i in range( n):
        print(arr[i], end = " ")
 
# This code is contributed
# by ChitraNayal


C#




// C# program to Replace every element
// by the product of all other elements
using System;
 
class GFG
{
static void ReplaceElements(int []arr, int n)
{
    int prod = 1;
 
    // Calculate the product of
    // all the elements
    for (int i = 0; i < n; ++i)
    {
        prod *= arr[i];
    }
 
    // Replace every element product
    // of all other elements
    for (int i = 0; i < n; ++i)
    {
        arr[i] = prod / arr[i];
    }
}
 
// Driver Code
static public void Main ()
{
    int []arr = { 2, 3, 3, 5, 7 };
    int n = arr.Length;
     
    ReplaceElements(arr, n);
     
    // Print the modified array.
    for (int i = 0; i < n; ++i)
    {
        Console.Write(arr[i]+" ");
    }
    Console.WriteLine("");
}
}
 
// This code is contributed by ajit


PHP




<?php
// PHP program to Replace every element
// by the product of all other elements
 
function ReplaceElements($arr, $n)
{
    $prod = 1;
 
    // Calculate the product of all
    // the elements
    for ($i = 0; $i < $n; ++$i)
    {
        $prod *= $arr[$i];
    }
 
    // Replace every element product
    // of all other elements
    for ($i = 0; $i < $n; ++$i)
    {
        $arr[$i] = (int)($prod / $arr[$i]);
    }
    return $arr;
}
 
// Driver Code
$arr = array( 2, 3, 3, 5, 7 );
$n = sizeof($arr);
 
$arr1 = ReplaceElements($arr, $n);
 
// Print the modified array.
for ($i = 0; $i < $n; ++$i)
{
    echo $arr1[$i] . " ";
}
 
// This code is contributed
// by Akanksha Rai
?>


Javascript




<script>
    // Javascript program to Replace every element
    // by the product of all other elements
     
    function ReplaceElements(arr, n)
    {
        let prod = 1;
 
        // Calculate the product of
        // all the elements
        for (let i = 0; i < n; ++i)
        {
            prod *= arr[i];
        }
 
        // Replace every element product
        // of all other elements
        for (let i = 0; i < n; ++i)
        {
            arr[i] = parseInt(prod / arr[i], 10);
        }
    }
     
    let arr = [ 2, 3, 3, 5, 7 ];
    let n = arr.length;
       
    ReplaceElements(arr, n);
       
    // Print the modified array.
    for (let i = 0; i < n; ++i)
    {
        document.write(arr[i]+" ");
    }
 
</script>


Output

315 210 210 126 90 

Complexity Analysis:

  • 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
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11959 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS