Not able to find whats the error. The description shows lossy conversion between long and int.
import java.util.*;
public class Main {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
long[] arr = new long[n];
for(int i=0 ; i<n ;i++){
arr[i]=input.nextLong();
}
majority(n,arr);
}
public static void majority(int n , long[] arr){
long element1 = arr[0];
long element2 = 0;
int count1 =1;
int count2=0;
for(int i=1; i<n; i++){
if(element1==arr[i]) count1++;
else if (element2==arr[i]) count2++;
else if(count1==0) {
element1=arr[i];
count1=1;
}
else if(count2==0){
element2=arr[i];
count2=1;
}
else{
count1--;
count2--;
}
}
int c1=0;
int c2=0;
for(long i=0 ; i<n ; i++){
if(arr[i]==element1){
c1++;
}
else if(arr[i]==element2){
c2++;
}
}
if(c1>n/3 && c2>n/3){
System.out.println(element1+ " "+element2);
}
else if(c2>n/3){
System.out.println(element2);
}
else if(c1>n/3){
System.out.println(element1);
}
else if(c1<=n/3 &&c2<=n/3){
System.out.println("No Majority Elements");
}
}
}