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

Geeksforgeeks Solution For " Sum of Middle Elements of two sorted arrays "
GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Sum of Middle Elements of two sorted arrays

Submit Your Solution :- Click Here 

Solution :- 

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int t,n,val;
cin>>t;
while(t--){

cin>>n;
int a[n],b[n];
vector<int>c(2*n);
for(int i=0;i<n;i++){
cin>>a[i];
}

for(int i=0;i<n;i++){
cin>>b[i];
}
merge(a,a+n,b,b+n,c.begin());
int s=c.size();
cout<<(c[(s/2)-1]+c[s/2])<<endl;

}
}

Output:-



Geeksforgeeks Solution For " Sum of Middle Elements of two sorted arrays "

Geeksforgeeks Solution For " Stock buy and sell "
GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Stock buy and sell

Submit Your Solution :- Click Here 

Solution :- 

#include<bits/stdc++.h>
using namespace std;
int main()
 {
int t;
    cin>>t;
    while(t--)
    {
        int n;
   cin>>n;
   int a[n];
   for(int i=0;i<n;i++)
       cin>>a[i];
   int buy=0,sell=0;
   bool key=false;
   for(int i=1;i<n;i++)
   {
       if(a[i]>a[i-1])
           sell++;
       else
       {
           if(buy!=sell)
           {
               cout<<"("<<buy<<" "<<sell<<") ";
               key=true;
           }
           buy=sell=i;
       }
   }
   if(buy!=sell)
   {
       cout<<"("<<buy<<" "<<sell<<") ";
        key=true;
   }
   if(!key)
        cout<<"No Profit";
   cout<<endl;       
    }
return 0;
}

Output:-



Geeksforgeeks Solution For " Stock buy and sell "

Geeksforgeeks Solution For " Reverse Coding "
GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Reverse Coding

Submit Your Solution :- Click Here 

Solution :- 

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
   int n,m;
   cin>>n>>m;
   int c=(n*(n+1))/2;
   if(c==m)
       cout<<1<<endl;
        else
            cout<<0<<endl;
}
return 0;
}

Output:-



Geeksforgeeks Solution For " Reverse Coding "

Geeksforgeeks Solution For " Move all negative elements to end "
GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Move all negative elements to end

Submit Your Solution :- Click Here 

Solution :- 

#include <iostream>
#include <vector>
using namespace std;

int main() {
int t;
    cin>>t;
    while(t--){
        int no,p=0,n=0;
        cin>>no;
        vector<int> po,ng;
        int a[no];
        for(int i=0;i<no;i++){
            cin>>a[i];
            if(a[i]>=0){
                p++;
                po.push_back(a[i]);
            }    
            else{
                n++;
                ng.push_back(a[i]);
            }    
        }
        for(int i=0;i<p;i++)
            a[i]=po[i];
        for(int i=0;i<n;i++)    
            a[p+i]=ng[i];
        for(int i=0;i<no;i++)    
            cout<<a[i]<<" ";
        cout<<endl;    
    }
return 0;
}

Output:-



Geeksforgeeks Solution For " Move all negative elements to end "

Geeksforgeeks Solution For " Maximum repeating number "
GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Maximum repeating number

Submit Your Solution :- Click Here 

Solution :- 

#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
   int n, k;
   cin>>n>>k;
   int a[n], b[n]={};
   for(int i=0; i<n; i++){
       cin>>a[i];
       b[a[i]]=b[a[i]]+1;
   }
   int index=0,max_v=b[0];
           
            for(int i=0;i<k;i++)
                if(b[i]>max_v){
                    max_v=b[i];
                    index=i;
                }
            cout<<index<<endl;
}
return 0;
}

Output:-



Geeksforgeeks Solution For " Maximum repeating number "