forked from tanish2k09/algorithms_and_data_structures
/
product_except_self.cpp
51 lines (46 loc) · 1.35 KB
/
product_except_self.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Given an array of n integers where n > 1, nums, return an array output
* such that output[i] is equal to the product of all the elements of nums except nums[i].
* Example: [1, 2, 3, 4] ==> [24, 12, 8, 6]
*
* Try to use constant space. (excluding result array)
*/
#include <iostream>
#include <vector>
/*
* Idea is to maintain two product variables, one from beginning and
* one from the end.
* Each result variable in result array
* would be touched twice, one from the left to right
* and other right to left, thus producing desired result.
*/
std::vector<int> product_except_self(const std::vector<int>& nums)
{
int product_from_beginning = 1;
int product_from_end = 1;
unsigned int n = nums.size();
std::vector<int> result(n, 1);
for (unsigned int i = 0; i < n; ++i) {
result[i] *= product_from_beginning;
product_from_beginning *= nums[i];
result[n-1-i] *= product_from_end;
product_from_end *= nums[n-1-i];
}
return result;
}
void print_vector(const std::vector<int>& vec)
{
for (auto n : vec) {
std::cout << n << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> vec{1, 2, 3, 4};
std::cout << "Input Vector: ";
print_vector(vec);
std::vector<int> result = product_except_self(vec);
std::cout << "Output Vector: ";
print_vector(result);
}