Advertisement
Junaid_Hossain

Doremy's Paint 3

Oct 28th, 2023
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int distinct(int arr[], int n){
  5.     int different=0;
  6.  
  7.     for (int i = 0; i < n; i++) {
  8.         while (i < n - 1 && arr[i] == arr[i + 1]){
  9.             i++;
  10.         }
  11.  
  12.         different++;
  13.     }
  14.  
  15.     return different;
  16. }
  17.  
  18. int frequency(int arr[], int n){
  19.     multiset <int> s;
  20.     for(int i=0; i<n; i++){
  21.         s.insert(arr[i]);
  22.     }
  23.  
  24.     auto it1 = s.begin();
  25.     int x = s.count(*it1);
  26.  
  27.     auto it2 = s.end();
  28.     it2--;
  29.     int y = s.count(*it2);
  30.  
  31.     if(abs(x-y)==0 || abs(x-y)==1){
  32.         return 1;
  33.     }else{
  34.         return 0;
  35.     }
  36. }
  37.  
  38. int main(){
  39.     int t;
  40.     cin >> t;
  41.  
  42.     while(t--){
  43.         int n;
  44.         cin >> n;
  45.         int arr[n];
  46.  
  47.         for(int i=0; i<n; i++){
  48.             cin >> arr[i];
  49.         }
  50.  
  51.         sort(arr, arr+n);
  52.         int different= distinct(arr, n);
  53.  
  54.         if(different==1){
  55.             cout << "Yes\n";
  56.         }else if(different!=2){
  57.             cout << "No\n";
  58.         }else{
  59.             int freq= frequency(arr, n);
  60.             if(freq){
  61.                 cout << "Yes\n";
  62.             }else{
  63.                 cout << "No\n";
  64.             }
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement