chore(CPlusPlus): add kadane for array (#558)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/592/head
MONIKA CHAUHAN 2021-10-18 00:12:14 +05:30 committed by GitHub
parent d04debeffb
commit 2f858d38ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 16 deletions

View File

@ -0,0 +1,82 @@
/* Maximum contiguous subarray Sum of array brute to optimized all approaches */
#include <bits/stdc++.h>
using namespace std;
//example int arr[3]={-1,3,-2};
/*subarrays are
-1
-1 3
-1 3 -2
3
3 -2
-2
3
ans=3
*/
int max_subarray_sum_method_1(int arr[],int n)
{
int sum,maxi;//declaring variables
maxi=INT_MIN;//initializing maxi by minimum value;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{ sum=0;
for(int k=i;k<=j;k++)
{
sum+=arr[k];//taking sum of elements from ith position to jth position
}
maxi=max(maxi,sum);//taking max of our final answer maxi and sum
}
}
return maxi;
}
//further optimized version by for two loops
int max_subarray_sum_method_2(int arr[],int n)
{
int sum,maxi;//declaring variables
maxi=INT_MIN;//initializing maxi by minimum value;
for(int i=0;i<n;i++)
{ sum=0;
for(int j=i;j<n;j++)
{
sum+=arr[j];//as we are just adding an element in our subarray so we can remove the k loop.
maxi=max(maxi,sum);//and taking max of maxi and sum
}
}
return maxi;
}
//most optimized version kadane's algo with one for loop
int max_subarray_sum_method_3(int arr[],int n)
{
int sum,maxi;//declaring variables
sum=0,maxi=arr[0];//initializing sum by 0 and maxi by 0th element;
for(int i=0;i<n;i++)
{
sum+=arr[i];
maxi=max(sum,maxi);
if(sum<0) sum=0;
}
return maxi;
}
int main()
{
int n;
cout<<"Enter the size of array"<<endl;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{ cout<<"Enter "<<i<<"th elements of array"<<endl;
cin>>arr[i];
}
int ans1=max_subarray_sum_method_1(arr,n);//this is brute force approach time complexity=O(n^3) || space complexity=O(1);
int ans2=max_subarray_sum_method_2(arr,n);//this is optimized brute force approach time complexity=O(n^2) || space complexity=O(1);
int ans3=max_subarray_sum_method_3(arr,n);//this is further optimized approach called "Kadane's Algorithm" time complexity=O(n)|| space complexity=O(1);
cout<<"max_subarray_sum:"<<ans1<<endl;
cout<<"max_subarray_sum:"<<ans2<<endl;
cout<<"max_subarray_sum:"<<ans3<<endl;//print any ans1,ans2,ans3;
}

View File

@ -1,7 +1,6 @@
# C++ # C++
## Arrays ## Arrays
- [Counting Inversions](Arrays/counting-inversions.cpp) - [Counting Inversions](Arrays/counting-inversions.cpp)
- [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp) - [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp)
- [Left Rotation](Arrays/left-rotation.cpp) - [Left Rotation](Arrays/left-rotation.cpp)
@ -20,20 +19,16 @@
- [Segregate 0s and 1s](Arrays/segregate-0-and-1.cpp) - [Segregate 0s and 1s](Arrays/segregate-0-and-1.cpp)
- [Search insert position](Arrays/search-insert-position.cpp) - [Search insert position](Arrays/search-insert-position.cpp)
- [Matrix Multiplication](Arrays/matrix-multiplication.cpp) - [Matrix Multiplication](Arrays/matrix-multiplication.cpp)
- [Smallest Possible Sum](Arrays/smallest-possible-sum.cpp) - [Kadane's Algorithm](Arrays/Kadane's-Algorithm.cpp)
## Dynamic-Programming ## Dynamic-Programming
- [Longest Common Subsequence](Dynamic-Programming/longest-common-subsequence.cpp) - [Longest Common Subsequence](Dynamic-Programming/longest-common-subsequence.cpp)
- [Longest Common Substring](Dynamic-Programming/longest-common-substring.cpp) - [Longest Common Substring](Dynamic-Programming/longest-common-substring.cpp)
- [0/1-knapsack](Dynamic-Programming/01-knapsack.cpp) - [0/1-knapsack](Dynamic-Programming/01-knapsack.cpp)
- [Matrix chain Multiplication](Dynamic-Programming/matrix-chain-multiplication.cpp) - [Matrix chain Multiplication](Dynamic-Programming/matrix-chain-multiplication.cpp)
- [Edit Distance](Dynamic-Programming/edit-distance.cpp) - [Edit Distance](Dynamic-Programming/edit-distance.cpp)
- [Maximum sum rectangle](Dynamic-Programming/maximum-sum-rectangle.cpp)
- [Coin Change](Dynamic-Programming/coin-change-problem.cpp)
## Graphs ## Graphs
- [Bellman Ford Algorithm](Graphs/bellman-ford.cpp) - [Bellman Ford Algorithm](Graphs/bellman-ford.cpp)
- [kruskal Algorithm](Graphs/kruskal-algorithm.cpp) - [kruskal Algorithm](Graphs/kruskal-algorithm.cpp)
- [Breadth First Search](Graphs/breadth-first-search.cpp) - [Breadth First Search](Graphs/breadth-first-search.cpp)
@ -43,11 +38,9 @@
- [Connected Components](Graphs/total-connected-components.cpp) - [Connected Components](Graphs/total-connected-components.cpp)
## Multiplication ## Multiplication
- [Karatsuba](Multiplication/karatsuba.cpp) - [Karatsuba](Multiplication/karatsuba.cpp)
## Linked Lists ## Linked Lists
- [All possible insertions](Linked-Lists/all-possible-insertion.cpp) - [All possible insertions](Linked-Lists/all-possible-insertion.cpp)
- [Singly linked lists](Linked-Lists/singly.cpp) - [Singly linked lists](Linked-Lists/singly.cpp)
- [doubley linked lists](Linked-Lists/doubly.cpp) - [doubley linked lists](Linked-Lists/doubly.cpp)
@ -62,7 +55,6 @@
- [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp) - [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp)
## Searching ## Searching
- [Linear Search](Searching/linear-search.cpp) - [Linear Search](Searching/linear-search.cpp)
- [Jump Search](Searching/jump-search.cpp) - [Jump Search](Searching/jump-search.cpp)
- [Binary Search](Searching/binary-search.cpp) - [Binary Search](Searching/binary-search.cpp)
@ -73,14 +65,12 @@
- [Exponential Search](Searching/exponential-search.cpp) - [Exponential Search](Searching/exponential-search.cpp)
## Stacks ## Stacks
- [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp) - [Balancing Parenthesis](Stacks/balanced-parenthesis.cpp)
- [Reversing Stack](Stacks/reverse-stack.cpp) - [Reversing Stack](Stacks/reverse-stack.cpp)
- [Stack using Array](Stacks/stack-using-array.cpp) - [Stack using Array](Stacks/stack-using-array.cpp)
- [Infix to postfix expression conversion](Stacks/infix-to-postfix.cpp) - [Infix to postfix expression conversion](Stacks/infix-to-postfix.cpp)
## Sorting ## Sorting
- [Bubble Sort](Sorting/bubble-sort.cpp) - [Bubble Sort](Sorting/bubble-sort.cpp)
- [Insertion Sort](Sorting/insertion-sort.cpp) - [Insertion Sort](Sorting/insertion-sort.cpp)
- [Quicksort](Sorting/quick-sort.cpp) - [Quicksort](Sorting/quick-sort.cpp)
@ -98,7 +88,6 @@
- [Cycle Sort](Sorting/cycle-sort.cpp) - [Cycle Sort](Sorting/cycle-sort.cpp)
## Strings ## Strings
- [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp) - [Rabin-Karp pattern search algo](Strings/rabin-karp.cpp)
- [All subsequence of a string (Recursion) ](Strings/sequence.cpp) - [All subsequence of a string (Recursion) ](Strings/sequence.cpp)
- [String reversal](Strings/string-reverse.cpp) - [String reversal](Strings/string-reverse.cpp)
@ -111,7 +100,6 @@
- [Boyer Moore pattern search](Strings/Boyer_Moore.cpp) - [Boyer Moore pattern search](Strings/Boyer_Moore.cpp)
## Trees ## Trees
- [Creating Binary Tree](Trees/build-binary-tree.cpp) - [Creating Binary Tree](Trees/build-binary-tree.cpp)
- [Counting and finding sum of all the nodes in BST](Trees/count-and-sum-of-nodes-in-binary-tree.cpp) - [Counting and finding sum of all the nodes in BST](Trees/count-and-sum-of-nodes-in-binary-tree.cpp)
- [Level Order Traversal](Trees/level-order-traversal.cpp) - [Level Order Traversal](Trees/level-order-traversal.cpp)
@ -125,10 +113,8 @@
- [Finding the elements of a tree visible from top view](Trees/Top-View-Of-A-Tree.cpp) - [Finding the elements of a tree visible from top view](Trees/Top-View-Of-A-Tree.cpp)
- [Binary Tree Implementation](Trees/binary-tree-implementation.cpp) - [Binary Tree Implementation](Trees/binary-tree-implementation.cpp)
- [Iterative Segment Tree](Trees/IterativeSegmentTree.cpp) - [Iterative Segment Tree](Trees/IterativeSegmentTree.cpp)
- [Fenwick Tree](Trees/Fenwick_Tree.cpp)
# Maths # Maths
- [Kaprekar Number](Maths/Kaprekar-number.cpp) - [Kaprekar Number](Maths/Kaprekar-number.cpp)
- [Prime Number](Maths/prime-check.cpp) - [Prime Number](Maths/prime-check.cpp)
- [Prime Sieve](Maths/prime-sieve.cpp) - [Prime Sieve](Maths/prime-sieve.cpp)
@ -143,7 +129,6 @@
- [Segmented Sieve](Maths/segmented-sieve-range.cpp) - [Segmented Sieve](Maths/segmented-sieve-range.cpp)
# Recursion # Recursion
- [Tower of Hanoi](Recursion/towerofHanoi.cpp) - [Tower of Hanoi](Recursion/towerofHanoi.cpp)
- [Factorial](Recursion/factorial.cpp) - [Factorial](Recursion/factorial.cpp)
- [Permutation](Recursion/permutation.cpp) - [Permutation](Recursion/permutation.cpp)
@ -161,3 +146,4 @@
- [Array sorted or not](Recursion\array-sorted-or-not.cpp) - [Array sorted or not](Recursion\array-sorted-or-not.cpp)
- [Product of two numbers](Recursion\product-of-numbers.cpp) - [Product of two numbers](Recursion\product-of-numbers.cpp)
- [Product of digits in a number](Recursion\product-of-digits.cpp) - [Product of digits in a number](Recursion\product-of-digits.cpp)