Skip to content
Related Articles

Related Articles

Replace every element of the array by product of all other elements
  • Difficulty Level : Easy
  • Last Updated : 23 Sep, 2020

Given an array of 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, get the products below the current index
  • Now get the products above the current index
  • Print the modified array.

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
     
      // Get the products below the current index
    for(int i=0;i<n;++i) {
      arr[i]=prod;
      prod*=arr[i];
    }
 
    // Get the products above the curent index
    prod=1;
    for(int i=n-1;i>=0;--i) {
      arr[i]*=prod;
      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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
     
      // Get the products below the current index
    for(int i=0;i<n;++i) {
      arr[i]=prod;
      prod*=arr[i];
    }
 
    // Get the products above the curent index
    prod=1;
    for(int i=n-1;i>=0;--i) {
      arr[i]*=prod;
      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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to Replace every
# element by the product of all
# other elements
 
def ReplaceElements(arr, n):
    prod = 1
    is_zero_present = False
 
    # Calculate the product of
    # all the elements
    for i in range(n):
      if arr[i] == 0:
        is_zero_present = True
      if arr[i] > 0:
           prod *= arr[i]
 
    # Replace every element product
    # of all other elements
    for i in range(n) :
      if arr[i] == 0:
        arr[i] = prod
      else:
        if is_zero_present:
          arr[i] = 0
        else:
          arr[i] = prod // arr[i]
 
# Driver Code
if __name__ == "__main__":
    arr = [ 2, 3, 3, 5, 7, 0 ]
    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
# Modified and Handlded Exceptions by: Suraj Kumar Dubey

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output

315 210 210 126 90 


Output : 
 



315 210 210 126 90




Time Complexity – O(N)

Space Complexity – O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :