forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63324d7
commit f88fcff
Showing
2 changed files
with
59 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,58 @@ | ||
// Github Username: amitShindeGit | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Solution{ | ||
public: | ||
// arr: input array | ||
// n: size of array | ||
//Function to find the sum of contiguous subarray with maximum sum. | ||
long long max(long long x, long long y){ | ||
if(x > y){ | ||
return x; | ||
}else{ | ||
return y; | ||
} | ||
} | ||
|
||
long long maxSubarraySum(int arr[], int n){ | ||
|
||
// Your code here | ||
long long max_current = arr[0]; | ||
long long max_global = arr[0]; | ||
|
||
for(int i=1; i<=n-1; i++){ | ||
max_current = max(arr[i], max_current + arr[i]); | ||
// cout << max_current << " " << max_global << endl; | ||
|
||
if(max_current > max_global){ | ||
max_global = max_current; | ||
} | ||
} | ||
|
||
return max_global; | ||
|
||
} | ||
}; | ||
|
||
|
||
int main() | ||
{ | ||
int t,n; | ||
|
||
cin>>t; //input testcases | ||
while(t--) //while testcases exist | ||
{ | ||
|
||
cin>>n; //input size of array | ||
|
||
int a[n]; | ||
|
||
for(int i=0;i<n;i++) | ||
cin>>a[i]; //inputting elements of array | ||
|
||
Solution ob; | ||
|
||
cout << ob.maxSubarraySum(a, n) << endl; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
| Sarthak Roy | <a href="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/sarthakroy2002">Sarthak Roy</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Hardik Pratap Singh | <a href="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/hardik-pratap-singh">Hardik Pratap Singh</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Suraj Bhandarkar S | <a href="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/Suraj-Bhandarkar-S">Adarsh Addee</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
| Amit Shinde | <a href="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/amitShindeGit">Amit Shinde</a> | <a href="mailto:[email protected]">E-Mail</a> | | ||
|
||
<br> | ||
<h1> | ||
|