Skip to content

Commit

Permalink
Merge pull request #242 from dipeshs809/max-prod-subarray
Browse files Browse the repository at this point in the history
Max prod subarray
  • Loading branch information
jrijul1201 authored Oct 28, 2022
2 parents faee54d + e094194 commit d01d663
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
29 changes: 29 additions & 0 deletions InterviewBit/10_Sub_Matrices_with_sum_Zero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Difficulty :- medium
Ref :- https://www.interviewbit.com/problems/sub-matrices-with-sum-zero/
*/

#include <bits/stdc++.h>
using namespace std;
int solve(vector<vector<int> > &A) {
unordered_map<int, int> mp;
int ans=0, sum=0;
for(int i=0;i<A.size();i++){
for(int j=i;j<A.size();j++){
if(i!=j){
for(int k=0;k<A[j].size();k++) A[i][k]+=A[j][k];
}
mp.clear();
sum=0;
mp[sum]++;
for(auto x: A[i]){
sum+=x;
if(mp.find(sum)!=mp.end()){
ans+=mp[sum];
}
mp[sum]++;
}
}
}
return ans;
}
20 changes: 20 additions & 0 deletions InterviewBit/12-max-prod-subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Difficulty :- Medium
Ref :- https://www.interviewbit.com/problems/max-product-subarray/
*/

#include <bits/stdc++.h>
using namespace std;

int maxProduct(const vector<int> &A) {
int ma=1,mi=1, ans=INT_MIN;
for(auto x: A){
if(x<0){
swap(ma,mi);
}
ma= max(x, ma*x);
mi= min(x, mi*x);
ans= max(ma, ans);
}
return ans;
}

0 comments on commit d01d663

Please sign in to comment.