Advertisement
audreych

12931 - Finding pairs - (1)

Jan 24th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. // math way
  3. int arr[100005];
  4. long long int cnt[10005];
  5. int main(){
  6.     int N;
  7.     long long int total = 0;
  8.     scanf("%d", &N);
  9.     for(int i = 0; i < N; i++){
  10.         scanf("%d", &arr[i]);
  11.         cnt[arr[i]]++;
  12.         //printf("cnt[%d] = %d\n", arr[i], cnt[arr[i]]);
  13.     }
  14.     for(int i = 0; i < 10001; i++){
  15.         if(cnt[i] <= 1) continue;
  16.         else {
  17.             // if there are more than one, e.g 5 of the same number
  18.             // it is the same as (5 2) 5 C 2 which is 5 * 4 / 2
  19.             total += (cnt[i]* (cnt[i] - 1)) /2;
  20.         }
  21.     }
  22.     printf("%lld\n", total);
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement