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

array #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

array #218

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
37 changes: 37 additions & 0 deletions Array/MaximumSubarraySum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package p1;

import java.util.*;
public class MaximumSubarraySum {
//code to find the maximum sum of a subarray in an array using the kadane's algorithm

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);

System.out.println("enter the size of array");
int size=sc.nextInt();
int arr[]=new int[size];

System.out.println("enter the elements of the array");
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();

System.out.println("subarray with maximum sum: "+ kadane(arr));
}

public static int kadane(int arr[]){
if(arr==null)
return 0;

int sum=0;
int max=arr[0];
for(int i=0;i<arr.length;i++){
sum+=arr[i];
max=Math.max(max,sum);
if(sum<0)
sum=0;
}

return max;
}
}