-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray3.java
More file actions
36 lines (34 loc) · 890 Bytes
/
Array3.java
File metadata and controls
36 lines (34 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Array3{
//maximum subarray sum (Kadane’s Algorithm)
public static int maxSubArray(int[] nums) {
int max= Integer.MIN_VALUE , curr=0;
for(int i=0 ;i<nums.length ; i++){
curr += nums[i];
max =Math.max(max,curr);
if(curr<0){
curr=0;
}
}
return max;
}
//Moore Voting Algorithm
public static int majorityElement(int arr[]){
int curr = 0;
int count = 0;
for( int num : arr){
if(count == 0){
curr = num;
}
if(curr == num){
count++;
}else{
count--;
}
}
return curr;
}
public static void main(String[] args) {
int a[] = {3,3,5,3,2,7,3,3};
System.out.println(majorityElement(a));
}
}