Skip to content

Commit

Permalink
Merge pull request #73 from heisenberg070/aniket
Browse files Browse the repository at this point in the history
C++ solution to Product_finder
  • Loading branch information
ZekromRND authored Oct 5, 2022
2 parents 210125d + dde8dff commit 186f9d4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
35 changes: 35 additions & 0 deletions coding_freshmen/C++/Aniket_Tripathy/PRODUCT_FINDER.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;


int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin >> arr[i];
}
int pivot;
cin >> pivot;
int prod = 1;

//edge case
if(pivot==0){
for (int i = 0; i < n;i++){
if(arr[i]==0){
continue;
}else{
prod *= arr[i];
}
}
cout << prod << endl;
exit(0);
}

//normal case
for(int i=0;i<n;i++){
prod *= arr[i];
}
cout << prod / pivot << endl;
return 0;
}
12 changes: 12 additions & 0 deletions coding_freshmen/C++/Aniket_Tripathy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Problem Title: PRODUCT_FINDER
Find the product of the elements of the array except the given pivot element

# Problem Explanation 🚀
We need to find the product of all elements of the array while skipping the pivot element

# Intuition and Logic🧠
* Assuming that all the elements in the array are unique we can simply calculate the products of all the elements in a prod variable, and when we encounter the pivot element we can simply use the continue; statememt to skip that element.

# Time Complexity and Space Complexity
* Time Complexity: O(n)
* Space Complexity: O(1)

0 comments on commit 186f9d4

Please sign in to comment.