Find the two non-repeating elements in an array of repeating elements

PROBLEM :

You are given an array A containing 2*N+2 positive numbers, out of which N numbers are repeated exactly once and the other two numbers occur exactly once and are distinct. You need to find the other two numbers and print them in ascending order.

Input :
The first line contains a value T, which denotes the number of test cases. Then T test cases follow .The first line of each test case contains a value N. The next line contains 2*N+2 space separated integers.

Output :
Print in a new line the two numbers in ascending order.

Constraints :
1<=T<=100
1<=N<=10^6
1<=A[i]<=5*10^8

Example:
Input :
2
2
1 2 3 2 1 4
1
2 1 3 2

Output :
3 4
1 3
   
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
{
    int t ;
    cin>>t ;
    while(t--){
        int no ;
        cin>>no ;
       
        no=2*no+2 ;
        int arr[no] ;
        for(int i=0;i<no;i++)
            cin>>arr[i] ;
           
        int temp=0 ;
        for(int i=0;i<no;i++)
            temp=temp^arr[i] ;
           
        temp=temp&(~(temp-1)) ;
       
        int x,y ;
        x=0 ;
        y=0 ;
       
        for(int i=0;i<no;i++){
            if(arr[i]&temp)
                x=x^arr[i] ;
            else
                y=y^arr[i] ;
        }
       
        if(x<y)
            cout<<x<<" "<<y<<endl ;
        else
            cout<<y<<" "<<x<<endl ;
    }
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )