AR16
QUESTION DESCRIPTION
Given an unsorted array that contains even number of occurrences for all numbers except two numbers.
Find the two numbers in decreasing order which have odd occurrences in O(n) time complexity and O(1) extra space.
Mandatory conditions should be " while(t--)"
Input:
The first line of input contains an integer T denoting the number of test cases.
Then T test cases follow. Each test case contains an integer 'n' denoting the size of the array.
Then the following line contains n space separated integers.
Output:
Print two space separated integers which have odd occurrences. Print the greater number first and then the smaller odd number.
Constraints:
1<=T<=10^5
2<=n<=10^5
1<=Ai<=10^5
Given an unsorted array that contains even number of occurrences for all numbers except two numbers.
Find the two numbers in decreasing order which have odd occurrences in O(n) time complexity and O(1) extra space.
Mandatory conditions should be " while(t--)"
Input:
The first line of input contains an integer T denoting the number of test cases.
Then T test cases follow. Each test case contains an integer 'n' denoting the size of the array.
Then the following line contains n space separated integers.
Output:
Print two space separated integers which have odd occurrences. Print the greater number first and then the smaller odd number.
Constraints:
1<=T<=10^5
2<=n<=10^5
1<=Ai<=10^5
TEST CASE 2
INPUT
INPUT
2
6
2 4 1 1 4 5
6
8 0 0 7 7 5
OUTPUT5 2
8 5
#include <iostream>
using namespace std;
void printTwoOdd(int arr[], int size)
{
int xor2 = arr[0];
int set_bit_no;
int i;
int n = size - 2;
int x = 0, y = 0;
for(i = 1; i < size; i++)
xor2 = xor2 ^ arr[i];
set_bit_no = xor2 & ~(xor2-1);
for(i = 0; i < size; i++)
{
if(arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
if(x>y)
cout<<x<<" "<<y<<endl;
else
cout<<y<<" "<<x<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int arr[100],arr_size;
cin>>arr_size;
for(int i=0;i<arr_size;i++)
{
cin>>arr[i];
}
printTwoOdd(arr, arr_size);
}
return 0;
}
Comments
Post a Comment