Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HighestProduct.cpp #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 5 additions & 83 deletions Greedy/HighestProduct.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,7 @@
// https://www.interviewbit.com/problems/highest-product/

int Solution::maxp3(vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

if(A.size() < 3){
return 0;
}

long long int neg1 = INT_MAX, neg2 = INT_MAX;
int neg1_count = 0;

for(int i = 0; i < A.size(); i++){
if(A[i] < neg1 && A[i] < 0){
neg1_count = 1;
neg1 = A[i];
}
else if(A[i] == neg1 && A[i] < 0){
neg1_count++;
}
}

if(neg1_count > 1){
neg2 = neg1;
}
else if(neg1_count == 1){
for(int i = 0; i < A.size(); i++){
if(A[i] < 0 && A[i] < neg2 && neg1 < A[i]){
neg2 = A[i];
}
}
}

long long int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;
int max1_count = 0, max2_count = 0;

for(int i = 0; i < A.size(); i++){
if(A[i] > max1){
max1 = A[i];
max1_count = 1;
}
else if(A[i] == max1){
max1_count++;
}
}

if(max1_count > 1){
max1_count--;
max2 = max1;
}
else{
for(int i = 0; i < A.size(); i++){
if(A[i] > max2 && max1 > A[i]){
max2 = A[i];
max2_count = 1;
}
else if(A[i] == max2 && max1 > A[i]){
max2_count++;
}
}
}

if(max1_count > 1){
max3 = max1;
}
else if(max2_count > 1){
max3 = max2;
}
else{
for(int i = 0; i < A.size(); i++){
if(A[i] > max3 && max2 > A[i] && max1 > A[i]){
max3 = A[i];
}
}
}

if(neg1 == INT_MIN || neg2 == INT_MIN){
return (int)max1*max2*max3;
}

return (int)max(max1*max2*max3, max1*neg1*neg2);
sort(A.begin(), A.end());
int n = A.size();
int all_pos = A[n-3]*A[n-2]*A[n-1]; //can be all negative
int pos_neg = A[0]*A[1]*A[n-1]; // if 2nd last el is negative
return max(all_pos, pos_neg);
}