public int solution(int[] A) {
        int candidateIndex = findCandidateIndex(A);
        if (candidateIndex == -1) return 0;

        int candidate = A[candidateIndex];
        int candidateCount = 0;

        for (int i = 0; i < A.length; i++) {
            if (A[i] == candidate) candidateCount++;
        }

        int equiLeaderCount = 0;

        if (candidateCount > A.length / 2) {   //he is a real leader
             int runningLeaderCount = 0;
             for (int i = 0; i < A.length-1; i++) {
                if (A[i] == candidate) runningLeaderCount++;
                if (runningLeaderCount > (i + 1) / 2 && candidateCount - runningLeaderCount > (A.length - i - 1) / 2) equiLeaderCount++;
            }
        }
        return equiLeaderCount;
    }