-
Notifications
You must be signed in to change notification settings - Fork 27
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 #51 from Prachi-2001/master
Added Kadane's Algo
- Loading branch information
Showing
2 changed files
with
55 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,22 @@ | ||
// Greatest Common Divisor Algorithm in recursive approach | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int gcd(int firstNum, int secondNum) { | ||
// firstNum%secondNum = secondNum; | ||
// when secondNum divides completely to the firstNum | ||
// firstNum%secondNum = remainder | ||
if(firstNum%secondNum==0){ | ||
return secondNum; | ||
} | ||
return gcd(secondNum,firstNum%secondNum); | ||
} | ||
|
||
int main(){ | ||
int firstnum , secondnum; | ||
cin>>firstnum>>secondnum; | ||
cout<<gcd(firstnum,secondnum); | ||
|
||
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,33 @@ | ||
// kadane's algorithm | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int max_array_sum(int ar[],int a){ | ||
|
||
int till_cur_sum=0 , total_sum=INT_MIN , max_element=INT_MIN; | ||
for(int j=0;j<a;j++){ | ||
till_cur_sum = till_cur_sum + ar[j]; | ||
total_sum = max(till_cur_sum,total_sum); | ||
// max_element= max(max_element,ar[j]); | ||
if(till_cur_sum<0){ | ||
till_cur_sum=0; | ||
} | ||
} | ||
if(total_sum==0){ | ||
total_sum = max_element; | ||
} | ||
return total_sum; | ||
} | ||
|
||
int main(){ | ||
int n; | ||
cin>>n; | ||
int arr[n]; | ||
|
||
for(int i=0;i<n;i++){ | ||
cin>>arr[i]; | ||
} | ||
cout<<" Maximum subarray sum : "<< max_array_sum(arr,n); | ||
return 0; | ||
} |