157478 | CodeChef https://www.codechef.com/problems/VOTERS

#include
#include
#include
using namespace std;

int main() {
// your code goes here
int n1,n2,n3;
cin>>n1>>n2>>n3;

int a[n1+n2+n3];
for(int i=0;i<n1+n2+n3;i++)
{
    cin>>a[i];
}

int count = 0;
vector<int> arr;
sort(a, a+n1+n2+n3);
for(int i=0;i<n1+n2+n3;i++)
{
    for(int j=i;j<n1+n2+n3;j++)
    {
        if(a[i] == a[j])
        {
            count++;
        }
    }
    if(count>=2)
    {
        arr.push_back(a[i]);
    }
    count=0;
}

// for(int i=0;i<arr.size();i++)
// {
//     cout<<arr[i]<<" ";
// }
vector<int>::iterator ip; 
ip = std::unique(arr.begin(), arr.end()); 
arr.resize(std::distance(arr.begin(), ip)); 
 for (ip = arr.begin(); ip != arr.end(); ++ip) { 
    cout << *ip<<endl; 
} 
return 0;

}

this is my solution can anyone help me to make it less time complexity

This solution is in Java so I don’t know if the same can happen for Cpp too:

  1. Create two TreeSets temp and finalList (I’ve picked TreeSet as it takes O(lg n) to search for an element)

  2. Dump all of n1 values in temp.

  3. For each of n2 values, check whether the value exists in temp or not. If yes, remove that value from temp and store in finalList. If not, store it in temp.

  4. Do step 3 for each of n3 values.

  5. Print finalList values by popping from the head.

Time: O(n1+n2+n3)

This can be easily solved using map and most of the questions dealing with frequency can be solved using map in convenient manner.

Here is the link to my solution, The variables are named acc. to question: Link to AC Solution using map

Can anyone tell me where should i start to make my programming skill best

Please