1771A - Hossam and Combinatorics - CodeForces Solution


math math

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    mini_count = l.count(min(l))
    maxi_count = l.count(max(l))
    ins = 0
    if max(l) == min(l):
        print(mini_count * (mini_count - 1))
    else:
        print(maxi_count * mini_count * 2)

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
class DSU

{

    public :

    vector<int> parent , size;

    DSU(int n)

    {
        for(int i = 0 ; i <= n ; i++)

        {
            parent.push_back(i);
            size.push_back(1);
        }
    }

    int findPar(int node)

    {

        if(parent[node] == node)
        return node;

        return parent[node] = findPar(parent[node]);

    }

    

    void Union(int a , int b)
    {

        int parA = findPar(a);
        int parB = findPar(b);

        if(parA == parB)
        return ;
        if(size[parA] < size[parB])

        {
            size[parB] += size[parA];
            parent[parA] = parB;

        }
        else

        {
            size[parA] += size[parB];
           parent[parB] = parA;
        }
    }

};

int power(int x, int y, int p)
{
 
    // Initialize answer
    int res = 1;
 
    // Check till the number becomes zero
    while (y > 0) {
 
        // If y is odd, multiply x with result
        if (y % 2 == 1)
            res = (res * x);
 
        // y = y/2
        y = y >> 1;
 
        // Change x to x^2
        x = (x * x);
    }
    return res % p;
}


void solve()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 for(int i=0;i<n;i++)

 cin>>a[i];
 sort(a,a+n);

 if(a[0]==a[n-1])
 {
    cout<<n*(n-1)<<endl;
    return;
 }
 map<int,int>m;
 int maxi=*max_element(a,a+n);
 int mini=*min_element(a,a+n);
 for(int i=0;i<n;i++)
 {
   m[a[i]]++; 
}
  
 cout<<m[maxi]*m[mini]*2<<endl;


}



   

                                 
main()                                                                            
{
    ios_base::sync_with_stdio(false);                         
    cin.tie(NULL);                          
    #ifndef ONLINE_JUDGE                 
    freopen("input.txt", "r", stdin);                                                
    freopen("output.txt", "w", stdout);                    
    #endif         
    int test_cases=1;                       
    cin>>test_cases;

    for(int i=1;i<=test_cases;i++)
    { 
     solve();
    }
    cout<<fixed<<setprecision(10);
    cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n"; 
} 


Comments

Submit
0 Comments
More Questions

1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane