-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from heisenberg070/aniket
C++ solution to Product_finder
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |