PROBLEM LINK:
Author: shrivas7
Tester: sandeep1103
Editorialist: shrivas7
DIFFICULTY:
Easy
PREREQUISITES:
Maps,Set,Hashing
EXPLANATION:
We know that in a Binary Search Tree the value of the left child of a node is less than that of the node, the value of the right child is greater than that of the node and there must not be any duplicate elements. So we need to check whether any such node values exist in the array or not. In other words we need to make sure that all the elements of the array are distinct for the construction of BST.
SOLUTIONS:
Setter's Solution
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t,n,x;
cin>>t;
while(t--)
{
cin>>n;
map<int,int> mp;
int fl=1;
for(int i=0;i<n;i++)
{
cin>>x;
mp[x]++;
if(mp[x]>1)
fl=0;
}
if(fl)
cout<<"Yes\n";
else
cout<<"No\n";
}
}
Tester's Solution
#include <bits/stdc++.h>
#define ll long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define ln cout << '\n'
#define mod 1000000007
#define MxN 100005
#define all(x) x.begin(), x.end()
using namespace std;
void solve(){
int i, j, n;
cin >> n;
set <int> st;
for(i = 0; i < n; ++i){
cin >> j;
st.insert(j);
}
cout << (((st.size() == n))?"Yes":"No");
ln;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
int t = 1;
cin >> t;
while(t--)
solve();
}