Skip to content

Commit 7962706

Browse files
Added solution to math/day2/q001
1 parent 15d191f commit 7962706

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

1005 Bytes
Binary file not shown.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Link to submission: https://codeforces.com/contest/1771/submission/355440128
2+
3+
/*
4+
PROBLEM STATEMENT:
5+
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
6+
Now, they have an array a of n positive integers, Hossam will choose a number ai and Hazem will choose a number aj.
7+
Count the number of interesting pairs (ai, aj) that meet all the following conditions:
8+
-> 1 ≤ i, j ≤ n
9+
-> i ≠ j
10+
-> The absolute difference |ai − aj| must be equal to the maximum absolute difference over all the pairs in the array.
11+
More formally, |ai − aj| = max|ap − aq|, 1 ≤ p, q≤ n.
12+
*/
13+
14+
/*
15+
Brief Explanation:
16+
We find the minimum and the maximum element in the entire array and count the number of ordered pairs of min and max in the array.
17+
We count the frequency of miminum and maximum elements and use the formula of combinatorics to compute number of pairs.
18+
if min != max, we have count = freqMin C 1 * freqMax C 1 * 2
19+
if min == max, we have count = freqMax P 2
20+
*/
21+
22+
import java.util.*;
23+
24+
public class Solution1 {
25+
public static void main(String[] args) {
26+
Scanner sc = new Scanner (System.in);
27+
int t = sc.nextInt();
28+
while (t-- > 0) {
29+
long min = 100000;
30+
long max = 0;
31+
long freqMin = 0;
32+
long freqMax = 0;
33+
int n = sc.nextInt();
34+
long[] arr = new long[n];
35+
for (int i = 0; i < n; i++) {
36+
arr[i] = sc.nextLong();
37+
if (arr[i] < min)
38+
min = arr[i];
39+
if (arr[i] > max)
40+
max = arr[i];
41+
}
42+
for (int i = 0; i < n; i++) {
43+
if (arr[i] == min)
44+
freqMin++;
45+
if (arr[i] == max)
46+
freqMax++;
47+
}
48+
49+
long count = 0;
50+
if (min == max)
51+
count = freqMax * (freqMax - 1);
52+
else
53+
count = freqMin * freqMax * 2;
54+
System.out.println(count);
55+
}
56+
sc.close();
57+
}
58+
}
59+
60+
// Time Complexity: O(n)
61+
// Space Complexity: O(n)

0 commit comments

Comments
 (0)