tutorialcup@gmail.com

Product Array Puzzle - Get elements by multiplying all other elements

Arrays
Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

We need to construct an array where ith element will be the product of all the elements in the given array except element at ith position

Example

Input array

Output array element 1: 3 x 5 x 6 x 2
Output array element 2: 10 x 5 x 6 x 2
Output array element 3:  10 x 3 x 6 x 2
Output array element 4: 10 x 3 x 5 x 2
Output array element 5: 10 x 3 x 5 x 6

Output array will be

 

Algorithm

Time Complexity: O(n)
Space Complexity: O(n)

Step 1 : Take a vector product to store the products.
a)    Initialize it by vector product

Step 2 : Construct two arrays left[] and right[], left stores product of elements upto left of ith index in the array. right stores product of elements right of ith index.
a)    Initialize the first element of left as 1 and last element of right as 1.
b)    from left, update the ith elements of left array with product of the i-1 th element of given array with previous element of left array. (left[i] = left [i-1] * array[i-1]). by doing this, It stores the product till the previous index in the left array from the given array.
c)    from right, update the ith elements of right array with product of the i+1 th element of given array with next element of right array. (right[i] = right[i+1] *array[i+1]). by doing this, It stores the product till the previous index in the left array from the given array.

Step 3 :  Product except the present element will be same as product of left and right arrays elements.
a)    product array will be, product[i] = left[i]*right[i].

Algorithm working Example

C++ Program

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){
	
	ll arr[] = { 10, 3, 5, 6, 2}; //array
	int N = sizeof(arr) / sizeof(arr[0]); // size of array
	
	int left[N],right[N]; //arrays to store product upto left of ith index and right of ith index stored in left and right array respectively
	
	vector<int> product;
	left[0] = 1; //initialize the first element as 1
	for(int i=1; i<N; i++)
		left[i] = left[i-1]*arr[i-1]; // store the product till just previous index

	right[N-1] = 1;//initialzie the first element as 1
	for(int i = N-2; i>= 0 ;i--)
		right[i] = right[i+1]*arr[i+1]; //store the product till just next index
	 	
	 for(int i = 0; i < N; i++)
	 product.push_back(left[i]*right[i]); // product of the whole array except the current element is same as product of left and right array's ith index
	 
	 for(int i=0;i<N;i++)//display the product array
	 cout<<product[i]<<"  "; 
	 
	return 0;
}
Try It

 


C++ Online Test - Check Your Rank across the World


Next > < Prev
Scroll to Top