problems which I practise runs well on ide and gives correct on all cases that i can think.But when i try to submit them it shows wrong answer please guide me

poroblem code Voters
my code is:

#include<iostream>
using namespace std;
int cmptrg(int,int,int);
int cmptrl(int,int,int);
int main()
{int n1,n2,n3;
cin>>n1>>n2>>n3;
if(n1<0||n1>50000||n2<0||n3<0||n2>50000||n3>50000)
return 0;
int l=0;
int *list1=new int[n1];
int *list2=new int[n2];
int *list3=new int[n3];
int g=cmptrg(n1,n2,n3);
int *list=new int[g];
for(int i=0;i<n1;i++)
cin>>list1[i];
for(int i=0;i<n2;i++)
cin>>list2[i];
for(int i=0;i<n3;i++)
cin>>list3[i];
int i=0,j=0,k=0;
while(i<n1&&j<n2&&k<n3)
{
int l1=list1[i];
int l2=list2[j];
int l3=list3[k];
if(l1==l2==l3)
{
list[l++]=l1;
if(i<n1-1)i++;
if(j<n2-1)j++;
if(k<n3-1)k++;
}
else
{if(l1==l2)
{list[l++]=l1;
if(i<n1-1)i++;
if(j<n2-1)j++;
}
else if(l1==l3)
{list[l++]=l1;
if(i<n1-1)i++;
if(k<n3-1)k++;
}
else if(l2==l3)
{list[l++]=l2;
if(j<n2-1)j++;
if(k<n3-1)k++;
}
int r=cmptrl(l1,l2,l3);
if (r==1)
i++;
if(r==2)
j++;
if(r==3)
k++;
}
if(i==g-1||j==g-1||k==g-1)
{
i++;j++;k++;
}
}
cout<<l<<endl;
for(int w=0;w<l;w++)
cout<<list[w]<<endl;
return 0;
}

int cmptrg(int n1,int n2,int n3)
{
if(n1>=n2)
{
if(n3>=n1)
return n3;
else
return n1;
}
else
{
if(n3>=n2)
return n3;
else
return n2;
}
}
int cmptrl(int n1,int n2,int n3)
{
	if(n1<n2)
{
if(n3<n1)
return 3;
if(n1<n3)
return 1;
}
if(n1>n2)
{
if(n3<n2)
return 3;
if(n2<n3)
return 2;
}
return 0;
}

Check your case for this input-

Input
2 1 1
2
1
1
2
Your Output
1
2
Expected Output
2//This is number of voters in final list
1
2