Find Missing And Repeating
PROBLEM :
Given an unsorted array of size n. Array elements are in range from 1 to n. One number 'A' from set {1, 2, …n} is missing and one number 'B' occurs twice in array. Find these two numbers.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print B, the repeating number followed by A which is missing in a single line.
Constraints:
1 = T = 40
1 = N = 100
1 = A[i] = N
Example:
Input
2
2
2 2
3
1 3 3
Output
2 1
3 2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void bubble(int[],int) ;
int main()
{
int t,no,a[100],i,diff,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
bubble(a,no) ;
for(i=0;i<no-1;i++)
{
if(a[i]==a[i+1])
{
cout<<a[i]<<" " ;
k=a[i] ;
}
}
for(i=0;i<no-1;i++)
{
diff=a[i+1]-a[i] ;
if((diff==0)&&(a[i]!=k))
{
cout<<a[i]-1 ;
break ;
}
else if(diff>1)
{
cout<<a[i]+1 ;
break ;
}
}
cout<<endl ;
}
return 0;
}
void bubble(int a[],int no)
{
int i,j,temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j] ;
a[j]=a[j+1] ;
a[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Given an unsorted array of size n. Array elements are in range from 1 to n. One number 'A' from set {1, 2, …n} is missing and one number 'B' occurs twice in array. Find these two numbers.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print B, the repeating number followed by A which is missing in a single line.
Constraints:
1 = T = 40
1 = N = 100
1 = A[i] = N
Example:
Input
2
2
2 2
3
1 3 3
Output
2 1
3 2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void bubble(int[],int) ;
int main()
{
int t,no,a[100],i,diff,k ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
bubble(a,no) ;
for(i=0;i<no-1;i++)
{
if(a[i]==a[i+1])
{
cout<<a[i]<<" " ;
k=a[i] ;
}
}
for(i=0;i<no-1;i++)
{
diff=a[i+1]-a[i] ;
if((diff==0)&&(a[i]!=k))
{
cout<<a[i]-1 ;
break ;
}
else if(diff>1)
{
cout<<a[i]+1 ;
break ;
}
}
cout<<endl ;
}
return 0;
}
void bubble(int a[],int no)
{
int i,j,temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j] ;
a[j]=a[j+1] ;
a[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment