Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coding Activity #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Array & Hash/Kadane_algorithm_01.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Base Algo is Dynamic Programing and approach is greedy approach.
//At ech step we are considering local maxima rather than looking at whole array

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please mention the Time Complexity for this solution.

public class Solution {
public int findSubarraySum(int[] a, int n) {
int sum = 0;
int maxi = a[0];

for (int i = 0; i < n; i++) {
sum += a[i];
maxi = Math.max(maxi, sum);

if (sum < 0) {
sum = 0;
}
}
return maxi;
}

public static void main(String[] args) {
Solution solution = new Solution();
int[] array = {-2, -3, 4, -1, -2, 1, 5, -3};
int result = solution.findSubarraySum(array, array.length);
System.out.println("Maximum subarray sum: " + result);
}
}