Skip to content

Commit 766416f

Browse files
committed
initial commit
1 parent c4639ac commit 766416f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Library/Array/prefixProduct.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int prefixProduct(int a[], int n){
5+
for (int i = 1; i<n; i++) {
6+
a[i] = a[i]*a[i-1];
7+
}
8+
9+
for (int i=0; i<n-1; i++){
10+
cout << a[i]<<", ";
11+
}
12+
cout << a[n - 1];
13+
return 0;
14+
}
15+
16+
int main(){
17+
int arr[5];
18+
for(int i =0; i<5; i++){
19+
cin >> arr[i];
20+
}
21+
// int arr[] = {2,4,6,5,10};
22+
23+
prefixProduct(arr,sizeof(arr)/sizeof(arr[0]));
24+
25+
return 0;
26+
}

Library/Array/suffixProduct.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int suffixProduct(int a[], int n){
5+
int suffix[n];
6+
suffix[n - 1] = a[n - 1];
7+
8+
// Compute suffix products
9+
for (int i = n - 2; i >= 0; i--) {
10+
suffix[i] = a[i] * suffix[i + 1];
11+
}
12+
for (int i=0; i<n; i++){
13+
cout << a[i]<< " ";
14+
}
15+
return 0;
16+
}
17+
18+
int main(){
19+
int arr[5];
20+
for(int i =0; i<5; i++){
21+
cin >> arr[i];
22+
}
23+
24+
25+
suffixProduct(arr,sizeof(arr)/sizeof(arr[0]));
26+
27+
return 0;
28+
}

0 commit comments

Comments
 (0)