06/04/2017

Geeksforgeeks Solution For Two Repeated Elements

Problem:- You are given an array of N+2 integer elements. All elements of the array are in range 1 to N. And all elements occur once except two numbers which occur twice. Find the two repeating numbers.

Input:

The first line of the input contains an integer T, denoting the total number of test cases. Then T test cases follow Each test case consists of two lines. The first line of each test case contains an integer N denoting the range of numbers to be inserted in an array of size N+2. The second line of each test case contains the N+2 space separated integers denoting the array elements.

Output:

Print the two elements occurring twice in the array. Order of the two elements must be preserved as in the original list, i.e., print the element which arrives first(2nd time).

Constraints:

1 ≤ T ≤ 30
1 ≤ N ≤ 100

Example:

INPUT

1
4
1 2 1 3 4 3

OUTPUT

1 3


Submit Your Solution:- Click Here

Solution:-

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
   int n;
   cin>>n;
   int a[n+2],b[n+2]={};

   for(int i=0;i<n+2;i++)
{
       cin>>a[i];
       b[a[i]]=b[a[i]]+1;
       if(b[a[i]]==2)
           cout<<a[i]<<" ";
   }
   cout<<endl;
}
return 0;
}


Output:-



Geeksforgeeks Solution For Two Repeated Elements

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: