28/10/2016

C++ Program To Product Array Such That Product Is Equal To The Product Of All The Elements Of An Array

Problem :- Given An Array arr[] Of n Integers, Construct A Product Array prod[] (Of Same Size) Such That prod[i] Is Equal To The Product Of All The Elements Of Arr[] Except arr[i]. Solve It Without Using Division Operator And In O(n).

Solution :-

#include<iostream>
using namespace std;
int main()
{
  int a[100];
  int n,i,j,left[100],right[100],product[100];
  cout<<"Enter The Size Of An Array \n";
  cin>>n;
  cout<<"\nEnter The Elelment Of The Array \n";
  for(i=0;i<n;i++)
  {
  cin>>a[i];
}
 
left[0]=1;
  right[n-1]=1;
  for(i=1;i<n;i++)
  left[i]=a[i-1]*left[i-1];
  for(j=n-2;j>=0;j--)
  right[j]=a[j+1]*right[j+1];
  cout<<"Product Of Array Without Division Operator\n\n";
  for(i=0;i<n;i++)
  {
    product[i]=left[i]*right[i];
    cout<<product[i]<<" ";
      }
    return 0;
}

Output :-


C++ Program To Product Array Such That Product Is Equal To The Product Of All The Elements Of An Array

Previous Post
Next Post

post written by:

2 comments:

  1. Thanks for this programming tutorial.

    ReplyDelete
    Replies
    1. Thanks Brother You Like This Tutorial Don't Forget To Share

      Delete