-
Notifications
You must be signed in to change notification settings - Fork 4
/
PUZZLE.C
62 lines (49 loc) · 1.28 KB
/
PUZZLE.C
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
51
52
53
54
55
56
57
58
59
60
61
62
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
/* Function to print product array
for a given array arr[] of size n */
void productArray(int arr[], int n)
{
// Base case
if (n == 1) {
cout << 0;
return;
}
/* Allocate memory for temporary
arrays left[] and right[] */
int* left = new int[sizeof(int) * n];
int* right = new int[sizeof(int) * n];
/* Allocate memory for the product array */
int* prod = new int[sizeof(int) * n];
int i, j;
/* Left most element of left
array is always 1 */
left[0] = 1;
/* Right most element of right
array is always 1 */
right[n - 1] = 1;
/* Construct the left array */
for (i = 1; i < n; i++)
left[i] = arr[i - 1] * left[i - 1];
/* Construct the right array */
for (j = n - 2; j >= 0; j--)
right[j] = arr[j + 1] * right[j + 1];
/* Construct the product array using
left[] and right[] */
for (i = 0; i < n; i++)
prod[i] = left[i] * right[i];
/* print the constructed prod array */
for (i = 0; i < n; i++)
cout << prod[i] << " ";
return;
}
/* Driver code*/
int main()
{
int arr[] = { 10, 3, 5, 6, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The product array is: \n";
productArray(arr, n);
}
// This is code is contributed by rathbhupendra