Welcome:
The question to the problem can be found here:
Explanation:
1. a[entered input] is the location of the input, say 10 it means the element is in the 10th position in the array.
2. Increase the value in that position by 1 when you get a different match of integers.
3. Store the first identical element in the 1st place of another array com[0] , second element to com[1]
4. Print YES if there are at exactly 2 different number in all cases.Then print those two numbers.
5. If not Print NO
6. Happy Coding!
Code:
#include<bits/stdc++.h>
using namespace std;
int a[100], com[2];
main(){
int sto=0;
int n;
cin>>n;
int x;
for(int i=0;i<n;i++){
cin>>x;
cin.ignore();
if(++a[x] == 1){ //if more than 1 loop condition becomes false;
com[sto++] = x;
}
}
if(sto == 2 && a[com[0]]== a[com[1]]){
cout<<"YES"<<endl;
cout<<com[0]<<" "<<com[1];
}
else{
cout<<"NO"<<endl;
}
}
No comments:
Post a Comment