

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
A product array puzzle (O(1) Space) in C++?
Here we will see one interesting problem related to array. There is an array with n elements. We have to create another array of n elements. But the i-th position of second array will hold the product of all elements of the first array except the i-th element. And one constraint is that we cannot use the division operator in this problem. We have to solve this problem in-place without using any additional spaces.
If we can use the division, operation, we can easily solve this problem, by getting the product of all elements, then divide i-th element of first array and store it into i-th place of the second array.
Here we are solving by taking one temp variable, that is finding the product of left part and the right part. This value will be placed into the final array. So it will not take extra space.
Algorithm
productArray(arr, n)
begin define an array called res of size n fill the res array with 1 temp := 1 for i in range 1 to n, do res[i] := temp; temp := temp * arr[i] done for i in range n-1 down to 0, do res[i] = res[i] * temp temp := temp * arr[i] done return res end
Example
#include<iostream> using namespace std; void printArray(int arr[], int n) { for(int i = 0; i<n; i++) { cout << arr[i] << " "; } cout << endl; } void productArray(int arr[], int product[], int n) { int temp = 1; for(int i = 0; i<n; i++) { product[i] = 1; //set all elements of product as 1 } for(int i = 0; i < n; i++) { //temp variable will hold product of elements on left excluding arr[i] product[i] = temp; temp *= arr[i]; } temp = 1; for(int i = n - 1; i >= 0; i--) { //temp variable will hold product of elements on right excluding arr[i] product[i] *= temp; temp *= arr[i];} } } main() { int myArr[7] = {5, 4, 7, 6, 9, 2, 3}; int resArr[7]; cout << "Initial Array: "; printArray(myArr, 7); productArray(myArr, resArr, 7); cout << "Final Array: "; printArray(resArr, 7); }
Output
Initial Array: 5 4 7 6 9 2 3 Final Array: 9072 11340 6480 7560 5040 22680 15120
- Related Questions & Answers
- A Product Array Puzzle in C++?
- A Product Array Puzzle in C?
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Print left rotation of array in O(n) time and O(1) space in C Program.
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Find median of BST in O(n) time and O(1) space in C++
- Find duplicates in constant array with elements 0 to N-1 in O(1) space in C++
- Print BST keys in given Range - O(1) Space in C++
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Find median of BST in O(n) time and O(1) space in Python
- Check for balanced parentheses in an expression - O(1) space - O(N^2) time complexity in C++