I CODE THIS WRONG, THE EXPECTED OUT IS 0,-1, but I keep getting -1,-1. Please fix this
Question:
I CODE THIS WRONG, THE EXPECTED OUT IS 0,-1, but I keep getting -1,-1. Please fix this code for the expected output is 0, -1
import java.util.Stack;
public class hwkp4 {
// method that finds the leader element
// which appears more than half of the list elements
// returns the leader
// if leader not found then returns -1
static int leader(int[] a) {
// base cas
Stack
// do for each element
for(int i = 0; i < a.length;){
{
// pop all the elements that are less than the current element
while (!stack.isEmpty() && stack.peek() < i) {
stack.pop();
}
// push current element into the stack
stack.push(i);
}
int num = a[stack.peek()];
int count = 0;
for(i = 0; i < a.length; i++){
if(a[i] == num){
}
}
if(count < a.length / 2){
return -1;
}
return stack.pop();}
return 0;
}
public static void main(String argv[]) {
int[] a = {23, 23, 67, 23, 67, 23, 45};
System.out.println(leader(a));
int[] a1 = {23, 24, 67, 23, 67, 23, 45};
System.out.println(leader(a1));
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill