PlayStation india

All related to gaming in india

LightBlog

Breaking

Sunday, 11 December 2022

A. Hossam and Combinatorics solution in c++ || Codeforces Round #837 (Div. 2)

 A. Hossam and Combinatorics

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.

Now, they have an array a of n positive integers, Hossam will choose a number ai and Hazem will choose a number aj.

Count the number of interesting pairs (ai,aj) that meet all the following conditions:

  • 1i,jn;
  • ij;
  • The absolute difference |aiaj| must be equal to the maximum absolute difference over all the pairs in the array. More formally, |aiaj|=max1p,qn|apaq|.
Input

The input consists of multiple test cases. The first line contains a single integer t (1t100), which denotes the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer n (2n105).

The second line of each test case contains n integers a1,a2,,an (1ai105).

It is guaranteed that the sum of n over all test cases does not exceed 105.



code of this question -----------------

  1. void solve()
  2. {
  3. int n;
  4. cin>>n;
  5. // int arr[n];
  6. // int min = INT_MAX;
  7. // int max = INT_MIN;
  8. map<int,int> mp;
  9. vector<int> arr(n);
  10. for(int i=0; i<n; ++i)
  11. {
  12. cin>>arr[i];
  13. mp[arr[i]]++;
  14. }
  15. sort(arr.begin(),arr.end());
  16. if(mp[arr[0]]==n)
  17. {
  18. cout<<n*(n -1)<<endl;
  19. }else{
  20. cout<<2*(mp[arr[0]]*mp[arr[n-1]])<<endl;
  21. }

No comments:

Post a Comment