Skip to content

Commit

Permalink
Merge pull request #51 from Prachi-2001/master
Browse files Browse the repository at this point in the history
Added Kadane's Algo
  • Loading branch information
keshavagarwal17 authored Oct 19, 2021
2 parents 0181a56 + 5e3d7e3 commit 8813502
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Greatest_common_divisor_Algo.cpp
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;
}
33 changes: 33 additions & 0 deletions kadane's_algorithm.cpp
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;
}

0 comments on commit 8813502

Please sign in to comment.