-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion22.c
102 lines (85 loc) · 2.08 KB
/
question22.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Given an array of n-integers, construct product array such that prod[i] is
equal to product of all elements except arr[i] without using division operator
NOTE: we can find the product of all the elements and divide by the respective element which is not to be included,
but since division is not allowed we cannot use it
METHOD1
we can use a for loop and apply continue statement on the index which is not required
Time complexity: O(n^2)
Space complexity: O(1)
METHOD2
we can use two temp array two include cumulative product from left and right respectively.
Then we combine the two results to get result for each index
Time complexity: O(n)
Space complexity: O(n)
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
int findProd(int index, int arr[],int size){
int prod = 1;
for(int j=0; j<size;j++){
if(j==index){
continue;
}
prod = prod*arr[j];
}
return prod;
}
void printArr(int arr[], int size){
for(int i=0; i<size; i++){
printf("%d\n", arr[i]);
}
}
int main(){
int a[] = {10,20,30,40};
int size = sizeof(a)/sizeof(a[0]);
int prod[size];
for(int i=0; i<size;i++){
prod[i] = findProd(i, a, size);
}
printArr(prod, size);
}
//METHOD2
#include <stdio.h>
#include <stdlib.h>
void printArr(int arr[], int size){
for(int i=0; i<size; i++){
printf("%d\n", arr[i]);
}
}
void computeLeftProd(int leftProd[],int parent[], int size){
int prod = 1;
for(int i=0; i<size; i++){
prod = prod*parent[i];
leftProd[i] = prod;
}
}
void computeRightProd(int rightProd[], int parent[], int size){
int prod = 1;
for(int i=size-1; i>0; i--){
prod = prod*parent[i];
rightProd[i] = prod;
}
}
void computeFinalArr(int final[], int left[], int right[], int size){
for(int i=0; i<size; i++){
if(i == 0){
final[i] = right[i+1];
}
else if(i==size-1){
final[i] = left[i-1];
}else{
final[i] = left[i-1]*right[i+1];
}
}
}
int main(){
int a[] = {10,20,30,40};
int size = sizeof(a)/sizeof(a[0]);
int left[size],right[size], prod[size];
computeLeftProd(left,a,size);
computeRightProd(right,a,size);
computeFinalArr(prod,left,right,size);
printArr(prod,size);
}