Find missing number in another array which is shuffled copy
PROBLEM :
Given an array ‘A’ of n positive integers. Contents of A[ ] are copied to another array ‘B’, but numbers are shuffled and one element is removed. The task is to find the missing element.
Examples:
Input : A[] = {4, 8, 1, 3, 7},
B[] = {7, 4, 3, 1}
Output : 8
Input : A[] = {12, 10, 15, 23, 11, 30},
B[] = {15, 12, 23, 11, 30}
Output : 10
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integers N, where N is the size of array. The second line of each test case contains N space separated integers denoting elements of the array A[ ]. The third line of each test case contains N-1 space separated integers denoting elements of the array B [ ].
Output:
Corresponding to each test case, in a new line, print the missing number.
Constraints:
1 <= T <=1000
1 <= N <=10000
1 <= A, B <=100000000
Example:
Input:
2
5
1 3 5 6 9
1 9 6 5
3
1 2 3
1 3
Output:
3
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--){
int no,i ;
cin>>no ;
int arr1[no],arr2[no-1] ;
for(i=0;i<no;i++)
cin>>arr1[i] ;
for(i=0;i<no-1;i++)
cin>>arr2[i] ;
int ans=0 ;
for(i=0;i<no;i++)
ans=ans^arr1[i] ;
for(i=0;i<no-1;i++)
ans=ans^arr2[i] ;
cout<<ans<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Given an array ‘A’ of n positive integers. Contents of A[ ] are copied to another array ‘B’, but numbers are shuffled and one element is removed. The task is to find the missing element.
Examples:
Input : A[] = {4, 8, 1, 3, 7},
B[] = {7, 4, 3, 1}
Output : 8
Input : A[] = {12, 10, 15, 23, 11, 30},
B[] = {15, 12, 23, 11, 30}
Output : 10
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integers N, where N is the size of array. The second line of each test case contains N space separated integers denoting elements of the array A[ ]. The third line of each test case contains N-1 space separated integers denoting elements of the array B [ ].
Output:
Corresponding to each test case, in a new line, print the missing number.
Constraints:
1 <= T <=1000
1 <= N <=10000
1 <= A, B <=100000000
Example:
Input:
2
5
1 3 5 6 9
1 9 6 5
3
1 2 3
1 3
Output:
3
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--){
int no,i ;
cin>>no ;
int arr1[no],arr2[no-1] ;
for(i=0;i<no;i++)
cin>>arr1[i] ;
for(i=0;i<no-1;i++)
cin>>arr2[i] ;
int ans=0 ;
for(i=0;i<no;i++)
ans=ans^arr1[i] ;
for(i=0;i<no-1;i++)
ans=ans^arr2[i] ;
cout<<ans<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment